]> git.proxmox.com Git - mirror_frr.git/blame - lib/mpls.h
Merge branch 'frr/pull/236' ("tools: frr-reload.py needs to treat "mpls" as a single...
[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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
23#ifndef _QUAGGA_MPLS_H
24#define _QUAGGA_MPLS_H
25
26/* Well-known MPLS label values (RFC 3032 etc). */
27#define MPLS_V4_EXP_NULL_LABEL 0
28#define MPLS_RA_LABEL 1
29#define MPLS_V6_EXP_NULL_LABEL 2
30#define MPLS_IMP_NULL_LABEL 3
31#define MPLS_ENTROPY_LABEL_INDICATOR 7
32#define MPLS_GAL_LABEL 13
33#define MPLS_OAM_ALERT_LABEL 14
34#define MPLS_EXTENSION_LABEL 15
35
36/* Minimum and maximum label values */
37#define MPLS_MIN_RESERVED_LABEL 0
38#define MPLS_MAX_RESERVED_LABEL 15
39#define MPLS_MIN_UNRESERVED_LABEL 16
40#define MPLS_MAX_UNRESERVED_LABEL 1048575
41
42#define IS_MPLS_RESERVED_LABEL(label) \
43 (label >= MPLS_MIN_RESERVED_LABEL && label <= MPLS_MAX_RESERVED_LABEL)
44
45#define IS_MPLS_UNRESERVED_LABEL(label) \
46 (label >= MPLS_MIN_UNRESERVED_LABEL && label <= MPLS_MAX_UNRESERVED_LABEL)
47
48/* Definitions for a MPLS label stack entry (RFC 3032). This encodes the
49 * label, EXP, BOS and TTL fields.
50 */
51typedef unsigned int mpls_lse_t;
52
53#define MPLS_LS_LABEL_MASK 0xFFFFF000
54#define MPLS_LS_LABEL_SHIFT 12
55#define MPLS_LS_EXP_MASK 0x00000E00
56#define MPLS_LS_EXP_SHIFT 9
57#define MPLS_LS_S_MASK 0x00000100
58#define MPLS_LS_S_SHIFT 8
59#define MPLS_LS_TTL_MASK 0x000000FF
60#define MPLS_LS_TTL_SHIFT 0
61
62#define MPLS_LABEL_VALUE(lse) \
63 ((lse & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT)
64#define MPLS_LABEL_EXP(lse) \
65 ((lse & MPLS_LS_EXP_MASK) >> MPLS_LS_EXP_SHIFT)
66#define MPLS_LABEL_BOS(lse) \
67 ((lse & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT)
68#define MPLS_LABEL_TTL(lse) \
69 ((lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT)
70
71#define IS_MPLS_LABEL_BOS(ls) (MPLS_LABEL_BOS(ls) == 1)
72
73#define MPLS_LABEL_LEN_BITS 20
74
75/* MPLS label value as a 32-bit (mostly we only care about the label value). */
76typedef unsigned int mpls_label_t;
77
ce549947 78#define MPLS_NO_LABEL 0xFFFFFFFF
50f34207 79#define MPLS_INVALID_LABEL 0xFFFFFFFF
80
ce549947
RW
81/* LSP types. */
82enum lsp_types_t
83{
84 ZEBRA_LSP_NONE = 0, /* No LSP. */
85 ZEBRA_LSP_STATIC = 1, /* Static LSP. */
86 ZEBRA_LSP_LDP = 2 /* LDP LSP. */
87};
88
50f34207 89/* Functions for basic label operations. */
90
91/* Encode a label stack entry from fields; convert to network byte-order as
92 * the Netlink interface expects MPLS labels to be in this format.
93 */
94static inline mpls_lse_t
95mpls_lse_encode (mpls_label_t label, u_int32_t ttl,
96 u_int32_t exp, u_int32_t bos)
97{
98 mpls_lse_t lse;
99 lse = htonl ((label << MPLS_LS_LABEL_SHIFT) |
100 (exp << MPLS_LS_EXP_SHIFT) |
101 (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
102 (ttl << MPLS_LS_TTL_SHIFT));
103 return lse;
104}
105
106/* Extract the fields from a label stack entry after converting to host-byte
107 * order. This is expected to be called only for messages received over the
108 * Netlink interface.
109 */
110static inline void
111mpls_lse_decode (mpls_lse_t lse, mpls_label_t *label,
112 u_int32_t *ttl, u_int32_t *exp, u_int32_t *bos)
113{
114 mpls_lse_t local_lse;
115
116 local_lse = ntohl (lse);
117 *label = MPLS_LABEL_VALUE(local_lse);
118 *exp = MPLS_LABEL_EXP(local_lse);
119 *bos = MPLS_LABEL_BOS(local_lse);
120 *ttl = MPLS_LABEL_TTL(local_lse);
121}
122
123
124/* Printable string for labels (with consideration for reserved values). */
125static inline char *
14878121 126label2str (mpls_label_t label, char *buf, size_t len)
50f34207 127{
128 switch(label) {
129 case MPLS_V4_EXP_NULL_LABEL:
14878121 130 strlcpy(buf, "IPv4 Explicit Null", len);
50f34207 131 return(buf);
50f34207 132 case MPLS_RA_LABEL:
14878121 133 strlcpy(buf, "Router Alert", len);
50f34207 134 return(buf);
50f34207 135 case MPLS_V6_EXP_NULL_LABEL:
14878121 136 strlcpy(buf, "IPv6 Explict Null", len);
50f34207 137 return(buf);
50f34207 138 case MPLS_IMP_NULL_LABEL:
14878121 139 strlcpy(buf, "implicit-null", len);
50f34207 140 return(buf);
50f34207 141 case MPLS_ENTROPY_LABEL_INDICATOR:
14878121 142 strlcpy(buf, "Entropy Label Indicator", len);
50f34207 143 return(buf);
50f34207 144 case MPLS_GAL_LABEL:
14878121 145 strlcpy(buf, "Generic Associated Channel", len);
50f34207 146 return(buf);
50f34207 147 case MPLS_OAM_ALERT_LABEL:
14878121 148 strlcpy(buf, "OAM Alert", len);
50f34207 149 return(buf);
50f34207 150 case MPLS_EXTENSION_LABEL:
14878121 151 strlcpy(buf, "Extension", len);
50f34207 152 return(buf);
50f34207 153 default:
14878121
DL
154 if (label < 16)
155 snprintf(buf, len, "Reserved (%u)", label);
156 else
157 snprintf(buf, len, "%u", label);
50f34207 158 return(buf);
159 }
50f34207 160}
161
eac6e3f0
RW
162/* constants used by ldpd */
163#define MPLS_LABEL_IPV4NULL 0 /* IPv4 Explicit NULL Label */
164#define MPLS_LABEL_RTALERT 1 /* Router Alert Label */
165#define MPLS_LABEL_IPV6NULL 2 /* IPv6 Explicit NULL Label */
166#define MPLS_LABEL_IMPLNULL 3 /* Implicit NULL Label */
167/* MPLS_LABEL_RESERVED 4-15 */ /* Values 4-15 are reserved */
168#define MPLS_LABEL_RESERVED_MAX 15
169#define MPLS_LABEL_MAX ((1 << 20) - 1)
170
50f34207 171#endif