Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * sr_replicate.c: ipv6 segment routing replicator for multicast |
| 3 | * |
| 4 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Keith Burns (alagalah) | ff07b87 | 2016-08-06 08:55:57 -0700 | [diff] [blame] | 17 | /** |
| 18 | * @file |
| 19 | * @brief Functions for replicating packets across SR tunnels. |
| 20 | * |
| 21 | * Leverages rte_pktmbuf_clone() so there is no memcpy for |
| 22 | * invariant parts of the packet. |
| 23 | * |
| 24 | * @note Currently requires DPDK |
| 25 | */ |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 26 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 27 | #if DPDK > 0 /* Cannot run replicate without DPDK */ |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 28 | #include <vlib/vlib.h> |
| 29 | #include <vnet/vnet.h> |
| 30 | #include <vnet/pg/pg.h> |
| 31 | #include <vnet/sr/sr.h> |
| 32 | #include <vnet/devices/dpdk/dpdk.h> |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 33 | #include <vnet/ip/ip.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 34 | #include <vnet/fib/ip6_fib.h> |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 35 | |
| 36 | #include <vppinfra/hash.h> |
| 37 | #include <vppinfra/error.h> |
| 38 | #include <vppinfra/elog.h> |
| 39 | |
Keith Burns (alagalah) | ff07b87 | 2016-08-06 08:55:57 -0700 | [diff] [blame] | 40 | /** |
| 41 | * @brief sr_replicate state. |
| 42 | * |
| 43 | */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 44 | typedef struct |
| 45 | { |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 46 | /* convenience */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 47 | vlib_main_t *vlib_main; |
| 48 | vnet_main_t *vnet_main; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 49 | } sr_replicate_main_t; |
| 50 | |
| 51 | sr_replicate_main_t sr_replicate_main; |
| 52 | |
Keith Burns (alagalah) | ff07b87 | 2016-08-06 08:55:57 -0700 | [diff] [blame] | 53 | /** |
| 54 | * @brief Information to display in packet trace. |
| 55 | * |
| 56 | */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 59 | ip6_address_t src, dst; |
| 60 | u16 length; |
| 61 | u32 next_index; |
| 62 | u32 tunnel_index; |
| 63 | u8 sr[256]; |
| 64 | } sr_replicate_trace_t; |
| 65 | |
Keith Burns (alagalah) | ff07b87 | 2016-08-06 08:55:57 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @brief packet trace format function. |
| 68 | * |
| 69 | * @param *s u8 used for string output |
| 70 | * @param *args va_list structured input to va_arg to output @ref sr_replicate_trace_t |
| 71 | * @return *s u8 - formatted trace output |
| 72 | */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 73 | static u8 * |
| 74 | format_sr_replicate_trace (u8 * s, va_list * args) |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 75 | { |
| 76 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 77 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 78 | sr_replicate_trace_t *t = va_arg (*args, sr_replicate_trace_t *); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 79 | ip6_sr_main_t *sm = &sr_main; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 80 | ip6_sr_tunnel_t *tun = pool_elt_at_index (sm->tunnels, t->tunnel_index); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 81 | ip6_fib_t *rx_fib, *tx_fib; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 82 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 83 | rx_fib = ip6_fib_get (tun->rx_fib_index); |
| 84 | tx_fib = ip6_fib_get (tun->tx_fib_index); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 85 | |
| 86 | s = format |
| 87 | (s, "SR-REPLICATE: next %s ip6 src %U dst %U len %u\n" |
| 88 | " rx-fib-id %d tx-fib-id %d\n%U", |
| 89 | "ip6-lookup", |
| 90 | format_ip6_address, &t->src, |
| 91 | format_ip6_address, &t->dst, t->length, |
| 92 | rx_fib->table_id, tx_fib->table_id, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 93 | format_ip6_sr_header, t->sr, 0 /* print_hmac */ ); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 94 | return s; |
| 95 | |
| 96 | } |
| 97 | |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 98 | #define foreach_sr_replicate_error \ |
| 99 | _(REPLICATED, "sr packets replicated") \ |
| 100 | _(NO_BUFFERS, "error allocating buffers for replicas") \ |
| 101 | _(NO_REPLICAS, "no replicas were needed") \ |
| 102 | _(NO_BUFFER_DROPS, "sr no buffer drops") |
| 103 | |
Keith Burns (alagalah) | 7214cf1 | 2016-08-08 15:56:50 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @brief Struct for SR replicate errors |
| 106 | */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 107 | typedef enum |
| 108 | { |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 109 | #define _(sym,str) SR_REPLICATE_ERROR_##sym, |
| 110 | foreach_sr_replicate_error |
| 111 | #undef _ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 112 | SR_REPLICATE_N_ERROR, |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 113 | } sr_replicate_error_t; |
| 114 | |
Keith Burns (alagalah) | 7214cf1 | 2016-08-08 15:56:50 -0700 | [diff] [blame] | 115 | /** |
| 116 | * @brief Error strings for SR replicate |
| 117 | */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 118 | static char *sr_replicate_error_strings[] = { |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 119 | #define _(sym,string) string, |
| 120 | foreach_sr_replicate_error |
| 121 | #undef _ |
| 122 | }; |
| 123 | |
Keith Burns (alagalah) | ff07b87 | 2016-08-06 08:55:57 -0700 | [diff] [blame] | 124 | /** |
| 125 | * @brief Defines next-nodes for packet processing. |
| 126 | * |
| 127 | */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 128 | typedef enum |
| 129 | { |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 130 | SR_REPLICATE_NEXT_IP6_LOOKUP, |
| 131 | SR_REPLICATE_N_NEXT, |
| 132 | } sr_replicate_next_t; |
| 133 | |
Keith Burns (alagalah) | ff07b87 | 2016-08-06 08:55:57 -0700 | [diff] [blame] | 134 | /** |
| 135 | * @brief Single loop packet replicator. |
| 136 | * |
| 137 | * @node sr-replicate |
| 138 | * @param vm vlib_main_t |
| 139 | * @return frame->n_vectors uword |
| 140 | */ |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 141 | static uword |
| 142 | sr_replicate_node_fn (vlib_main_t * vm, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 143 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 144 | { |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 145 | u32 n_left_from, *from, *to_next; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 146 | sr_replicate_next_t next_index; |
| 147 | int pkts_replicated = 0; |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 148 | ip6_sr_main_t *sm = &sr_main; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 149 | int no_buffer_drops = 0; |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 150 | vlib_buffer_free_list_t *fl; |
| 151 | unsigned socket_id = rte_socket_id (); |
| 152 | vlib_buffer_main_t *bm = vm->buffer_main; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 153 | |
| 154 | from = vlib_frame_vector_args (frame); |
| 155 | n_left_from = frame->n_vectors; |
| 156 | next_index = node->cached_next_index; |
| 157 | |
| 158 | fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
| 159 | |
| 160 | while (n_left_from > 0) |
| 161 | { |
| 162 | u32 n_left_to_next; |
| 163 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 164 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 165 | |
| 166 | while (n_left_from > 0 && n_left_to_next > 0) |
| 167 | { |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 168 | u32 bi0, hdr_bi0; |
| 169 | vlib_buffer_t *b0, *orig_b0; |
| 170 | struct rte_mbuf *orig_mb0 = 0, *hdr_mb0 = 0, *clone0 = 0; |
| 171 | struct rte_mbuf **hdr_vec = 0, **rte_mbuf_vec = 0; |
| 172 | ip6_sr_policy_t *pol0 = 0; |
| 173 | ip6_sr_tunnel_t *t0 = 0; |
| 174 | ip6_sr_header_t *hdr_sr0 = 0; |
| 175 | ip6_header_t *ip0 = 0, *hdr_ip0 = 0; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 176 | int num_replicas = 0; |
| 177 | int i; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 178 | u32 len_bytes = sizeof (ip6_header_t); |
| 179 | u8 next_hdr, ip_next_hdr = IPPROTO_IPV6_ROUTE; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 180 | |
| 181 | bi0 = from[0]; |
| 182 | |
| 183 | b0 = vlib_get_buffer (vm, bi0); |
| 184 | orig_b0 = b0; |
| 185 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 186 | pol0 = pool_elt_at_index (sm->policies, |
| 187 | vnet_buffer (b0)->ip.save_protocol); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 188 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 189 | ip0 = vlib_buffer_get_current (b0); |
| 190 | /* Skip forward to the punch-in point */ |
| 191 | vlib_buffer_advance (b0, sizeof (*ip0)); |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 192 | next_hdr = ip0->protocol; |
| 193 | |
| 194 | /* HBH must immediately follow ipv6 header */ |
| 195 | if (PREDICT_FALSE |
| 196 | (ip0->protocol == IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS)) |
| 197 | { |
| 198 | ip6_hop_by_hop_ext_t *ext_hdr = |
| 199 | (ip6_hop_by_hop_ext_t *) ip6_next_header (ip0); |
| 200 | u32 ext_hdr_len = 0; |
| 201 | ext_hdr_len = ip6_ext_header_len ((ip6_ext_header_t *) ext_hdr); |
| 202 | len_bytes += ext_hdr_len; |
| 203 | next_hdr = ext_hdr->next_hdr; |
| 204 | ext_hdr->next_hdr = IPPROTO_IPV6_ROUTE; |
| 205 | ip_next_hdr = IP_PROTOCOL_IP6_HOP_BY_HOP_OPTIONS; |
| 206 | /* Skip forward to the punch-in point */ |
| 207 | vlib_buffer_advance (b0, ext_hdr_len); |
| 208 | |
| 209 | } |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 210 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 211 | orig_mb0 = rte_mbuf_from_vlib_buffer (b0); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 212 | |
| 213 | i16 delta0 = vlib_buffer_length_in_chain (vm, orig_b0) |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 214 | - (i16) orig_mb0->pkt_len; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 215 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 216 | u16 new_data_len0 = (u16) ((i16) orig_mb0->data_len + delta0); |
| 217 | u16 new_pkt_len0 = (u16) ((i16) orig_mb0->pkt_len + delta0); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 218 | |
| 219 | orig_mb0->data_len = new_data_len0; |
| 220 | orig_mb0->pkt_len = new_pkt_len0; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 221 | orig_mb0->data_off += (u16) (b0->current_data); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 222 | |
| 223 | /* |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 224 | Before entering loop determine if we can allocate: |
| 225 | - all the new HEADER RTE_MBUFs and assign them to a vector |
| 226 | - all the clones |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 227 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 228 | if successful, then iterate over vectors of resources |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 229 | |
| 230 | */ |
| 231 | num_replicas = vec_len (pol0->tunnel_indices); |
| 232 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 233 | if (PREDICT_FALSE (num_replicas == 0)) |
| 234 | { |
| 235 | b0->error = node->errors[SR_REPLICATE_ERROR_NO_REPLICAS]; |
| 236 | goto do_trace0; |
| 237 | } |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 238 | |
| 239 | vec_reset_length (hdr_vec); |
| 240 | vec_reset_length (rte_mbuf_vec); |
| 241 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 242 | for (i = 0; i < num_replicas; i++) |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 243 | { |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 244 | uint8_t nb_seg; |
| 245 | struct rte_mbuf *clone0i; |
| 246 | vlib_buffer_t *clone0_c, *clone_b0; |
| 247 | |
| 248 | t0 = vec_elt_at_index (sm->tunnels, pol0->tunnel_indices[i]); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 249 | hdr_mb0 = rte_pktmbuf_alloc (bm->pktmbuf_pools[socket_id]); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 250 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 251 | if (i < (num_replicas - 1)) |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 252 | { |
| 253 | /* Not the last tunnel to process */ |
| 254 | clone0 = rte_pktmbuf_clone |
| 255 | (orig_mb0, bm->pktmbuf_pools[socket_id]); |
Dave Barach | bd6462e | 2016-12-16 09:20:04 -0500 | [diff] [blame] | 256 | if (clone0 == 0) |
| 257 | goto clone_fail; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 258 | nb_seg = 0; |
| 259 | clone0i = clone0; |
| 260 | clone0_c = NULL; |
| 261 | while ((clone0->nb_segs >= 1) && (nb_seg < clone0->nb_segs)) |
| 262 | { |
| 263 | |
| 264 | clone_b0 = vlib_buffer_from_rte_mbuf (clone0i); |
| 265 | vlib_buffer_init_for_free_list (clone_b0, fl); |
| 266 | |
| 267 | ASSERT ((clone_b0->flags & VLIB_BUFFER_NEXT_PRESENT) == |
| 268 | 0); |
| 269 | ASSERT (clone_b0->current_data == 0); |
| 270 | |
| 271 | clone_b0->current_data = |
| 272 | (clone0i->buf_addr + clone0i->data_off) - |
| 273 | (void *) clone_b0->data; |
| 274 | |
| 275 | clone_b0->current_length = clone0i->data_len; |
| 276 | if (PREDICT_FALSE (clone0_c != NULL)) |
| 277 | { |
| 278 | clone0_c->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 279 | clone0_c->next_buffer = |
| 280 | vlib_get_buffer_index (vm, clone_b0); |
| 281 | } |
| 282 | clone0_c = clone_b0; |
| 283 | clone0i = clone0i->next; |
| 284 | nb_seg++; |
| 285 | } |
| 286 | } |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 287 | else |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 288 | /* First tunnel to process, use original MB */ |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 289 | clone0 = orig_mb0; |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 290 | |
| 291 | |
| 292 | if (PREDICT_FALSE (!clone0 || !hdr_mb0)) |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 293 | { |
Dave Barach | bd6462e | 2016-12-16 09:20:04 -0500 | [diff] [blame] | 294 | clone_fail: |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 295 | b0->error = node->errors[SR_REPLICATE_ERROR_NO_BUFFERS]; |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 296 | |
| 297 | vec_foreach_index (i, rte_mbuf_vec) |
| 298 | { |
| 299 | rte_pktmbuf_free (rte_mbuf_vec[i]); |
| 300 | } |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 301 | vec_free (rte_mbuf_vec); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 302 | |
| 303 | vec_foreach_index (i, hdr_vec) |
| 304 | { |
| 305 | rte_pktmbuf_free (hdr_vec[i]); |
| 306 | } |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 307 | vec_free (hdr_vec); |
| 308 | |
| 309 | goto do_trace0; |
| 310 | } |
| 311 | |
| 312 | vec_add1 (hdr_vec, hdr_mb0); |
| 313 | vec_add1 (rte_mbuf_vec, clone0); |
| 314 | |
| 315 | } |
| 316 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 317 | for (i = 0; i < num_replicas; i++) |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 318 | { |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 319 | vlib_buffer_t *hdr_b0; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 320 | u16 new_l0 = 0; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 321 | |
| 322 | t0 = vec_elt_at_index (sm->tunnels, pol0->tunnel_indices[i]); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 323 | /* Our replicas */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 324 | hdr_mb0 = hdr_vec[i]; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 325 | clone0 = rte_mbuf_vec[i]; |
| 326 | |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 327 | hdr_mb0->data_len = len_bytes + vec_len (t0->rewrite); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 328 | hdr_mb0->pkt_len = hdr_mb0->data_len + |
| 329 | vlib_buffer_length_in_chain (vm, orig_b0); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 330 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 331 | hdr_b0 = vlib_buffer_from_rte_mbuf (hdr_mb0); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 332 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 333 | vlib_buffer_init_for_free_list (hdr_b0, fl); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 334 | |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 335 | memcpy (hdr_b0->data, ip0, len_bytes); |
| 336 | memcpy (hdr_b0->data + len_bytes, t0->rewrite, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 337 | vec_len (t0->rewrite)); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 338 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 339 | hdr_b0->current_data = 0; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 340 | hdr_b0->current_length = len_bytes + vec_len (t0->rewrite); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 341 | hdr_b0->flags = orig_b0->flags | VLIB_BUFFER_NEXT_PRESENT; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 342 | hdr_b0->trace_index = orig_b0->trace_index; |
| 343 | vnet_buffer (hdr_b0)->l2_classify.opaque_index = 0; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 344 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 345 | hdr_b0->total_length_not_including_first_buffer = |
| 346 | hdr_mb0->pkt_len - hdr_b0->current_length; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 347 | vnet_buffer (hdr_b0)->sw_if_index[VLIB_TX] = t0->tx_fib_index; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 348 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 349 | hdr_ip0 = (ip6_header_t *) hdr_b0->data; |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 350 | new_l0 = clib_net_to_host_u16 (ip0->payload_length) + |
| 351 | vec_len (t0->rewrite); |
| 352 | hdr_ip0->payload_length = clib_host_to_net_u16 (new_l0); |
| 353 | hdr_sr0 = (ip6_sr_header_t *) ((u8 *) hdr_ip0 + len_bytes); |
| 354 | /* $$$ tune */ |
| 355 | clib_memcpy (hdr_sr0, t0->rewrite, vec_len (t0->rewrite)); |
| 356 | hdr_sr0->protocol = next_hdr; |
| 357 | hdr_ip0->protocol = ip_next_hdr; |
| 358 | |
| 359 | /* Copy dst address into the DA slot in the segment list */ |
| 360 | clib_memcpy (hdr_sr0->segments, ip0->dst_address.as_u64, |
| 361 | sizeof (ip6_address_t)); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 362 | |
| 363 | /* Rewrite the ip6 dst address */ |
| 364 | hdr_ip0->dst_address.as_u64[0] = t0->first_hop.as_u64[0]; |
| 365 | hdr_ip0->dst_address.as_u64[1] = t0->first_hop.as_u64[1]; |
| 366 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 367 | sr_fix_hmac (sm, hdr_ip0, hdr_sr0); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 368 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 369 | /* prepend new header to invariant piece */ |
| 370 | hdr_mb0->next = clone0; |
| 371 | hdr_b0->next_buffer = |
| 372 | vlib_get_buffer_index (vm, |
| 373 | vlib_buffer_from_rte_mbuf (clone0)); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 374 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 375 | /* update header's fields */ |
| 376 | hdr_mb0->pkt_len = |
| 377 | (uint16_t) (hdr_mb0->data_len + clone0->pkt_len); |
| 378 | hdr_mb0->nb_segs = (uint8_t) (clone0->nb_segs + 1); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 379 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 380 | /* copy metadata from source packet */ |
| 381 | hdr_mb0->port = clone0->port; |
| 382 | hdr_mb0->vlan_tci = clone0->vlan_tci; |
| 383 | hdr_mb0->vlan_tci_outer = clone0->vlan_tci_outer; |
| 384 | hdr_mb0->tx_offload = clone0->tx_offload; |
| 385 | hdr_mb0->hash = clone0->hash; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 386 | |
Shwetha | b78292e | 2016-09-13 11:51:00 +0100 | [diff] [blame] | 387 | hdr_mb0->ol_flags = clone0->ol_flags & ~(IND_ATTACHED_MBUF); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 388 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 389 | __rte_mbuf_sanity_check (hdr_mb0, 1); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 390 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 391 | hdr_bi0 = vlib_get_buffer_index (vm, hdr_b0); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 392 | |
| 393 | to_next[0] = hdr_bi0; |
| 394 | to_next += 1; |
| 395 | n_left_to_next -= 1; |
| 396 | |
| 397 | if (n_left_to_next == 0) |
| 398 | { |
| 399 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 400 | vlib_get_next_frame (vm, node, next_index, |
| 401 | to_next, n_left_to_next); |
| 402 | |
| 403 | } |
| 404 | pkts_replicated++; |
| 405 | } |
| 406 | |
| 407 | from += 1; |
| 408 | n_left_from -= 1; |
| 409 | |
| 410 | do_trace0: |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 411 | if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 412 | { |
| 413 | sr_replicate_trace_t *tr = vlib_add_trace (vm, node, |
| 414 | b0, sizeof (*tr)); |
| 415 | tr->tunnel_index = t0 - sm->tunnels; |
| 416 | tr->length = 0; |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 417 | if (hdr_ip0) |
| 418 | { |
| 419 | memcpy (tr->src.as_u8, hdr_ip0->src_address.as_u8, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 420 | sizeof (tr->src.as_u8)); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 421 | memcpy (tr->dst.as_u8, hdr_ip0->dst_address.as_u8, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 422 | sizeof (tr->dst.as_u8)); |
| 423 | if (hdr_ip0->payload_length) |
| 424 | tr->length = clib_net_to_host_u16 |
| 425 | (hdr_ip0->payload_length); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 426 | } |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 427 | tr->next_index = next_index; |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 428 | if (hdr_sr0) |
| 429 | memcpy (tr->sr, hdr_sr0, sizeof (tr->sr)); |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 430 | } |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 431 | |
| 432 | } |
| 433 | |
| 434 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 435 | } |
| 436 | |
| 437 | vlib_node_increment_counter (vm, sr_replicate_node.index, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 438 | SR_REPLICATE_ERROR_REPLICATED, |
| 439 | pkts_replicated); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 440 | |
| 441 | vlib_node_increment_counter (vm, sr_replicate_node.index, |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 442 | SR_REPLICATE_ERROR_NO_BUFFER_DROPS, |
| 443 | no_buffer_drops); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 444 | |
| 445 | return frame->n_vectors; |
| 446 | } |
| 447 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 448 | /* *INDENT-OFF* */ |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 449 | VLIB_REGISTER_NODE (sr_replicate_node) = { |
| 450 | .function = sr_replicate_node_fn, |
| 451 | .name = "sr-replicate", |
| 452 | .vector_size = sizeof (u32), |
| 453 | .format_trace = format_sr_replicate_trace, |
| 454 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 455 | |
| 456 | .n_errors = ARRAY_LEN(sr_replicate_error_strings), |
| 457 | .error_strings = sr_replicate_error_strings, |
| 458 | |
| 459 | .n_next_nodes = SR_REPLICATE_N_NEXT, |
| 460 | |
| 461 | .next_nodes = { |
| 462 | [SR_REPLICATE_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 463 | }, |
| 464 | }; |
| 465 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 466 | VLIB_NODE_FUNCTION_MULTIARCH (sr_replicate_node, sr_replicate_node_fn) |
Damjan Marion | e33b9e0 | 2016-10-17 12:51:18 +0200 | [diff] [blame] | 467 | /* *INDENT-ON* */ |
| 468 | |
| 469 | clib_error_t * |
| 470 | sr_replicate_init (vlib_main_t * vm) |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 471 | { |
| 472 | sr_replicate_main_t *msm = &sr_replicate_main; |
| 473 | |
| 474 | msm->vlib_main = vm; |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 475 | msm->vnet_main = vnet_get_main (); |
Keith Burns (alagalah) | 52fc44d | 2016-03-25 09:38:50 -0700 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 480 | VLIB_INIT_FUNCTION (sr_replicate_init); |
Keith Burns (alagalah) | 21c33bb | 2016-05-02 13:13:46 -0700 | [diff] [blame] | 481 | |
| 482 | #endif /* DPDK */ |
Keith Burns (alagalah) | 06c5ffd | 2016-08-06 08:32:45 -0700 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * fd.io coding-style-patch-verification: ON |
| 486 | * |
| 487 | * Local Variables: |
| 488 | * eval: (c-set-style "gnu") |
| 489 | * End: |
| 490 | */ |