]>
git.proxmox.com Git - mirror_frr.git/blob - lib/mpls.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2018 Cumulus Networks, Inc.
13 * String to label conversion, labels separated by '/'.
15 * @param label_str labels separated by /
16 * @param num_labels number of labels; zero if conversion was unsuccessful
17 * @param labels preallocated mpls_label_t array of size MPLS_MAX_LABELS; only
18 * modified if the conversion succeeded
19 * @return 0 on success
20 * -1 if the string could not be parsed as integers
21 * -2 if a label was inside the reserved range (0-15)
22 * -3 if the number of labels given exceeds MPLS_MAX_LABELS
24 int mpls_str2label(const char *label_str
, uint8_t *num_labels
,
27 char *ostr
; // copy of label string (start)
28 char *lstr
; // copy of label string
29 char *nump
; // pointer to next segment
30 char *endp
; // end pointer
31 int i
; // for iterating label_str
32 int rc
; // return code
33 mpls_label_t pl
[MPLS_MAX_LABELS
]; // parsed labels
35 /* labels to zero until we have a successful parse */
36 ostr
= lstr
= XSTRDUP(MTYPE_TMP
, label_str
);
40 for (i
= 0; i
< MPLS_MAX_LABELS
&& lstr
&& !rc
; i
++) {
41 nump
= strsep(&lstr
, "/");
42 pl
[i
] = strtoul(nump
, &endp
, 10);
48 else if (!IS_MPLS_UNRESERVED_LABEL(pl
[i
]))
53 if (!rc
&& i
== MPLS_MAX_LABELS
&& lstr
)
58 memcpy(labels
, pl
, *num_labels
* sizeof(mpls_label_t
));
61 XFREE(MTYPE_TMP
, ostr
);
67 * Label to string conversion, labels in string separated by '/'.
69 char *mpls_label2str(uint8_t num_labels
, const mpls_label_t
*labels
, char *buf
,
70 int len
, enum lsp_types_t type
, int pretty
)
72 char label_buf
[BUFSIZ
];
76 for (i
= 0; i
< num_labels
; i
++) {
78 strlcat(buf
, "/", len
);
80 label2str(labels
[i
], type
, label_buf
,
83 snprintf(label_buf
, sizeof(label_buf
), "%u",
84 ((type
== ZEBRA_LSP_EVPN
)
85 ? label2vni(&labels
[i
])
88 strlcat(buf
, label_buf
, len
);