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 | #include <vnet/bier/bier_imp.h> |
| 17 | #include <vnet/bier/bier_hdr_inlines.h> |
| 18 | #include <vnet/ip/ip.h> |
| 19 | |
| 20 | /** |
| 21 | * @brief A struct to hold tracing information for the BIER imposition |
| 22 | * node. |
| 23 | */ |
| 24 | typedef struct bier_imp_trace_t_ |
| 25 | { |
| 26 | /** |
| 27 | * BIER imposition object hit |
| 28 | */ |
| 29 | index_t imp; |
| 30 | |
| 31 | /** |
| 32 | * BIER hdr applied |
| 33 | */ |
| 34 | bier_hdr_t hdr; |
| 35 | } bier_imp_trace_t; |
| 36 | |
| 37 | always_inline uword |
| 38 | bier_imp_dpo_inline (vlib_main_t * vm, |
| 39 | vlib_node_runtime_t * node, |
| 40 | vlib_frame_t * from_frame, |
| 41 | fib_protocol_t fproto, |
| 42 | bier_hdr_proto_id_t bproto) |
| 43 | { |
| 44 | u32 n_left_from, next_index, * from, * to_next; |
| 45 | |
| 46 | from = vlib_frame_vector_args (from_frame); |
| 47 | n_left_from = from_frame->n_vectors; |
| 48 | |
| 49 | next_index = node->cached_next_index; |
| 50 | |
| 51 | while (n_left_from > 0) |
| 52 | { |
| 53 | u32 n_left_to_next; |
| 54 | |
| 55 | vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next); |
| 56 | |
| 57 | while (n_left_from > 0 && n_left_to_next > 0) |
| 58 | { |
| 59 | vlib_buffer_t * b0; |
| 60 | bier_imp_t *bimp0; |
| 61 | bier_hdr_t *hdr0; |
| 62 | u32 bi0, bii0; |
| 63 | u32 next0; |
| 64 | |
| 65 | bi0 = from[0]; |
| 66 | to_next[0] = bi0; |
| 67 | from += 1; |
| 68 | to_next += 1; |
| 69 | n_left_from -= 1; |
| 70 | n_left_to_next -= 1; |
| 71 | |
| 72 | b0 = vlib_get_buffer (vm, bi0); |
| 73 | |
| 74 | bii0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX]; |
| 75 | bimp0 = bier_imp_get(bii0); |
| 76 | |
| 77 | if (FIB_PROTOCOL_IP4 == fproto) |
| 78 | { |
| 79 | /* |
| 80 | * decrement the TTL on ingress to the BIER domain |
| 81 | */ |
| 82 | ip4_header_t * ip0 = vlib_buffer_get_current(b0); |
| 83 | u32 checksum0; |
| 84 | |
| 85 | checksum0 = ip0->checksum + clib_host_to_net_u16 (0x0100); |
| 86 | checksum0 += checksum0 >= 0xffff; |
| 87 | |
| 88 | ip0->checksum = checksum0; |
| 89 | ip0->ttl -= 1; |
| 90 | |
| 91 | /* |
| 92 | * calculate an entropy |
| 93 | */ |
| 94 | if (0 == vnet_buffer(b0)->ip.flow_hash) |
| 95 | { |
| 96 | vnet_buffer(b0)->ip.flow_hash = |
| 97 | ip4_compute_flow_hash (ip0, IP_FLOW_HASH_DEFAULT); |
| 98 | } |
| 99 | } |
| 100 | if (FIB_PROTOCOL_IP6 == fproto) |
| 101 | { |
| 102 | /* |
| 103 | * decrement the TTL on ingress to the BIER domain |
| 104 | */ |
| 105 | ip6_header_t * ip0 = vlib_buffer_get_current(b0); |
| 106 | |
| 107 | ip0->hop_limit -= 1; |
| 108 | |
| 109 | /* |
| 110 | * calculate an entropy |
| 111 | */ |
| 112 | if (0 == vnet_buffer(b0)->ip.flow_hash) |
| 113 | { |
| 114 | vnet_buffer(b0)->ip.flow_hash = |
| 115 | ip6_compute_flow_hash (ip0, IP_FLOW_HASH_DEFAULT); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* Paint the BIER header */ |
| 120 | vlib_buffer_advance(b0, -(sizeof(bier_hdr_t) + |
| 121 | bier_hdr_len_id_to_num_bytes(bimp0->bi_tbl.bti_hdr_len))); |
| 122 | hdr0 = vlib_buffer_get_current(b0); |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 123 | |
Neale Ranns | fe4e48f | 2018-09-24 23:38:37 -0700 | [diff] [blame] | 124 | /* RPF check */ |
| 125 | if (PREDICT_FALSE(BIER_RX_ITF == vnet_buffer(b0)->ip.adj_index[VLIB_RX])) |
| 126 | { |
| 127 | next0 = 0; |
| 128 | } |
| 129 | else |
| 130 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 131 | clib_memcpy_fast(hdr0, &bimp0->bi_hdr, |
Neale Ranns | fe4e48f | 2018-09-24 23:38:37 -0700 | [diff] [blame] | 132 | (sizeof(bier_hdr_t) + |
| 133 | bier_hdr_len_id_to_num_bytes(bimp0->bi_tbl.bti_hdr_len))); |
| 134 | /* |
| 135 | * Fixup the entropy and protocol, both of which have a |
| 136 | * zero value post the paint job |
| 137 | */ |
| 138 | hdr0->bh_oam_dscp_proto |= |
| 139 | clib_host_to_net_u16(bproto << BIER_HDR_PROTO_FIELD_SHIFT); |
| 140 | hdr0->bh_first_word |= |
| 141 | clib_host_to_net_u32((vnet_buffer(b0)->ip.flow_hash & |
| 142 | BIER_HDR_ENTROPY_FIELD_MASK) << |
| 143 | BIER_HDR_ENTROPY_FIELD_SHIFT); |
Neale Ranns | 9128637 | 2017-12-05 13:24:04 -0800 | [diff] [blame] | 144 | |
Neale Ranns | fe4e48f | 2018-09-24 23:38:37 -0700 | [diff] [blame] | 145 | /* |
| 146 | * use TTL 64 for the post enacp MPLS label/BIFT-ID |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 147 | * this we be decremented in bier_output node. |
Neale Ranns | fe4e48f | 2018-09-24 23:38:37 -0700 | [diff] [blame] | 148 | */ |
| 149 | vnet_buffer(b0)->mpls.ttl = 65; |
| 150 | |
| 151 | /* next node */ |
| 152 | next0 = bimp0->bi_dpo[fproto].dpoi_next_node; |
| 153 | vnet_buffer(b0)->ip.adj_index[VLIB_TX] = |
| 154 | bimp0->bi_dpo[fproto].dpoi_index; |
| 155 | } |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 156 | |
| 157 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 158 | { |
| 159 | bier_imp_trace_t *tr = |
| 160 | vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 161 | tr->imp = bii0; |
| 162 | tr->hdr = *hdr0; |
| 163 | } |
| 164 | |
| 165 | vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, |
| 166 | n_left_to_next, bi0, next0); |
| 167 | } |
| 168 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 169 | } |
| 170 | return from_frame->n_vectors; |
| 171 | } |
| 172 | |
| 173 | static u8 * |
| 174 | format_bier_imp_trace (u8 * s, va_list * args) |
| 175 | { |
| 176 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 177 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 178 | bier_imp_trace_t * t; |
| 179 | u32 indent; |
| 180 | |
| 181 | t = va_arg (*args, bier_imp_trace_t *); |
| 182 | indent = format_get_indent (s); |
| 183 | |
| 184 | s = format (s, "%U", format_bier_imp, t->imp, indent, BIER_SHOW_BRIEF); |
| 185 | return (s); |
| 186 | } |
| 187 | |
Filip Tehlar | a01e032 | 2019-03-05 03:34:52 -0800 | [diff] [blame] | 188 | VLIB_NODE_FN (bier_imp_ip4_node) (vlib_main_t * vm, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 189 | vlib_node_runtime_t * node, |
| 190 | vlib_frame_t * frame) |
| 191 | { |
| 192 | return (bier_imp_dpo_inline(vm, node, frame, |
| 193 | FIB_PROTOCOL_IP4, |
| 194 | BIER_HDR_PROTO_IPV4)); |
| 195 | } |
| 196 | |
| 197 | VLIB_REGISTER_NODE (bier_imp_ip4_node) = { |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 198 | .name = "bier-imp-ip4", |
| 199 | .vector_size = sizeof (u32), |
| 200 | |
| 201 | .format_trace = format_bier_imp_trace, |
| 202 | .n_next_nodes = 1, |
| 203 | .next_nodes = { |
Neale Ranns | fe4e48f | 2018-09-24 23:38:37 -0700 | [diff] [blame] | 204 | [0] = "bier-drop", |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 205 | } |
| 206 | }; |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 207 | |
Filip Tehlar | a01e032 | 2019-03-05 03:34:52 -0800 | [diff] [blame] | 208 | VLIB_NODE_FN (bier_imp_ip6_node) (vlib_main_t * vm, |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 209 | vlib_node_runtime_t * node, |
| 210 | vlib_frame_t * frame) |
| 211 | { |
| 212 | return (bier_imp_dpo_inline(vm, node, frame, |
| 213 | FIB_PROTOCOL_IP6, |
| 214 | BIER_HDR_PROTO_IPV6)); |
| 215 | } |
| 216 | |
| 217 | VLIB_REGISTER_NODE (bier_imp_ip6_node) = { |
Neale Ranns | d792d9c | 2017-10-21 10:53:20 -0700 | [diff] [blame] | 218 | .name = "bier-imp-ip6", |
| 219 | .vector_size = sizeof (u32), |
| 220 | |
| 221 | .format_trace = format_bier_imp_trace, |
| 222 | .n_next_nodes = 1, |
| 223 | .next_nodes = { |
| 224 | [0] = "error-drop", |
| 225 | } |
| 226 | }; |