blob: ca6ac407686a7b15a7a6d9e1792b5a118d841b58 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001#ifndef included_vnet_mpls_packet_h
2#define included_vnet_mpls_packet_h
3
4/*
5 * MPLS packet format
6 *
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:
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 */
20
21/**
22 * A label value only, i.e. 20bits.
23 */
24typedef u32 mpls_label_t;
25
26typedef struct {
27 /* Label: top 20 bits [in network byte order] */
28 /* Experimental: 3 bits ... */
29 /* S (bottom of label stack): 1 bit */
30 /* TTL: 8 bits */
31 mpls_label_t label_exp_s_ttl;
32} mpls_unicast_header_t;
33
34typedef enum mpls_eos_bit_t_
35{
36 MPLS_NON_EOS = 0,
37 MPLS_EOS = 1,
38} mpls_eos_bit_t;
39
40#define MPLS_EOS_BITS { \
41 [MPLS_NON_EOS] = "neos", \
42 [MPLS_EOS] = "eos", \
43}
44
Neale Ranns31ed7442018-02-23 05:29:09 -080045/**
46 * The Default TTL added to MPLS label headers when no other value is available
47 */
48#define MPLS_LABEL_DEFAULT_TTL 64
49
50/**
51 * The Default EXP added to MPLS label headers when no other value is available
52 */
53#define MPLS_LABEL_DEFAULT_EXP 0
54
55/**
56 * When in uniform mode convert an IPv[46] DSCP value to an MPLS EXP value
57 */
58static inline u8 ip_dscp_to_mpls_exp (u8 tos)
59{
60 return (tos >> 5);
61}
62
63/**
64 * When in uniform mode convert an MPLS EXP value to an IPv[46] DSCP value
65 */
66static inline u8 mpls_exp_to_ip_dscp (u8 exp)
67{
68 return (exp << 5);
69}
70
Neale Ranns0bfe5d82016-08-25 15:29:12 +010071#define FOR_EACH_MPLS_EOS_BIT(_eos) \
72 for (_eos = MPLS_NON_EOS; _eos <= MPLS_EOS; _eos++)
73
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)
79
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)
87
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)
94
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)
102
103static inline u32 vnet_mpls_uc_get_label (mpls_label_t label_exp_s_ttl)
104{
105 return (label_exp_s_ttl>>MPLS_ENTRY_LABEL_SHIFT);
106}
107
108static inline u32 vnet_mpls_uc_get_exp (mpls_label_t label_exp_s_ttl)
109{
110 return (MPLS_ENTRY_EXP(label_exp_s_ttl));
111}
112
113static inline u32 vnet_mpls_uc_get_s (mpls_label_t label_exp_s_ttl)
114{
115 return (MPLS_ENTRY_EOS(label_exp_s_ttl));
116}
117
118static inline u32 vnet_mpls_uc_get_ttl (mpls_label_t label_exp_s_ttl)
119{
120 return (MPLS_ENTRY_TTL(label_exp_s_ttl));
121}
122
123static inline void vnet_mpls_uc_set_label (mpls_label_t *label_exp_s_ttl,
124 u32 value)
125{
126 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_LABEL_BITS)) |
127 ((value & MPLS_ENTRY_LABEL_MASK) << MPLS_ENTRY_LABEL_SHIFT));
128}
129
130static inline void vnet_mpls_uc_set_exp (mpls_label_t *label_exp_s_ttl,
131 u32 exp)
132{
133 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EXP_BITS)) |
134 ((exp & MPLS_ENTRY_EXP_MASK) << MPLS_ENTRY_EXP_SHIFT));
135}
136
137static inline void vnet_mpls_uc_set_s (mpls_label_t *label_exp_s_ttl,
138 u32 eos)
139{
140 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EOS_BIT)) |
141 ((eos & MPLS_ENTRY_EOS_MASK) << MPLS_ENTRY_EOS_SHIFT));
142}
143
144static inline void vnet_mpls_uc_set_ttl (mpls_label_t *label_exp_s_ttl,
145 u32 ttl)
146{
147 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_TTL_BITS)) |
148 ((ttl & MPLS_ENTRY_TTL_MASK)));
149}
150
151#endif /* included_vnet_mpls_packet_h */