]> git.proxmox.com Git - mirror_frr.git/blob - lib/mpls.h
lib, zebra: Move help string to appropriate header
[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 <arpa/inet.h>
26
27 #ifdef MPLS_LABEL_MAX
28 #undef MPLS_LABEL_MAX
29 #endif
30
31 #define MPLS_LABEL_HELPSTR \
32 "Specify label(s) for this route\nOne or more " \
33 "labels in the range (16-1048575) separated by '/'\n"
34
35 /* Well-known MPLS label values (RFC 3032 etc). */
36 #define MPLS_LABEL_IPV4_EXPLICIT_NULL 0 /* [RFC3032] */
37 #define MPLS_LABEL_ROUTER_ALERT 1 /* [RFC3032] */
38 #define MPLS_LABEL_IPV6_EXPLICIT_NULL 2 /* [RFC3032] */
39 #define MPLS_LABEL_IMPLICIT_NULL 3 /* [RFC3032] */
40 #define MPLS_LABEL_ELI 7 /* [RFC6790] */
41 #define MPLS_LABEL_GAL 13 /* [RFC5586] */
42 #define MPLS_LABEL_OAM_ALERT 14 /* [RFC3429] */
43 #define MPLS_LABEL_EXTENSION 15 /* [RFC7274] */
44 #define MPLS_LABEL_MAX 1048575
45 #define MPLS_LABEL_NONE 0xFFFFFFFF /* for internal use only */
46
47 /* Minimum and maximum label values */
48 #define MPLS_LABEL_RESERVED_MIN 0
49 #define MPLS_LABEL_RESERVED_MAX 15
50 #define MPLS_LABEL_UNRESERVED_MIN 16
51 #define MPLS_LABEL_UNRESERVED_MAX 1048575
52
53 /* Default min and max SRGB label range */
54 /* Even if the SRGB allows to manage different Label space between routers,
55 * if an operator want to use the same SRGB for all its router, we must fix
56 * a common range. However, Cisco start its SRGB at 16000 and Juniper ends
57 * its SRGB at 16384 for OSPF. Thus, by fixing the minimum SRGB label to
58 * 8000 we could deal with both Cisco and Juniper.
59 */
60 #define MPLS_DEFAULT_MIN_SRGB_LABEL 8000
61 #define MPLS_DEFAULT_MAX_SRGB_LABEL 50000
62 #define MPLS_DEFAULT_MIN_SRGB_SIZE 5000
63 #define MPLS_DEFAULT_MAX_SRGB_SIZE 20000
64
65 /* Maximum # labels that can be pushed. */
66 #define MPLS_MAX_LABELS 16
67
68 #define IS_MPLS_RESERVED_LABEL(label) \
69 (label >= MPLS_LABEL_RESERVED_MIN && label <= MPLS_LABEL_RESERVED_MAX)
70
71 #define IS_MPLS_UNRESERVED_LABEL(label) \
72 (label >= MPLS_LABEL_UNRESERVED_MIN \
73 && label <= MPLS_LABEL_UNRESERVED_MAX)
74
75 /* Definitions for a MPLS label stack entry (RFC 3032). This encodes the
76 * label, EXP, BOS and TTL fields.
77 */
78 typedef unsigned int mpls_lse_t;
79
80 #define MPLS_LS_LABEL_MASK 0xFFFFF000
81 #define MPLS_LS_LABEL_SHIFT 12
82 #define MPLS_LS_EXP_MASK 0x00000E00
83 #define MPLS_LS_EXP_SHIFT 9
84 #define MPLS_LS_S_MASK 0x00000100
85 #define MPLS_LS_S_SHIFT 8
86 #define MPLS_LS_TTL_MASK 0x000000FF
87 #define MPLS_LS_TTL_SHIFT 0
88
89 #define MPLS_LABEL_VALUE(lse) \
90 ((lse & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT)
91 #define MPLS_LABEL_EXP(lse) ((lse & MPLS_LS_EXP_MASK) >> MPLS_LS_EXP_SHIFT)
92 #define MPLS_LABEL_BOS(lse) ((lse & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT)
93 #define MPLS_LABEL_TTL(lse) ((lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT)
94
95 #define IS_MPLS_LABEL_BOS(ls) (MPLS_LABEL_BOS(ls) == 1)
96
97 #define MPLS_LABEL_LEN_BITS 20
98
99 /* MPLS label value as a 32-bit (mostly we only care about the label value). */
100 typedef unsigned int mpls_label_t;
101
102 struct mpls_label_stack {
103 uint8_t num_labels;
104 uint8_t reserved[3];
105 mpls_label_t label[0]; /* 1 or more labels */
106 };
107
108 /* The MPLS explicit-null label is 0 which means when you memset a mpls_label_t
109 * to zero you have set that variable to explicit-null which was probably not
110 * your intent. The work-around is to use one bit to indicate if the
111 * mpls_label_t has been set by the user. MPLS_INVALID_LABEL has this bit clear
112 * so that we can use MPLS_INVALID_LABEL to initialize mpls_label_t variables.
113 */
114 #define MPLS_INVALID_LABEL 0xFFFDFFFF
115
116 /* LSP types. */
117 enum lsp_types_t {
118 ZEBRA_LSP_NONE = 0, /* No LSP. */
119 ZEBRA_LSP_STATIC = 1, /* Static LSP. */
120 ZEBRA_LSP_LDP = 2, /* LDP LSP. */
121 ZEBRA_LSP_BGP = 3, /* BGP LSP. */
122 ZEBRA_LSP_SR = 4, /* Segment Routing LSP. */
123 ZEBRA_LSP_SHARP = 5, /* Identifier for test protocol */
124 };
125
126 /* Functions for basic label operations. */
127
128 /* Encode a label stack entry from fields; convert to network byte-order as
129 * the Netlink interface expects MPLS labels to be in this format.
130 */
131 static inline mpls_lse_t mpls_lse_encode(mpls_label_t label, uint32_t ttl,
132 uint32_t exp, uint32_t bos)
133 {
134 mpls_lse_t lse;
135 lse = htonl((label << MPLS_LS_LABEL_SHIFT) | (exp << MPLS_LS_EXP_SHIFT)
136 | (bos ? (1 << MPLS_LS_S_SHIFT) : 0)
137 | (ttl << MPLS_LS_TTL_SHIFT));
138 return lse;
139 }
140
141 /* Extract the fields from a label stack entry after converting to host-byte
142 * order. This is expected to be called only for messages received over the
143 * Netlink interface.
144 */
145 static inline void mpls_lse_decode(mpls_lse_t lse, mpls_label_t *label,
146 uint32_t *ttl, uint32_t *exp, uint32_t *bos)
147 {
148 mpls_lse_t local_lse;
149
150 local_lse = ntohl(lse);
151 *label = MPLS_LABEL_VALUE(local_lse);
152 *exp = MPLS_LABEL_EXP(local_lse);
153 *bos = MPLS_LABEL_BOS(local_lse);
154 *ttl = MPLS_LABEL_TTL(local_lse);
155 }
156
157 /* Invalid label index value (when used with BGP Prefix-SID). Should
158 * match the BGP definition.
159 */
160 #define MPLS_INVALID_LABEL_INDEX 0xFFFFFFFF
161
162 /* Printable string for labels (with consideration for reserved values). */
163 static inline char *label2str(mpls_label_t label, char *buf, size_t len)
164 {
165 switch (label) {
166 case MPLS_LABEL_IPV4_EXPLICIT_NULL:
167 strlcpy(buf, "IPv4 Explicit Null", len);
168 return (buf);
169 case MPLS_LABEL_ROUTER_ALERT:
170 strlcpy(buf, "Router Alert", len);
171 return (buf);
172 case MPLS_LABEL_IPV6_EXPLICIT_NULL:
173 strlcpy(buf, "IPv6 Explict Null", len);
174 return (buf);
175 case MPLS_LABEL_IMPLICIT_NULL:
176 strlcpy(buf, "implicit-null", len);
177 return (buf);
178 case MPLS_LABEL_ELI:
179 strlcpy(buf, "Entropy Label Indicator", len);
180 return (buf);
181 case MPLS_LABEL_GAL:
182 strlcpy(buf, "Generic Associated Channel", len);
183 return (buf);
184 case MPLS_LABEL_OAM_ALERT:
185 strlcpy(buf, "OAM Alert", len);
186 return (buf);
187 case MPLS_LABEL_EXTENSION:
188 strlcpy(buf, "Extension", len);
189 return (buf);
190 default:
191 if (label < 16)
192 snprintf(buf, len, "Reserved (%u)", label);
193 else
194 snprintf(buf, len, "%u", label);
195 return (buf);
196 }
197 }
198
199
200 #endif