1 #ifndef included_vnet_mpls_packet_h
2 #define included_vnet_mpls_packet_h
7 * Copyright (c) 2012 Cisco and/or its affiliates.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * A label value only, i.e. 20bits.
24 typedef u32 mpls_label_t;
27 /* Label: top 20 bits [in network byte order] */
28 /* Experimental: 3 bits ... */
29 /* S (bottom of label stack): 1 bit */
31 mpls_label_t label_exp_s_ttl;
32 } mpls_unicast_header_t;
34 typedef enum mpls_eos_bit_t_
40 #define MPLS_EOS_BITS { \
41 [MPLS_NON_EOS] = "neos", \
46 * The Default TTL added to MPLS label headers when no other value is available
48 #define MPLS_LABEL_DEFAULT_TTL 64
51 * The Default EXP added to MPLS label headers when no other value is available
53 #define MPLS_LABEL_DEFAULT_EXP 0
56 * When in uniform mode convert an IPv[46] DSCP value to an MPLS EXP value
58 static inline u8 ip_dscp_to_mpls_exp (u8 tos)
64 * When in uniform mode convert an MPLS EXP value to an IPv[46] DSCP value
66 static inline u8 mpls_exp_to_ip_dscp (u8 exp)
71 #define FOR_EACH_MPLS_EOS_BIT(_eos) \
72 for (_eos = MPLS_NON_EOS; _eos <= MPLS_EOS; _eos++)
74 #define MPLS_ENTRY_LABEL_OFFSET 0
75 #define MPLS_ENTRY_LABEL_SHIFT 12
76 #define MPLS_ENTRY_LABEL_MASK 0x000fffff
77 #define MPLS_ENTRY_LABEL_BITS \
78 (MPLS_ENTRY_LABEL_MASK << MPLS_ENTRY_LABEL_SHIFT)
80 #define MPLS_ENTRY_EXP_OFFSET 2 /* byte offset to EXP bits */
81 #define MPLS_ENTRY_EXP_SHIFT 9
82 #define MPLS_ENTRY_EXP_MASK 0x07
83 #define MPLS_ENTRY_EXP(mpls) \
84 (((mpls)>>MPLS_ENTRY_EXP_SHIFT) & MPLS_ENTRY_EXP_MASK)
85 #define MPLS_ENTRY_EXP_BITS \
86 (MPLS_ENTRY_EXP_MASK << MPLS_ENTRY_EXP_SHIFT)
88 #define MPLS_ENTRY_EOS_OFFSET 2 /* byte offset to EOS bit */
89 #define MPLS_ENTRY_EOS_SHIFT 8
90 #define MPLS_ENTRY_EOS_MASK 0x01 /* EOS bit in its byte */
91 #define MPLS_ENTRY_EOS(mpls) \
92 (((mpls) >> MPLS_ENTRY_EOS_SHIFT) & MPLS_ENTRY_EOS_MASK)
93 #define MPLS_ENTRY_EOS_BIT (MPLS_ENTRY_EOS_MASK << MPLS_ENTRY_EOS_SHIFT)
95 #define MPLS_ENTRY_TTL_OFFSET 3 /* byte offset to ttl field */
96 #define MPLS_ENTRY_TTL_SHIFT 0
97 #define MPLS_ENTRY_TTL_MASK 0xff
98 #define MPLS_ENTRY_TTL(mpls) \
99 (((mpls) >> MPLS_ENTRY_TTL_SHIFT) & MPLS_ENTRY_TTL_MASK)
100 #define MPLS_ENTRY_TTL_BITS \
101 (MPLS_ENTRY_TTL_MASK << MPLS_ENTRY_TTL_SHIFT)
103 static inline u32 vnet_mpls_uc_get_label (mpls_label_t label_exp_s_ttl)
105 return (label_exp_s_ttl>>MPLS_ENTRY_LABEL_SHIFT);
108 static inline u32 vnet_mpls_uc_get_exp (mpls_label_t label_exp_s_ttl)
110 return (MPLS_ENTRY_EXP(label_exp_s_ttl));
113 static inline u32 vnet_mpls_uc_get_s (mpls_label_t label_exp_s_ttl)
115 return (MPLS_ENTRY_EOS(label_exp_s_ttl));
118 static inline u32 vnet_mpls_uc_get_ttl (mpls_label_t label_exp_s_ttl)
120 return (MPLS_ENTRY_TTL(label_exp_s_ttl));
123 static inline void vnet_mpls_uc_set_label (mpls_label_t *label_exp_s_ttl,
126 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_LABEL_BITS)) |
127 ((value & MPLS_ENTRY_LABEL_MASK) << MPLS_ENTRY_LABEL_SHIFT));
130 static inline void vnet_mpls_uc_set_exp (mpls_label_t *label_exp_s_ttl,
133 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EXP_BITS)) |
134 ((exp & MPLS_ENTRY_EXP_MASK) << MPLS_ENTRY_EXP_SHIFT));
137 static inline void vnet_mpls_uc_set_s (mpls_label_t *label_exp_s_ttl,
140 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EOS_BIT)) |
141 ((eos & MPLS_ENTRY_EOS_MASK) << MPLS_ENTRY_EOS_SHIFT));
144 static inline void vnet_mpls_uc_set_ttl (mpls_label_t *label_exp_s_ttl,
147 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_TTL_BITS)) |
148 ((ttl & MPLS_ENTRY_TTL_MASK)));
151 #endif /* included_vnet_mpls_packet_h */