Skip to content

Commit 67f67ba

Browse files
committed
bgpd: Drop label_ntop/label_pton functions
Start using mpls_lse_encode/mpls_lse_decode, that is endian-aware, because we always use host-byte order, should use network-byte. Signed-off-by: Donatas Abraitis <[email protected]>
1 parent 134890b commit 67f67ba

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed

bgpd/bgp_label.c

+27-14
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int bgp_parse_fec_update(void)
8585
if (label == MPLS_INVALID_LABEL)
8686
bgp_unset_valid_label(&dest->local_label);
8787
else {
88-
label_ntop(label, 1, &dest->local_label);
88+
dest->local_label = mpls_lse_encode(label, 0, 0, 1);
8989
bgp_set_valid_label(&dest->local_label);
9090
}
9191
SET_FLAG(dest->flags, BGP_NODE_LABEL_CHANGED);
@@ -129,9 +129,16 @@ static void bgp_send_fec_register_label_msg(struct bgp_dest *dest, bool reg,
129129
uint16_t flags = 0;
130130
size_t flags_pos = 0;
131131
mpls_label_t *local_label = &(dest->local_label);
132-
bool have_label_to_reg =
133-
bgp_is_valid_label(local_label)
134-
&& label_pton(local_label) != MPLS_LABEL_IMPLICIT_NULL;
132+
uint32_t ttl = 0;
133+
uint32_t bos = 0;
134+
uint32_t exp = 0;
135+
mpls_label_t label = MPLS_INVALID_LABEL;
136+
bool have_label_to_reg;
137+
138+
mpls_lse_decode(*local_label, &label, &ttl, &exp, &bos);
139+
140+
have_label_to_reg = bgp_is_valid_label(local_label) &&
141+
label != MPLS_LABEL_IMPLICIT_NULL;
135142

136143
p = bgp_dest_get_prefix(dest);
137144

@@ -142,7 +149,7 @@ static void bgp_send_fec_register_label_msg(struct bgp_dest *dest, bool reg,
142149
if (BGP_DEBUG(labelpool, LABELPOOL))
143150
zlog_debug("%s: FEC %sregister %pRN label_index=%u label=%u",
144151
__func__, reg ? "" : "un", bgp_dest_to_rnode(dest),
145-
label_index, label_pton(local_label));
152+
label_index, label);
146153
/* If the route node has a local_label assigned or the
147154
* path node has an MPLS SR label index allowing zebra to
148155
* derive the label, proceed with registration. */
@@ -161,7 +168,7 @@ static void bgp_send_fec_register_label_msg(struct bgp_dest *dest, bool reg,
161168
stream_putl(s, label_index);
162169
} else if (have_label_to_reg) {
163170
flags |= ZEBRA_FEC_REGISTER_LABEL;
164-
stream_putl(s, label_pton(local_label));
171+
stream_putl(s, label);
165172
}
166173
SET_FLAG(dest->flags, BGP_NODE_REGISTERED_FOR_LABEL);
167174
} else
@@ -216,13 +223,13 @@ int bgp_reg_for_label_callback(mpls_label_t new_label, void *labelid,
216223
*/
217224
if (CHECK_FLAG(dest->flags, BGP_NODE_REGISTERED_FOR_LABEL)) {
218225
UNSET_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED);
219-
label_ntop(MPLS_LABEL_IMPLICIT_NULL, 1,
220-
&dest->local_label);
226+
dest->local_label = mpls_lse_encode(
227+
MPLS_LABEL_IMPLICIT_NULL, 0, 0, 1);
221228
bgp_set_valid_label(&dest->local_label);
222229
}
223230
}
224231

225-
label_ntop(new_label, 1, &dest->local_label);
232+
dest->local_label = mpls_lse_encode(new_label, 0, 0, 1);
226233
bgp_set_valid_label(&dest->local_label);
227234

