4437dc82e245b70bd0fa456c46effed7787e882d
[vpp.git] / src / vnet / bier / bier_hdr_inlines.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @brief The BIER inline functions acting on the bier header
17  */
18
19 #ifndef __BIER_HDR_INLINES_H__
20 #define __BIER_HDR_INLINES_H__
21
22 #include <vppinfra/byte_order.h>
23 #include <vppinfra/string.h>
24
25 #include <vnet/bier/bier_types.h>
26 #include <vnet/bier/bier_bit_string.h>
27 #include <vnet/ip/ip6_packet.h>
28
29 /**
30  * Mask and shift values for the fields incorporated
31  * into the header's first word
32  */
33 #define BIER_HDR_1ST_NIBBLE_MASK    0xf0000000
34 #define BIER_HDR_VERSION_FIELD_MASK 0x0f000000
35 #define BIER_HDR_LEN_FIELD_MASK     0x00f00000
36 #define BIER_HDR_ENTROPY_FIELD_MASK 0x000fffff
37
38 #define BIER_HDR_1ST_NIBBLE_SHIFT    28
39 #define BIER_HDR_VERSION_FIELD_SHIFT 24
40 #define BIER_HDR_LEN_FIELD_SHIFT     20
41 #define BIER_HDR_ENTROPY_FIELD_SHIFT  0
42
43 #define BIER_HDR_1ST_NIBBLE_VALUE 0x5
44
45 /**
46  * Mask and shift values for fields in the headers trainling word
47  */
48 #define BIER_HDR_PROTO_FIELD_MASK   0x003f
49 #define BIER_HDR_OAM_FIELD_MASK     0xc000
50 #define BIER_HDR_DSCP_FIELD_MASK    0x0fc0
51 #define BIER_HDR_DSCP_FIELD_SHIFT   6
52 #define BIER_HDR_PROTO_FIELD_SHIFT  0
53 #define BIER_HDR_OAM_FIELD_SHIFT    14
54
55 static inline bier_hdr_version_t
56 bier_hdr_get_version (const bier_hdr_t *bier_hdr)
57 {
58     return ((bier_hdr->bh_first_word &
59              BIER_HDR_VERSION_FIELD_MASK) >>
60             BIER_HDR_VERSION_FIELD_SHIFT);
61 }
62
63 static inline bier_hdr_len_id_t
64 bier_hdr_get_len_id (const bier_hdr_t *bier_hdr)
65 {
66     return ((bier_hdr->bh_first_word &
67              BIER_HDR_LEN_FIELD_MASK) >>
68             BIER_HDR_LEN_FIELD_SHIFT);
69 }
70
71 static inline bier_hdr_entropy_t
72 bier_hdr_get_entropy (const bier_hdr_t *bier_hdr)
73 {
74     return ((bier_hdr->bh_first_word &
75              BIER_HDR_ENTROPY_FIELD_MASK) >>
76             BIER_HDR_ENTROPY_FIELD_SHIFT);
77 }
78
79 static inline void
80 bier_hdr_1st_nibble (bier_hdr_t *hdr)
81 {
82     hdr->bh_first_word &= ~(BIER_HDR_1ST_NIBBLE_MASK);
83     hdr->bh_first_word |= (BIER_HDR_1ST_NIBBLE_VALUE <<
84                            BIER_HDR_1ST_NIBBLE_SHIFT);
85 }
86
87 static inline u8
88 bier_hdr_get_1st_nibble (bier_hdr_t *hdr)
89 {
90     return ((hdr->bh_first_word & BIER_HDR_1ST_NIBBLE_MASK) >>
91             BIER_HDR_1ST_NIBBLE_SHIFT);
92 }
93
94 static inline void
95 bier_hdr_set_version (bier_hdr_t *hdr,
96                       bier_hdr_version_t version)
97 {
98     hdr->bh_first_word &= ~(BIER_HDR_VERSION_FIELD_MASK);
99     hdr->bh_first_word |= (version << BIER_HDR_VERSION_FIELD_SHIFT);
100 }
101
102 static inline void
103 bier_hdr_set_len_id (bier_hdr_t *hdr,
104                      bier_hdr_len_id_t len)
105 {
106     hdr->bh_first_word &= ~(BIER_HDR_LEN_FIELD_MASK);
107     hdr->bh_first_word |= (len << BIER_HDR_LEN_FIELD_SHIFT);
108 }
109
110 static inline void
111 bier_hdr_set_entropy (bier_hdr_t *hdr,
112                       bier_hdr_entropy_t entropy)
113 {
114     entropy = entropy & BIER_HDR_ENTROPY_FIELD_MASK;
115     hdr->bh_first_word &= ~(BIER_HDR_ENTROPY_FIELD_MASK);
116     hdr->bh_first_word |= (entropy << BIER_HDR_ENTROPY_FIELD_SHIFT);
117 }
118
119 #define BIER_HDR_FIRST_WORD(version, len, entropy)          \
120     ((BIER_HDR_1ST_NIBBLE_VALUE <<                          \
121       BIER_HDR_1ST_NIBBLE_SHIFT) |                          \
122      (version << BIER_HDR_VERSION_FIELD_SHIFT) |            \
123      (len     << BIER_HDR_LEN_FIELD_SHIFT)     |            \
124      ((entropy & BIER_HDR_ENTROPY_FIELD_MASK)               \
125       << BIER_HDR_ENTROPY_FIELD_SHIFT))
126
127 static inline void
128 bier_hdr_ntoh (bier_hdr_t *bier_hdr)
129 {
130     bier_hdr->bh_first_word = clib_net_to_host_u32(bier_hdr->bh_first_word);
131     bier_hdr->bh_oam_dscp_proto = clib_net_to_host_u16(bier_hdr->bh_oam_dscp_proto);
132     bier_hdr->bh_bfr_id = clib_net_to_host_u16(bier_hdr->bh_bfr_id);
133 }
134
135 static inline void
136 bier_hdr_hton (bier_hdr_t *bier_hdr)
137 {
138     bier_hdr->bh_first_word = clib_host_to_net_u32(bier_hdr->bh_first_word);
139     bier_hdr->bh_oam_dscp_proto = clib_host_to_net_u16(bier_hdr->bh_oam_dscp_proto);
140     bier_hdr->bh_bfr_id = clib_host_to_net_u16(bier_hdr->bh_bfr_id);
141 }
142
143 static inline bier_hdr_src_id_t
144 bier_hdr_get_src_id (const bier_hdr_t *bier_hdr)
145 {
146     return (bier_hdr->bh_bfr_id);
147 }
148
149 static inline void
150 bier_hdr_set_src_id (bier_hdr_t *bier_hdr,
151                      bier_hdr_src_id_t src_id)
152 {
153     bier_hdr->bh_bfr_id = src_id;
154 }
155 static inline void
156 bier_hdr_set_proto_id (bier_hdr_t *bier_hdr,
157                        bier_hdr_proto_id_t proto)
158 {
159     bier_hdr->bh_oam_dscp_proto &= ~(BIER_HDR_PROTO_FIELD_MASK);
160     bier_hdr->bh_oam_dscp_proto |= (proto << BIER_HDR_PROTO_FIELD_SHIFT);
161 }
162
163 static inline bier_hdr_proto_id_t
164 bier_hdr_get_proto_id (const bier_hdr_t *bier_hdr)
165 {
166     return ((bier_hdr->bh_oam_dscp_proto & BIER_HDR_PROTO_FIELD_MASK) >>
167             BIER_HDR_PROTO_FIELD_SHIFT);
168 }
169
170 static inline void
171 bier_hdr_clear (bier_hdr_t *bier_hdr)
172 {
173     memset(&bier_hdr->bh_bit_string, 0,
174            bier_hdr_len_id_to_num_buckets(
175                bier_hdr_get_len_id(bier_hdr)));
176 }
177
178 static inline void
179 bier_hdr_init (bier_hdr_t *bier_hdr,
180                bier_hdr_version_t version,
181                bier_hdr_proto_id_t proto,
182                bier_hdr_len_id_t len,
183                bier_hdr_entropy_t entropy,
184                bier_bp_t src)
185 {
186     bier_hdr_1st_nibble(bier_hdr);
187     bier_hdr_set_version(bier_hdr, version);
188     bier_hdr_set_len_id(bier_hdr, len);
189     bier_hdr_set_entropy(bier_hdr, entropy);
190     bier_hdr_set_proto_id(bier_hdr, proto);
191     bier_hdr_set_src_id(bier_hdr, src);
192     bier_hdr_clear(bier_hdr);
193 }
194
195 static inline size_t
196 bier_hdr_str_num_bytes (const bier_hdr_t *bier_hdr)
197 {
198     return (bier_hdr_len_id_to_num_bytes(
199                 bier_hdr_get_len_id(bier_hdr)));
200 }
201
202 static inline size_t
203 bier_hdr_num_bytes (const bier_hdr_t *bier_hdr)
204 {
205     return (sizeof(bier_hdr_t) +
206             bier_hdr_str_num_bytes(bier_hdr));
207 }
208
209 static inline void
210 bier_bit_string_init_from_hdr (bier_hdr_t *bier_hdr,
211                                bier_bit_string_t *bit_string)
212 {
213     bit_string->bbs_len = bier_hdr_str_num_bytes(bier_hdr);
214     bit_string->bbs_buckets = bier_hdr->bh_bit_string;
215 }
216
217 #endif