blob: bc67445be8987aa004ee4c4028a88b4f868c3c54 [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
45#define FOR_EACH_MPLS_EOS_BIT(_eos) \
46 for (_eos = MPLS_NON_EOS; _eos <= MPLS_EOS; _eos++)
47
48#define MPLS_ENTRY_LABEL_OFFSET 0
49#define MPLS_ENTRY_LABEL_SHIFT 12
50#define MPLS_ENTRY_LABEL_MASK 0x000fffff
51#define MPLS_ENTRY_LABEL_BITS \
52 (MPLS_ENTRY_LABEL_MASK << MPLS_ENTRY_LABEL_SHIFT)
53
54#define MPLS_ENTRY_EXP_OFFSET 2 /* byte offset to EXP bits */
55#define MPLS_ENTRY_EXP_SHIFT 9
56#define MPLS_ENTRY_EXP_MASK 0x07
57#define MPLS_ENTRY_EXP(mpls) \
58 (((mpls)>>MPLS_ENTRY_EXP_SHIFT) & MPLS_ENTRY_EXP_MASK)
59#define MPLS_ENTRY_EXP_BITS \
60 (MPLS_ENTRY_EXP_MASK << MPLS_ENTRY_EXP_SHIFT)
61
62#define MPLS_ENTRY_EOS_OFFSET 2 /* byte offset to EOS bit */
63#define MPLS_ENTRY_EOS_SHIFT 8
64#define MPLS_ENTRY_EOS_MASK 0x01 /* EOS bit in its byte */
65#define MPLS_ENTRY_EOS(mpls) \
66 (((mpls) >> MPLS_ENTRY_EOS_SHIFT) & MPLS_ENTRY_EOS_MASK)
67#define MPLS_ENTRY_EOS_BIT (MPLS_ENTRY_EOS_MASK << MPLS_ENTRY_EOS_SHIFT)
68
69#define MPLS_ENTRY_TTL_OFFSET 3 /* byte offset to ttl field */
70#define MPLS_ENTRY_TTL_SHIFT 0
71#define MPLS_ENTRY_TTL_MASK 0xff
72#define MPLS_ENTRY_TTL(mpls) \
73 (((mpls) >> MPLS_ENTRY_TTL_SHIFT) & MPLS_ENTRY_TTL_MASK)
74#define MPLS_ENTRY_TTL_BITS \
75 (MPLS_ENTRY_TTL_MASK << MPLS_ENTRY_TTL_SHIFT)
76
77static inline u32 vnet_mpls_uc_get_label (mpls_label_t label_exp_s_ttl)
78{
79 return (label_exp_s_ttl>>MPLS_ENTRY_LABEL_SHIFT);
80}
81
82static inline u32 vnet_mpls_uc_get_exp (mpls_label_t label_exp_s_ttl)
83{
84 return (MPLS_ENTRY_EXP(label_exp_s_ttl));
85}
86
87static inline u32 vnet_mpls_uc_get_s (mpls_label_t label_exp_s_ttl)
88{
89 return (MPLS_ENTRY_EOS(label_exp_s_ttl));
90}
91
92static inline u32 vnet_mpls_uc_get_ttl (mpls_label_t label_exp_s_ttl)
93{
94 return (MPLS_ENTRY_TTL(label_exp_s_ttl));
95}
96
97static inline void vnet_mpls_uc_set_label (mpls_label_t *label_exp_s_ttl,
98 u32 value)
99{
100 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_LABEL_BITS)) |
101 ((value & MPLS_ENTRY_LABEL_MASK) << MPLS_ENTRY_LABEL_SHIFT));
102}
103
104static inline void vnet_mpls_uc_set_exp (mpls_label_t *label_exp_s_ttl,
105 u32 exp)
106{
107 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EXP_BITS)) |
108 ((exp & MPLS_ENTRY_EXP_MASK) << MPLS_ENTRY_EXP_SHIFT));
109}
110
111static inline void vnet_mpls_uc_set_s (mpls_label_t *label_exp_s_ttl,
112 u32 eos)
113{
114 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EOS_BIT)) |
115 ((eos & MPLS_ENTRY_EOS_MASK) << MPLS_ENTRY_EOS_SHIFT));
116}
117
118static inline void vnet_mpls_uc_set_ttl (mpls_label_t *label_exp_s_ttl,
119 u32 ttl)
120{
121 *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_TTL_BITS)) |
122 ((ttl & MPLS_ENTRY_TTL_MASK)));
123}
124
125#endif /* included_vnet_mpls_packet_h */