228235
/*
@@ -238,9 +245,16 @@ void bgp_reg_dereg_for_label(struct bgp_dest *dest, struct bgp_path_info *pi,
238245
{
239246
bool with_label_index = false;
240247
const struct prefix *p;
241-
bool have_label_to_reg =
242-
bgp_is_valid_label(&dest->local_label)
243-
&& label_pton(&dest->local_label) != MPLS_LABEL_IMPLICIT_NULL;
248+
bool have_label_to_reg;
249+
uint32_t ttl = 0;
250+
uint32_t bos = 0;
251+
uint32_t exp = 0;
252+
mpls_label_t label = MPLS_INVALID_LABEL;
253+
254+
mpls_lse_decode(dest->local_label, &label, &ttl, &exp, &bos);
255+
256+
have_label_to_reg = bgp_is_valid_label(&dest->local_label) &&
257+
label != MPLS_LABEL_IMPLICIT_NULL;
244258

245259
p = bgp_dest_get_prefix(dest);
246260

@@ -283,8 +297,7 @@ void bgp_reg_dereg_for_label(struct bgp_dest *dest, struct bgp_path_info *pi,
283297
}
284298
} else {
285299
UNSET_FLAG(dest->flags, BGP_NODE_LABEL_REQUESTED);
286-
bgp_lp_release(LP_TYPE_BGP_LU, dest,
287-
label_pton(&dest->local_label));
300+
bgp_lp_release(LP_TYPE_BGP_LU, dest, label);
288301
}
289302

290303
bgp_send_fec_register_label_msg(

bgpd/bgp_label.h

-19
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,6 @@ static inline void bgp_unregister_for_label(struct bgp_dest *dest)
9898
bgp_reg_dereg_for_label(dest, NULL, false);
9999
}
100100

101-
/* Label stream to value */
102-
static inline uint32_t label_pton(const mpls_label_t *label)
103-
{
104-
uint8_t *t = (uint8_t *)label;
105-
return ((((unsigned int)t[0]) << 12) | (((unsigned int)t[1]) << 4)
106-
| ((unsigned int)((t[2] & 0xF0) >> 4)));
107-
}
108-
109-
/* Encode label values */
110-
static inline void label_ntop(uint32_t l, int bos, mpls_label_t *label)
111-
{
112-
uint8_t *t = (uint8_t *)label;
113-
t[0] = ((l & 0x000FF000) >> 12);
114-
t[1] = ((l & 0x00000FF0) >> 4);
115-
t[2] = ((l & 0x0000000F) << 4);
116-
if (bos)
117-
t[2] |= 0x01;
118-
}
119-
120101
/* Return BOS value of label stream */
121102
static inline uint8_t label_bos(mpls_label_t *label)
122103
{

bgpd/bgp_route.c

+15-7
Original file line numberDiff line numberDiff line change
@@ -2958,8 +2958,9 @@ static void bgp_process_main_one(struct bgp *bgp, struct bgp_dest *dest,
29582958
dest->flags,
29592959
BGP_NODE_LABEL_REQUESTED))
29602960
bgp_unregister_for_label(dest);
2961-
label_ntop(MPLS_LABEL_IMPLICIT_NULL, 1,
2962-
&dest->local_label);
2961+
dest->local_label = mpls_lse_encode(
2962+
MPLS_LABEL_IMPLICIT_NULL, 0, 0,
2963+
1);
29632964
bgp_set_valid_label(&dest->local_label);
29642965
} else
29652966
bgp_register_for_label(dest,
@@ -9872,6 +9873,10 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct bgp_dest *bn,
98729873
int i;
98739874
char *nexthop_hostname =
98749875
bgp_nexthop_hostname(path->peer, path->nexthop);
9876+
uint32_t ttl = 0;
9877+
uint32_t bos = 0;
9878+
uint32_t exp = 0;
9879+
mpls_label_t label = MPLS_INVALID_LABEL;
98759880

98769881
if (json_paths) {
98779882
json_path = json_object_new_object();
@@ -10614,7 +10619,8 @@ void route_vty_out_detail(struct vty *vty, struct bgp *bgp, struct bgp_dest *bn,
1061410619
/* Remote Label */
1061510620
if (path->extra && bgp_is_valid_label(&path->extra->label[0])
1061610621
&& (safi != SAFI_EVPN && !is_route_parent_evpn(path))) {
10617-
mpls_label_t label = label_pton(&path->extra->label[0]);
10622+
mpls_lse_decode(path->extra->label[0], &label, &ttl, &exp,
10623+
&bos);
1061810624

1061910625
if (json_paths)
1062010626
json_object_int_add(json_path, "remoteLabel", label);
@@ -11433,12 +11439,14 @@ void route_vty_out_detail_header(struct vty *vty, struct bgp *bgp,
1143311439
int has_valid_label = 0;
1143411440
mpls_label_t label = 0;
1143511441
json_object *json_adv_to = NULL;
11442+
uint32_t ttl = 0;
11443+
uint32_t bos = 0;
11444+
uint32_t exp = 0;
1143611445

11437-
p = bgp_dest_get_prefix(dest);
11438-
has_valid_label = bgp_is_valid_label(&dest->local_label);
11446+
mpls_lse_decode(dest->local_label, &label, &ttl, &exp, &bos);
1143911447

11440-
if (has_valid_label)
11441-
label = label_pton(&dest->local_label);
11448+
p = bgp_dest_get_prefix(dest);
11449+
has_valid_label = bgp_is_valid_label(&label);
1144211450

1144311451
if (safi == SAFI_EVPN) {
1144411452

bgpd/bgp_zebra.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,9 @@ void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
12741274
uint64_t cum_bw = 0;
12751275
uint32_t nhg_id = 0;
12761276
bool is_add;
1277+
uint32_t ttl = 0;
1278+
uint32_t bos = 0;
1279+
uint32_t exp = 0;
12771280

12781281
/* Don't try to install if we're not connected to Zebra or Zebra doesn't
12791282
* know of this instance.
@@ -1465,7 +1468,8 @@ void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
14651468
if (mpinfo->extra
14661469
&& bgp_is_valid_label(&mpinfo->extra->label[0])
14671470
&& !CHECK_FLAG(api.flags, ZEBRA_FLAG_EVPN_ROUTE)) {
1468-
label = label_pton(&mpinfo->extra->label[0]);
1471+
mpls_lse_decode(mpinfo->extra->label[0], &label, &ttl,
1472+
&exp, &bos);
14691473

14701474
SET_FLAG(api_nh->flags, ZAPI_NEXTHOP_FLAG_LABEL);
14711475

@@ -1493,7 +1497,8 @@ void bgp_zebra_announce(struct bgp_dest *dest, const struct prefix *p,
14931497
&mpinfo->extra->label[0]))
14941498
continue;
14951499

1496-
label = label_pton(&mpinfo->extra->label[0]);
1500+
mpls_lse_decode(mpinfo->extra->label[0], &label,
1501+
&ttl, &exp, &bos);
14971502
transpose_sid(&api_nh->seg6_segs, label,
14981503
sid_info->transposition_offset,
14991504
sid_info->transposition_len);

0 commit comments

Comments
 (0)