]> git.proxmox.com Git - mirror_frr.git/blob - lib/mpls.h
Merge pull request #12789 from donaldsharp/version_cleanup
[mirror_frr.git] / lib / mpls.h
1 /*
2 * MPLS definitions
3 * Copyright 2015 Cumulus Networks, Inc.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2, or (at your
10 * option) any later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _QUAGGA_MPLS_H
23 #define _QUAGGA_MPLS_H
24
25 #include <zebra.h>
26 #include <vxlan.h>
27 #include <arpa/inet.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #ifdef MPLS_LABEL_MAX
34 #undef MPLS_LABEL_MAX
35 #endif
36
37 #define MPLS_LABEL_HELPSTR \
38 "Specify label(s) for this route\nOne or more " \
39 "labels in the range (16-1048575) separated by '/'\n"
40
41 /* Well-known MPLS label values (RFC 3032 etc). */
42 #define MPLS_LABEL_IPV4_EXPLICIT_NULL 0 /* [RFC3032] */
43 #define MPLS_LABEL_ROUTER_ALERT 1 /* [RFC3032] */
44 #define MPLS_LABEL_IPV6_EXPLICIT_NULL 2 /* [RFC3032] */
45 #define MPLS_LABEL_IMPLICIT_NULL 3 /* [RFC3032] */
46 #define MPLS_LABEL_ELI 7 /* [RFC6790] */
47 #define MPLS_LABEL_GAL 13 /* [RFC5586] */
48 #define MPLS_LABEL_OAM_ALERT 14 /* [RFC3429] */
49 #define MPLS_LABEL_EXTENSION 15 /* [RFC7274] */
50 #define MPLS_LABEL_MAX 1048575
51 #define MPLS_LABEL_VALUE_MASK 0x000FFFFF
52 #define MPLS_LABEL_NONE 0xFFFFFFFF /* for internal use only */
53
54 /* Minimum and maximum label values */
55 #define MPLS_LABEL_RESERVED_MIN 0
56 #define MPLS_LABEL_RESERVED_MAX 15
57 #define MPLS_LABEL_UNRESERVED_MIN 16
58 #define MPLS_LABEL_UNRESERVED_MAX 1048575
59 #define MPLS_LABEL_BASE_ANY 0
60
61 /* Default min and max SRGB label range */
62 /* Even if the SRGB allows to manage different Label space between routers,
63 * if an operator want to use the same SRGB for all its router, we must fix
64 * a common range. However, Cisco start its SRGB at 16000 and Juniper ends
65 * its SRGB at 16384 for OSPF. Thus, by fixing the minimum SRGB label to
66 * 8000 we could deal with both Cisco and Juniper.
67 */
68 #define MPLS_DEFAULT_MIN_SRGB_LABEL 8000
69 #define MPLS_DEFAULT_MAX_SRGB_LABEL 50000
70 #define MPLS_DEFAULT_MIN_SRGB_SIZE 5000
71 #define MPLS_DEFAULT_MAX_SRGB_SIZE 20000
72
73 /* Maximum # labels that can be pushed. */
74 #define MPLS_MAX_LABELS 16
75
76 #define IS_MPLS_RESERVED_LABEL(label) (label <= MPLS_LABEL_RESERVED_MAX)
77
78 #define IS_MPLS_UNRESERVED_LABEL(label) \
79 (label >= MPLS_LABEL_UNRESERVED_MIN \
80 && label <= MPLS_LABEL_UNRESERVED_MAX)
81
82 /* Definitions for a MPLS label stack entry (RFC 3032). This encodes the
83 * label, EXP, BOS and TTL fields.
84 */
85 typedef unsigned int mpls_lse_t;
86
87 #define MPLS_LS_LABEL_MASK 0xFFFFF000
88 #define MPLS_LS_LABEL_SHIFT 12
89 #define MPLS_LS_EXP_MASK 0x00000E00
90 #define MPLS_LS_EXP_SHIFT 9
91 #define MPLS_LS_S_MASK 0x00000100
92 #define MPLS_LS_S_SHIFT 8
93 #define MPLS_LS_TTL_MASK 0x000000FF
94 #define MPLS_LS_TTL_SHIFT 0
95
96 #define MPLS_LABEL_VALUE(lse) \
97 ((lse & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT)
98 #define MPLS_LABEL_EXP(lse) ((lse & MPLS_LS_EXP_MASK) >> MPLS_LS_EXP_SHIFT)
99 #define MPLS_LABEL_BOS(lse) ((lse & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT)
100 #define MPLS_LABEL_TTL(lse) ((lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT)
101
102 #define IS_MPLS_LABEL_BOS(ls) (MPLS_LABEL_BOS(ls) == 1)
103
104 #define MPLS_LABEL_LEN_BITS 20
105
106 /* MPLS label value as a 32-bit (mostly we only care about the label value). */
107 typedef unsigned int mpls_label_t;
108
109 struct mpls_label_stack {
110 uint8_t num_labels;
111 uint8_t reserved[3];
112 mpls_label_t label[0]; /* 1 or more labels */
113 };
114
115 /* The MPLS explicit-null label is 0 which means when you memset a mpls_label_t
116 * to zero you have set that variable to explicit-null which was probably not
117 * your intent. The work-around is to use one bit to indicate if the
118 * mpls_label_t has been set by the user. MPLS_INVALID_LABEL has this bit clear
119 * so that we can use MPLS_INVALID_LABEL to initialize mpls_label_t variables.
120 */
121 #define MPLS_INVALID_LABEL 0xFFFDFFFF
122
123 /* LSP types. */
124 enum lsp_types_t {
125 ZEBRA_LSP_NONE = 0, /* No LSP. */
126 ZEBRA_LSP_STATIC = 1, /* Static LSP. */
127 ZEBRA_LSP_LDP = 2, /* LDP LSP. */
128 ZEBRA_LSP_BGP = 3, /* BGP LSP. */
129 ZEBRA_LSP_OSPF_SR = 4,/* OSPF Segment Routing LSP. */
130 ZEBRA_LSP_ISIS_SR = 5,/* IS-IS Segment Routing LSP. */
131 ZEBRA_LSP_SHARP = 6, /* Identifier for test protocol */
132 ZEBRA_LSP_SRTE = 7, /* SR-TE LSP */
133 ZEBRA_LSP_EVPN = 8, /* EVPN VNI Label */
134 };
135
136 /* Functions for basic label operations. */
137
138 static inline void vni2label(vni_t vni, mpls_label_t *label)
139 {
140 uint8_t *tag = (uint8_t *)label;
141
142 assert(tag);
143
144 tag[0] = (vni >> 16) & 0xFF;
145 tag[1] = (vni >> 8) & 0xFF;
146 tag[2] = vni & 0xFF;
147 }
148
149 static inline vni_t label2vni(const mpls_label_t *label)
150 {
151 uint8_t *tag = (uint8_t *)label;
152 vni_t vni;
153
154 assert(tag);
155
156 vni = ((uint32_t)*tag++ << 16);
157 vni |= (uint32_t)*tag++ << 8;
158 vni |= (uint32_t)(*tag & 0xFF);
159
160 return vni;
161 }
162
163 /* Encode a label stack entry from fields; convert to network byte-order as
164 * the Netlink interface expects MPLS labels to be in this format.
165 */
166 static inline mpls_lse_t mpls_lse_encode(mpls_label_t label, uint32_t ttl,
167 uint32_t exp, uint32_t bos)
168 {
169 mpls_lse_t lse;
170 lse = htonl((label << MPLS_LS_LABEL_SHIFT) | (exp << MPLS_LS_EXP_SHIFT)
171 | (bos ? (1 << MPLS_LS_S_SHIFT) : 0)
172 | (ttl << MPLS_LS_TTL_SHIFT));
173 return lse;
174 }
175
176 /* Extract the fields from a label stack entry after converting to host-byte
177 * order. This is expected to be called only for messages received over the
178 * Netlink interface.
179 */
180 static inline void mpls_lse_decode(mpls_lse_t lse, mpls_label_t *label,
181 uint32_t *ttl, uint32_t *exp, uint32_t *bos)
182 {
183 mpls_lse_t local_lse;
184
185 local_lse = ntohl(lse);
186 *label = MPLS_LABEL_VALUE(local_lse);
187 *exp = MPLS_LABEL_EXP(local_lse);
188 *bos = MPLS_LABEL_BOS(local_lse);
189 *ttl = MPLS_LABEL_TTL(local_lse);
190 }
191
192 /* Invalid label index value (when used with BGP Prefix-SID). Should
193 * match the BGP definition.
194 */
195 #define MPLS_INVALID_LABEL_INDEX 0xFFFFFFFF
196
197 /* Printable string for labels (with consideration for reserved values). */
198 static inline char *label2str(mpls_label_t label, enum lsp_types_t type,
199 char *buf, size_t len)
200 {
201 if (type == ZEBRA_LSP_EVPN) {
202 snprintf(buf, len, "%u", label2vni(&label));
203 return (buf);
204 }
205
206 switch (label) {
207 case MPLS_LABEL_IPV4_EXPLICIT_NULL:
208 strlcpy(buf, "IPv4 Explicit Null", len);
209 return (buf);
210 case MPLS_LABEL_ROUTER_ALERT:
211 strlcpy(buf, "Router Alert", len);
212 return (buf);
213 case MPLS_LABEL_IPV6_EXPLICIT_NULL:
214 strlcpy(buf, "IPv6 Explicit Null", len);
215 return (buf);
216 case MPLS_LABEL_IMPLICIT_NULL:
217 strlcpy(buf, "implicit-null", len);
218 return (buf);
219 case MPLS_LABEL_ELI:
220 strlcpy(buf, "Entropy Label Indicator", len);
221 return (buf);
222 case MPLS_LABEL_GAL:
223 strlcpy(buf, "Generic Associated Channel", len);
224 return (buf);
225 case MPLS_LABEL_OAM_ALERT:
226 strlcpy(buf, "OAM Alert", len);
227 return (buf);
228 case MPLS_LABEL_EXTENSION:
229 strlcpy(buf, "Extension", len);
230 return (buf);
231 default:
232 if (label < 16)
233 snprintf(buf, len, "Reserved (%u)", label);
234 else
235 snprintf(buf, len, "%u", label);
236 return buf;
237 }
238 }
239
240 /*
241 * String to label conversion, labels separated by '/'.
242 */
243 int mpls_str2label(const char *label_str, uint8_t *num_labels,
244 mpls_label_t *labels);
245
246 /* Generic string buffer for label-stack-to-str */
247 #define MPLS_LABEL_STRLEN 1024
248
249 /*
250 * Label to string conversion, labels in string separated by '/'.
251 */
252 char *mpls_label2str(uint8_t num_labels, const mpls_label_t *labels, char *buf,
253 int len, enum lsp_types_t type, int pretty);
254
255 #ifdef __cplusplus
256 }
257 #endif
258
259 #endif