Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 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 | /** |
Neale Ranns | fe4e48f | 2018-09-24 23:38:37 -0700 | [diff] [blame] | 30 | * Special Value of the BIER RX interface |
| 31 | */ |
| 32 | #define BIER_RX_ITF (~0 - 1) |
| 33 | |
| 34 | /** |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 35 | * Mask and shift values for the fields incorporated |
| 36 | * into the header's first word |
| 37 | */ |
| 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 |
| 42 | |
| 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 |
| 47 | |
| 48 | #define BIER_HDR_1ST_NIBBLE_VALUE 0x5 |
| 49 | |
| 50 | /** |
Paul Vinciguerra | e6eefb6 | 2019-05-13 15:56:41 -0400 | [diff] [blame] | 51 | * Mask and shift values for fields in the headers training word |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 52 | */ |
| 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 |
| 59 | |
| 60 | static inline bier_hdr_version_t |
| 61 | bier_hdr_get_version (const bier_hdr_t *bier_hdr) |
| 62 | { |
| 63 | return ((bier_hdr->bh_first_word & |
| 64 | BIER_HDR_VERSION_FIELD_MASK) >> |
| 65 | BIER_HDR_VERSION_FIELD_SHIFT); |
| 66 | } |
| 67 | |
| 68 | static inline bier_hdr_len_id_t |
| 69 | bier_hdr_get_len_id (const bier_hdr_t *bier_hdr) |
| 70 | { |
| 71 | return ((bier_hdr->bh_first_word & |
| 72 | BIER_HDR_LEN_FIELD_MASK) >> |
| 73 | BIER_HDR_LEN_FIELD_SHIFT); |
| 74 | } |
| 75 | |
| 76 | static inline bier_hdr_entropy_t |
| 77 | bier_hdr_get_entropy (const bier_hdr_t *bier_hdr) |
| 78 | { |
| 79 | return ((bier_hdr->bh_first_word & |
| 80 | BIER_HDR_ENTROPY_FIELD_MASK) >> |
| 81 | BIER_HDR_ENTROPY_FIELD_SHIFT); |
| 82 | } |
| 83 | |
| 84 | static inline void |
| 85 | bier_hdr_1st_nibble (bier_hdr_t *hdr) |
| 86 | { |
| 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); |
| 90 | } |
| 91 | |
| 92 | static inline u8 |
| 93 | bier_hdr_get_1st_nibble (bier_hdr_t *hdr) |
| 94 | { |
| 95 | return ((hdr->bh_first_word & BIER_HDR_1ST_NIBBLE_MASK) >> |
| 96 | BIER_HDR_1ST_NIBBLE_SHIFT); |
| 97 | } |
| 98 | |
| 99 | static inline void |
| 100 | bier_hdr_set_version (bier_hdr_t *hdr, |
| 101 | bier_hdr_version_t version) |
| 102 | { |
| 103 | hdr->bh_first_word &= ~(BIER_HDR_VERSION_FIELD_MASK); |
| 104 | hdr->bh_first_word |= (version << BIER_HDR_VERSION_FIELD_SHIFT); |
| 105 | } |
| 106 | |
| 107 | static inline void |
| 108 | bier_hdr_set_len_id (bier_hdr_t *hdr, |
| 109 | bier_hdr_len_id_t len) |
| 110 | { |
| 111 | hdr->bh_first_word &= ~(BIER_HDR_LEN_FIELD_MASK); |
| 112 | hdr->bh_first_word |= (len << BIER_HDR_LEN_FIELD_SHIFT); |
| 113 | } |
| 114 | |
| 115 | static inline void |
| 116 | bier_hdr_set_entropy (bier_hdr_t *hdr, |
| 117 | bier_hdr_entropy_t entropy) |
| 118 | { |
| 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); |
| 122 | } |
| 123 | |
| 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)) |
| 131 | |
| 132 | static inline void |
| 133 | bier_hdr_ntoh (bier_hdr_t *bier_hdr) |
| 134 | { |
| 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); |
| 138 | } |
| 139 | |
| 140 | static inline void |
| 141 | bier_hdr_hton (bier_hdr_t *bier_hdr) |
| 142 | { |
| 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); |
| 146 | } |
| 147 | |
| 148 | static inline bier_hdr_src_id_t |
| 149 | bier_hdr_get_src_id (const bier_hdr_t *bier_hdr) |
| 150 | { |
| 151 | return (bier_hdr->bh_bfr_id); |
| 152 | } |
| 153 | |
| 154 | static inline void |
| 155 | bier_hdr_set_src_id (bier_hdr_t *bier_hdr, |
| 156 | bier_hdr_src_id_t src_id) |
| 157 | { |
| 158 | bier_hdr->bh_bfr_id = src_id; |
| 159 | } |
| 160 | static inline void |
| 161 | bier_hdr_set_proto_id (bier_hdr_t *bier_hdr, |
| 162 | bier_hdr_proto_id_t proto) |
| 163 | { |
| 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); |
| 166 | } |
| 167 | |
| 168 | static inline bier_hdr_proto_id_t |
| 169 | bier_hdr_get_proto_id (const bier_hdr_t *bier_hdr) |
| 170 | { |
| 171 | return ((bier_hdr->bh_oam_dscp_proto & BIER_HDR_PROTO_FIELD_MASK) >> |
| 172 | BIER_HDR_PROTO_FIELD_SHIFT); |
| 173 | } |
| 174 | |
| 175 | static inline void |
| 176 | bier_hdr_clear (bier_hdr_t *bier_hdr) |
| 177 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 178 | clib_memset(&bier_hdr->bh_bit_string, 0, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 179 | bier_hdr_len_id_to_num_buckets( |
| 180 | bier_hdr_get_len_id(bier_hdr))); |
| 181 | } |
| 182 | |
| 183 | static inline void |
| 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, |
| 189 | bier_bp_t src) |
| 190 | { |
| 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); |
| 198 | } |
| 199 | |
| 200 | static inline size_t |
| 201 | bier_hdr_str_num_bytes (const bier_hdr_t *bier_hdr) |
| 202 | { |
| 203 | return (bier_hdr_len_id_to_num_bytes( |
| 204 | bier_hdr_get_len_id(bier_hdr))); |
| 205 | } |
| 206 | |
| 207 | static inline size_t |
| 208 | bier_hdr_num_bytes (const bier_hdr_t *bier_hdr) |
| 209 | { |
| 210 | return (sizeof(bier_hdr_t) + |
| 211 | bier_hdr_str_num_bytes(bier_hdr)); |
| 212 | } |
| 213 | |
| 214 | static inline void |
| 215 | bier_bit_string_init_from_hdr (bier_hdr_t *bier_hdr, |
| 216 | bier_bit_string_t *bit_string) |
| 217 | { |
| 218 | bit_string->bbs_len = bier_hdr_str_num_bytes(bier_hdr); |
| 219 | bit_string->bbs_buckets = bier_hdr->bh_bit_string; |
| 220 | } |
| 221 | |
| 222 | #endif |