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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * @brief The BIER inline functions acting on the bier header
19 #ifndef __BIER_HDR_INLINES_H__
20 #define __BIER_HDR_INLINES_H__
22 #include <vppinfra/byte_order.h>
23 #include <vppinfra/string.h>
25 #include <vnet/bier/bier_types.h>
26 #include <vnet/bier/bier_bit_string.h>
27 #include <vnet/ip/ip6_packet.h>
30 * Special Value of the BIER RX interface
32 #define BIER_RX_ITF (~0 - 1)
35 * Mask and shift values for the fields incorporated
36 * into the header's first word
38 #define BIER_HDR_1ST_NIBBLE_MASK 0xf0000000
39 #define BIER_HDR_VERSION_FIELD_MASK 0x0f000000
40 #define BIER_HDR_LEN_FIELD_MASK 0x00f00000
41 #define BIER_HDR_ENTROPY_FIELD_MASK 0x000fffff
43 #define BIER_HDR_1ST_NIBBLE_SHIFT 28
44 #define BIER_HDR_VERSION_FIELD_SHIFT 24
45 #define BIER_HDR_LEN_FIELD_SHIFT 20
46 #define BIER_HDR_ENTROPY_FIELD_SHIFT 0
48 #define BIER_HDR_1ST_NIBBLE_VALUE 0x5
51 * Mask and shift values for fields in the headers training word
53 #define BIER_HDR_PROTO_FIELD_MASK 0x003f
54 #define BIER_HDR_OAM_FIELD_MASK 0xc000
55 #define BIER_HDR_DSCP_FIELD_MASK 0x0fc0
56 #define BIER_HDR_DSCP_FIELD_SHIFT 6
57 #define BIER_HDR_PROTO_FIELD_SHIFT 0
58 #define BIER_HDR_OAM_FIELD_SHIFT 14
60 static inline bier_hdr_version_t
61 bier_hdr_get_version (const bier_hdr_t *bier_hdr)
63 return ((bier_hdr->bh_first_word &
64 BIER_HDR_VERSION_FIELD_MASK) >>
65 BIER_HDR_VERSION_FIELD_SHIFT);
68 static inline bier_hdr_len_id_t
69 bier_hdr_get_len_id (const bier_hdr_t *bier_hdr)
71 return ((bier_hdr->bh_first_word &
72 BIER_HDR_LEN_FIELD_MASK) >>
73 BIER_HDR_LEN_FIELD_SHIFT);
76 static inline bier_hdr_entropy_t
77 bier_hdr_get_entropy (const bier_hdr_t *bier_hdr)
79 return ((bier_hdr->bh_first_word &
80 BIER_HDR_ENTROPY_FIELD_MASK) >>
81 BIER_HDR_ENTROPY_FIELD_SHIFT);
85 bier_hdr_1st_nibble (bier_hdr_t *hdr)
87 hdr->bh_first_word &= ~(BIER_HDR_1ST_NIBBLE_MASK);
88 hdr->bh_first_word |= (BIER_HDR_1ST_NIBBLE_VALUE <<
89 BIER_HDR_1ST_NIBBLE_SHIFT);
93 bier_hdr_get_1st_nibble (bier_hdr_t *hdr)
95 return ((hdr->bh_first_word & BIER_HDR_1ST_NIBBLE_MASK) >>
96 BIER_HDR_1ST_NIBBLE_SHIFT);
100 bier_hdr_set_version (bier_hdr_t *hdr,
101 bier_hdr_version_t version)
103 hdr->bh_first_word &= ~(BIER_HDR_VERSION_FIELD_MASK);
104 hdr->bh_first_word |= (version << BIER_HDR_VERSION_FIELD_SHIFT);
108 bier_hdr_set_len_id (bier_hdr_t *hdr,
109 bier_hdr_len_id_t len)
111 hdr->bh_first_word &= ~(BIER_HDR_LEN_FIELD_MASK);
112 hdr->bh_first_word |= (len << BIER_HDR_LEN_FIELD_SHIFT);
116 bier_hdr_set_entropy (bier_hdr_t *hdr,
117 bier_hdr_entropy_t entropy)
119 entropy = entropy & BIER_HDR_ENTROPY_FIELD_MASK;
120 hdr->bh_first_word &= ~(BIER_HDR_ENTROPY_FIELD_MASK);
121 hdr->bh_first_word |= (entropy << BIER_HDR_ENTROPY_FIELD_SHIFT);
124 #define BIER_HDR_FIRST_WORD(version, len, entropy) \
125 ((BIER_HDR_1ST_NIBBLE_VALUE << \
126 BIER_HDR_1ST_NIBBLE_SHIFT) | \
127 (version << BIER_HDR_VERSION_FIELD_SHIFT) | \
128 (len << BIER_HDR_LEN_FIELD_SHIFT) | \
129 ((entropy & BIER_HDR_ENTROPY_FIELD_MASK) \
130 << BIER_HDR_ENTROPY_FIELD_SHIFT))
133 bier_hdr_ntoh (bier_hdr_t *bier_hdr)
135 bier_hdr->bh_first_word = clib_net_to_host_u32(bier_hdr->bh_first_word);
136 bier_hdr->bh_oam_dscp_proto = clib_net_to_host_u16(bier_hdr->bh_oam_dscp_proto);
137 bier_hdr->bh_bfr_id = clib_net_to_host_u16(bier_hdr->bh_bfr_id);
141 bier_hdr_hton (bier_hdr_t *bier_hdr)
143 bier_hdr->bh_first_word = clib_host_to_net_u32(bier_hdr->bh_first_word);
144 bier_hdr->bh_oam_dscp_proto = clib_host_to_net_u16(bier_hdr->bh_oam_dscp_proto);
145 bier_hdr->bh_bfr_id = clib_host_to_net_u16(bier_hdr->bh_bfr_id);
148 static inline bier_hdr_src_id_t
149 bier_hdr_get_src_id (const bier_hdr_t *bier_hdr)
151 return (bier_hdr->bh_bfr_id);
155 bier_hdr_set_src_id (bier_hdr_t *bier_hdr,
156 bier_hdr_src_id_t src_id)
158 bier_hdr->bh_bfr_id = src_id;
161 bier_hdr_set_proto_id (bier_hdr_t *bier_hdr,
162 bier_hdr_proto_id_t proto)
164 bier_hdr->bh_oam_dscp_proto &= ~(BIER_HDR_PROTO_FIELD_MASK);
165 bier_hdr->bh_oam_dscp_proto |= (proto << BIER_HDR_PROTO_FIELD_SHIFT);
168 static inline bier_hdr_proto_id_t
169 bier_hdr_get_proto_id (const bier_hdr_t *bier_hdr)
171 return ((bier_hdr->bh_oam_dscp_proto & BIER_HDR_PROTO_FIELD_MASK) >>
172 BIER_HDR_PROTO_FIELD_SHIFT);
176 bier_hdr_clear (bier_hdr_t *bier_hdr)
178 clib_memset(&bier_hdr->bh_bit_string, 0,
179 bier_hdr_len_id_to_num_buckets(
180 bier_hdr_get_len_id(bier_hdr)));
184 bier_hdr_init (bier_hdr_t *bier_hdr,
185 bier_hdr_version_t version,
186 bier_hdr_proto_id_t proto,
187 bier_hdr_len_id_t len,
188 bier_hdr_entropy_t entropy,
191 bier_hdr_1st_nibble(bier_hdr);
192 bier_hdr_set_version(bier_hdr, version);
193 bier_hdr_set_len_id(bier_hdr, len);
194 bier_hdr_set_entropy(bier_hdr, entropy);
195 bier_hdr_set_proto_id(bier_hdr, proto);
196 bier_hdr_set_src_id(bier_hdr, src);
197 bier_hdr_clear(bier_hdr);
201 bier_hdr_str_num_bytes (const bier_hdr_t *bier_hdr)
203 return (bier_hdr_len_id_to_num_bytes(
204 bier_hdr_get_len_id(bier_hdr)));
208 bier_hdr_num_bytes (const bier_hdr_t *bier_hdr)
210 return (sizeof(bier_hdr_t) +
211 bier_hdr_str_num_bytes(bier_hdr));
215 bier_bit_string_init_from_hdr (bier_hdr_t *bier_hdr,
216 bier_bit_string_t *bit_string)
218 bit_string->bbs_len = bier_hdr_str_num_bytes(bier_hdr);
219 bit_string->bbs_buckets = bier_hdr->bh_bit_string;