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