2 * sr_replicate.c: ipv6 segment routing replicator for multicast
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #if DPDK > 0 /* Cannot run replicate without DPDK */
19 #include <vlib/vlib.h>
20 #include <vnet/vnet.h>
21 #include <vnet/pg/pg.h>
22 #include <vnet/sr/sr.h>
23 #include <vnet/devices/dpdk/dpdk.h>
24 #include <vnet/dpdk_replication.h>
25 #include <vnet/ip/ip.h>
27 #include <vppinfra/hash.h>
28 #include <vppinfra/error.h>
29 #include <vppinfra/elog.h>
34 vlib_main_t *vlib_main;
35 vnet_main_t *vnet_main;
36 } sr_replicate_main_t;
38 sr_replicate_main_t sr_replicate_main;
43 ip6_address_t src, dst;
48 } sr_replicate_trace_t;
50 /* packet trace format function */
52 format_sr_replicate_trace (u8 * s, va_list * args)
54 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
55 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
56 sr_replicate_trace_t *t = va_arg (*args, sr_replicate_trace_t *);
57 ip6_main_t *im = &ip6_main;
58 ip6_sr_main_t *sm = &sr_main;
59 ip6_sr_tunnel_t *tun = pool_elt_at_index (sm->tunnels, t->tunnel_index);
60 ip6_fib_t *rx_fib, *tx_fib;
62 rx_fib = find_ip6_fib_by_table_index_or_id (im, tun->rx_fib_index,
63 IP6_ROUTE_FLAG_FIB_INDEX);
65 tx_fib = find_ip6_fib_by_table_index_or_id (im, tun->tx_fib_index,
66 IP6_ROUTE_FLAG_FIB_INDEX);
69 (s, "SR-REPLICATE: next %s ip6 src %U dst %U len %u\n"
70 " rx-fib-id %d tx-fib-id %d\n%U",
72 format_ip6_address, &t->src,
73 format_ip6_address, &t->dst, t->length,
74 rx_fib->table_id, tx_fib->table_id,
75 format_ip6_sr_header, t->sr, 0 /* print_hmac */ );
80 #define foreach_sr_replicate_error \
81 _(REPLICATED, "sr packets replicated") \
82 _(NO_BUFFERS, "error allocating buffers for replicas") \
83 _(NO_REPLICAS, "no replicas were needed") \
84 _(NO_BUFFER_DROPS, "sr no buffer drops")
88 #define _(sym,str) SR_REPLICATE_ERROR_##sym,
89 foreach_sr_replicate_error
92 } sr_replicate_error_t;
94 static char *sr_replicate_error_strings[] = {
95 #define _(sym,string) string,
96 foreach_sr_replicate_error
102 SR_REPLICATE_NEXT_IP6_LOOKUP,
104 } sr_replicate_next_t;
107 sr_replicate_node_fn (vlib_main_t * vm,
108 vlib_node_runtime_t * node, vlib_frame_t * frame)
110 u32 n_left_from, *from, *to_next;
111 sr_replicate_next_t next_index;
112 int pkts_replicated = 0;
113 ip6_sr_main_t *sm = &sr_main;
114 int no_buffer_drops = 0;
115 vlib_buffer_free_list_t *fl;
116 unsigned socket_id = rte_socket_id ();
117 vlib_buffer_main_t *bm = vm->buffer_main;
119 from = vlib_frame_vector_args (frame);
120 n_left_from = frame->n_vectors;
121 next_index = node->cached_next_index;
123 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
125 while (n_left_from > 0)
129 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
131 while (n_left_from > 0 && n_left_to_next > 0)
134 vlib_buffer_t *b0, *orig_b0;
135 struct rte_mbuf *orig_mb0 = 0, *hdr_mb0 = 0, *clone0 = 0;
136 struct rte_mbuf **hdr_vec = 0, **rte_mbuf_vec = 0;
137 ip6_sr_policy_t *pol0 = 0;
138 ip6_sr_tunnel_t *t0 = 0;
139 ip6_sr_header_t *hdr_sr0 = 0;
140 ip6_header_t *ip0 = 0, *hdr_ip0 = 0;
141 int num_replicas = 0;
146 b0 = vlib_get_buffer (vm, bi0);
149 pol0 = pool_elt_at_index (sm->policies,
150 vnet_buffer (b0)->ip.save_protocol);
152 ip0 = vlib_buffer_get_current (b0);
153 /* Skip forward to the punch-in point */
154 vlib_buffer_advance (b0, sizeof (*ip0));
156 orig_mb0 = rte_mbuf_from_vlib_buffer (b0);
158 i16 delta0 = vlib_buffer_length_in_chain (vm, orig_b0)
159 - (i16) orig_mb0->pkt_len;
161 u16 new_data_len0 = (u16) ((i16) orig_mb0->data_len + delta0);
162 u16 new_pkt_len0 = (u16) ((i16) orig_mb0->pkt_len + delta0);
164 orig_mb0->data_len = new_data_len0;
165 orig_mb0->pkt_len = new_pkt_len0;
167 (u16) (RTE_PKTMBUF_HEADROOM + b0->current_data);
170 Before entering loop determine if we can allocate:
171 - all the new HEADER RTE_MBUFs and assign them to a vector
174 if successful, then iterate over vectors of resources
177 num_replicas = vec_len (pol0->tunnel_indices);
179 if (PREDICT_FALSE (num_replicas == 0))
181 b0->error = node->errors[SR_REPLICATE_ERROR_NO_REPLICAS];
185 vec_reset_length (hdr_vec);
186 vec_reset_length (rte_mbuf_vec);
188 for (i = 0; i < num_replicas; i++)
190 hdr_mb0 = rte_pktmbuf_alloc (bm->pktmbuf_pools[socket_id]);
192 if (i < (num_replicas - 1))
193 /* Not the last tunnel to process */
194 clone0 = rte_pktmbuf_clone
195 (orig_mb0, bm->pktmbuf_pools[socket_id]);
197 /* Last tunnel to process, use original MB */
201 if (PREDICT_FALSE (!clone0 || !hdr_mb0))
203 b0->error = node->errors[SR_REPLICATE_ERROR_NO_BUFFERS];
205 vec_foreach_index (i, rte_mbuf_vec)
207 rte_pktmbuf_free (rte_mbuf_vec[i]);
209 vec_free (rte_mbuf_vec);
211 vec_foreach_index (i, hdr_vec)
213 rte_pktmbuf_free (hdr_vec[i]);
220 vec_add1 (hdr_vec, hdr_mb0);
221 vec_add1 (rte_mbuf_vec, clone0);
225 for (i = 0; i < num_replicas; i++)
227 vlib_buffer_t *hdr_b0;
229 t0 = vec_elt_at_index (sm->tunnels, pol0->tunnel_indices[i]);
232 hdr_mb0 = hdr_vec[i];
233 clone0 = rte_mbuf_vec[i];
235 hdr_mb0->data_len = sizeof (*ip0) + vec_len (t0->rewrite);
236 hdr_mb0->pkt_len = hdr_mb0->data_len +
237 vlib_buffer_length_in_chain (vm, orig_b0);
239 hdr_b0 = vlib_buffer_from_rte_mbuf (hdr_mb0);
241 vlib_buffer_init_for_free_list (hdr_b0, fl);
243 memcpy (hdr_b0->data, ip0, sizeof (*ip0));
244 memcpy (hdr_b0->data + sizeof (*ip0), t0->rewrite,
245 vec_len (t0->rewrite));
247 hdr_b0->current_data = 0;
248 hdr_b0->current_length = sizeof (*ip0) + vec_len (t0->rewrite);
249 hdr_b0->flags = orig_b0->flags | VLIB_BUFFER_NEXT_PRESENT;
252 hdr_b0->total_length_not_including_first_buffer =
253 hdr_mb0->pkt_len - hdr_b0->current_length;
255 hdr_ip0 = (ip6_header_t *) hdr_b0->data;
256 hdr_ip0->payload_length =
257 clib_host_to_net_u16 (hdr_mb0->data_len);
258 hdr_sr0 = (ip6_sr_header_t *) (hdr_ip0 + 1);
259 hdr_sr0->protocol = hdr_ip0->protocol;
260 hdr_ip0->protocol = 43;
262 /* Rewrite the ip6 dst address */
263 hdr_ip0->dst_address.as_u64[0] = t0->first_hop.as_u64[0];
264 hdr_ip0->dst_address.as_u64[1] = t0->first_hop.as_u64[1];
266 sr_fix_hmac (sm, hdr_ip0, hdr_sr0);
268 /* prepend new header to invariant piece */
269 hdr_mb0->next = clone0;
270 hdr_b0->next_buffer =
271 vlib_get_buffer_index (vm,
272 vlib_buffer_from_rte_mbuf (clone0));
274 /* update header's fields */
276 (uint16_t) (hdr_mb0->data_len + clone0->pkt_len);
277 hdr_mb0->nb_segs = (uint8_t) (clone0->nb_segs + 1);
279 /* copy metadata from source packet */
280 hdr_mb0->port = clone0->port;
281 hdr_mb0->vlan_tci = clone0->vlan_tci;
282 hdr_mb0->vlan_tci_outer = clone0->vlan_tci_outer;
283 hdr_mb0->tx_offload = clone0->tx_offload;
284 hdr_mb0->hash = clone0->hash;
286 hdr_mb0->ol_flags = clone0->ol_flags;
288 __rte_mbuf_sanity_check (hdr_mb0, 1);
290 hdr_bi0 = vlib_get_buffer_index (vm, hdr_b0);
292 to_next[0] = hdr_bi0;
296 if (n_left_to_next == 0)
298 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
299 vlib_get_next_frame (vm, node, next_index,
300 to_next, n_left_to_next);
310 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
312 sr_replicate_trace_t *tr = vlib_add_trace (vm, node,
314 tr->tunnel_index = t0 - sm->tunnels;
318 memcpy (tr->src.as_u8, hdr_ip0->src_address.as_u8,
319 sizeof (tr->src.as_u8));
320 memcpy (tr->dst.as_u8, hdr_ip0->dst_address.as_u8,
321 sizeof (tr->dst.as_u8));
322 if (hdr_ip0->payload_length)
323 tr->length = clib_net_to_host_u16
324 (hdr_ip0->payload_length);
326 tr->next_index = next_index;
327 memcpy (tr->sr, hdr_sr0, sizeof (tr->sr));
332 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
335 vlib_node_increment_counter (vm, sr_replicate_node.index,
336 SR_REPLICATE_ERROR_REPLICATED,
339 vlib_node_increment_counter (vm, sr_replicate_node.index,
340 SR_REPLICATE_ERROR_NO_BUFFER_DROPS,
343 return frame->n_vectors;
347 VLIB_REGISTER_NODE (sr_replicate_node) = {
348 .function = sr_replicate_node_fn,
349 .name = "sr-replicate",
350 .vector_size = sizeof (u32),
351 .format_trace = format_sr_replicate_trace,
352 .type = VLIB_NODE_TYPE_INTERNAL,
354 .n_errors = ARRAY_LEN(sr_replicate_error_strings),
355 .error_strings = sr_replicate_error_strings,
357 .n_next_nodes = SR_REPLICATE_N_NEXT,
360 [SR_REPLICATE_NEXT_IP6_LOOKUP] = "ip6-lookup",
365 VLIB_NODE_FUNCTION_MULTIARCH (sr_replicate_node, sr_replicate_node_fn)
366 clib_error_t *sr_replicate_init (vlib_main_t * vm)
368 sr_replicate_main_t *msm = &sr_replicate_main;
371 msm->vnet_main = vnet_get_main ();
376 VLIB_INIT_FUNCTION (sr_replicate_init);
381 * fd.io coding-style-patch-verification: ON
384 * eval: (c-set-style "gnu")