4 * Copyright (c) 2012 Cisco and/or its affiliates.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef included_vnet_mpls_packet_h
19 #define included_vnet_mpls_packet_h
21 #include <vnet/ip/ip_packet.h>
24 * A label value only, i.e. 20bits.
26 typedef u32 mpls_label_t;
29 /* Label: top 20 bits [in network byte order] */
30 /* Experimental: 3 bits ... */
31 /* S (bottom of label stack): 1 bit */
33 mpls_label_t label_exp_s_ttl;
34 } mpls_unicast_header_t;
36 typedef enum mpls_eos_bit_t_
42 #define MPLS_EOS_BITS { \
43 [MPLS_NON_EOS] = "neos", \
48 * The Default TTL added to MPLS label headers when no other value is available
50 #define MPLS_LABEL_DEFAULT_TTL 64
53 * The Default EXP added to MPLS label headers when no other value is available
55 #define MPLS_LABEL_DEFAULT_EXP 0
58 * When in uniform mode convert an IPv[46] DSCP value to an MPLS EXP value
60 static inline u8 ip_dscp_to_mpls_exp (ip_dscp_t tos)
66 * When in uniform mode convert an MPLS EXP value to an IPv[46] DSCP value
68 static inline u8 mpls_exp_to_ip_dscp (u8 exp)
73 #define FOR_EACH_MPLS_EOS_BIT(_eos) \
74 for (_eos = MPLS_NON_EOS; _eos <= MPLS_EOS; _eos++)
76 #define MPLS_ENTRY_LABEL_OFFSET 0
77 #define MPLS_ENTRY_LABEL_SHIFT 12
78 #define MPLS_ENTRY_LABEL_MASK 0x000fffff
79 #define MPLS_ENTRY_LABEL_BITS \
80 (MPLS_ENTRY_LABEL_MASK << MPLS_ENTRY_LABEL_SHIFT)
82 #define MPLS_ENTRY_EXP_OFFSET 2 /* byte offset to EXP bits */
83 #define MPLS_ENTRY_EXP_SHIFT 9
84 #define MPLS_ENTRY_EXP_MASK 0x07
85 #define MPLS_ENTRY_EXP(mpls) \
86 (((mpls)>>MPLS_ENTRY_EXP_SHIFT) & MPLS_ENTRY_EXP_MASK)
87 #define MPLS_ENTRY_EXP_BITS \
88 (MPLS_ENTRY_EXP_MASK << MPLS_ENTRY_EXP_SHIFT)
90 #define MPLS_ENTRY_EOS_OFFSET 2 /* byte offset to EOS bit */
91 #define MPLS_ENTRY_EOS_SHIFT 8
92 #define MPLS_ENTRY_EOS_MASK 0x01 /* EOS bit in its byte */
93 #define MPLS_ENTRY_EOS(mpls) \
94 (((mpls) >> MPLS_ENTRY_EOS_SHIFT) & MPLS_ENTRY_EOS_MASK)
95 #define MPLS_ENTRY_EOS_BIT (MPLS_ENTRY_EOS_MASK << MPLS_ENTRY_EOS_SHIFT)
97 #define MPLS_ENTRY_TTL_OFFSET 3 /* byte offset to ttl field */
98 #define MPLS_ENTRY_TTL_SHIFT 0
99 #define MPLS_ENTRY_TTL_MASK 0xff
100 #define MPLS_ENTRY_TTL(mpls) \
101 (((mpls) >> MPLS_ENTRY_TTL_SHIFT) & MPLS_ENTRY_TTL_MASK)
102 #define MPLS_ENTRY_TTL_BITS \
103 (MPLS_ENTRY_TTL_MASK << MPLS_ENTRY_TTL_SHIFT)
105 static inline u32 vnet_mpls_uc_get_label (mpls_label_t label_exp_s_ttl)
107 return (label_exp_s_ttl>>MPLS_ENTRY_LABEL_SHIFT);
110 static inline u32 vnet_mpls_uc_get_exp (mpls_label_t label_exp_s_ttl)
112 return (MPLS_ENTRY_EXP(label_exp_s_ttl));
115 static inline u32 vnet_mpls_uc_get_s (mpls_label_t label_exp_s_ttl)
117 return (MPLS_ENTRY_EOS(label_exp_s_ttl));
120 static inline u32 vnet_mpls_uc_get_ttl (mpls_label_t label_exp_s_ttl)
122 return (MPLS_ENTRY_TTL(label_exp_s_ttl));
125 static inline void vnet_mpls_uc_set_label (mpls_label_t *label_exp_s_ttl,
128 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_LABEL_BITS)) |
129 ((value & MPLS_ENTRY_LABEL_MASK) << MPLS_ENTRY_LABEL_SHIFT));
132 static inline void vnet_mpls_uc_set_exp (mpls_label_t *label_exp_s_ttl,
135 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EXP_BITS)) |
136 ((exp & MPLS_ENTRY_EXP_MASK) << MPLS_ENTRY_EXP_SHIFT));
139 static inline void vnet_mpls_uc_set_s (mpls_label_t *label_exp_s_ttl,
142 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EOS_BIT)) |
143 ((eos & MPLS_ENTRY_EOS_MASK) << MPLS_ENTRY_EOS_SHIFT));
146 static inline void vnet_mpls_uc_set_ttl (mpls_label_t *label_exp_s_ttl,
149 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_TTL_BITS)) |
150 ((ttl & MPLS_ENTRY_TTL_MASK)));
153 #endif /* included_vnet_mpls_packet_h */