e60b95624e13daa189c60aa7db85fffd80e9a668
[vpp.git] / vnet / vnet / sr / sr_replicate.c
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  */
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vnet/sr/sr.h>
22 #include <vnet/devices/dpdk/dpdk.h>
23 #include <vnet/dpdk_replication.h>
24 #include <vnet/ip/ip.h>
25
26 #include <vppinfra/hash.h>
27 #include <vppinfra/error.h>
28 #include <vppinfra/elog.h>
29
30 typedef struct {
31   /* convenience */
32   vlib_main_t * vlib_main;
33   vnet_main_t * vnet_main;
34 } sr_replicate_main_t;
35
36 sr_replicate_main_t sr_replicate_main;
37
38 vlib_node_registration_t sr_replicate_node;
39
40
41 typedef struct {
42   ip6_address_t src, dst;
43   u16 length;
44   u32 next_index;
45   u32 tunnel_index;
46   u8 sr[256];
47 } sr_replicate_trace_t;
48
49 /* packet trace format function */
50 static u8 * format_sr_replicate_trace (u8 * s, va_list * args)
51 {
52   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54   sr_replicate_trace_t * t = va_arg (*args, sr_replicate_trace_t *);
55   ip6_main_t * im = &ip6_main;
56   ip6_sr_main_t * sm = &sr_main;
57   ip6_sr_tunnel_t *tun = pool_elt_at_index (sm->tunnels, t->tunnel_index);
58   ip6_fib_t * rx_fib, * tx_fib;
59
60   rx_fib = find_ip6_fib_by_table_index_or_id (im, tun->rx_fib_index,
61                                               IP6_ROUTE_FLAG_FIB_INDEX);
62
63   tx_fib = find_ip6_fib_by_table_index_or_id (im, tun->tx_fib_index,
64                                               IP6_ROUTE_FLAG_FIB_INDEX);
65
66   s = format
67     (s, "SR-REPLICATE: next %s ip6 src %U dst %U len %u\n"
68      "           rx-fib-id %d tx-fib-id %d\n%U",
69      "ip6-lookup",
70      format_ip6_address, &t->src,
71      format_ip6_address, &t->dst, t->length,
72      rx_fib->table_id, tx_fib->table_id,
73      format_ip6_sr_header, t->sr, 0 /* print_hmac */);
74   return s;
75
76 }
77
78 vlib_node_registration_t sr_replicate_node;
79
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")
85
86 typedef enum {
87 #define _(sym,str) SR_REPLICATE_ERROR_##sym,
88   foreach_sr_replicate_error
89 #undef _
90   SR_REPLICATE_N_ERROR,
91 } sr_replicate_error_t;
92
93 static char * sr_replicate_error_strings[] = {
94 #define _(sym,string) string,
95   foreach_sr_replicate_error
96 #undef _
97 };
98
99 typedef enum {
100   SR_REPLICATE_NEXT_IP6_LOOKUP,
101   SR_REPLICATE_N_NEXT,
102 } sr_replicate_next_t;
103
104 static uword
105 sr_replicate_node_fn (vlib_main_t * vm,
106                   vlib_node_runtime_t * node,
107                   vlib_frame_t * frame)
108 {
109   u32 n_left_from, * from, * to_next;
110   sr_replicate_next_t next_index;
111   int pkts_replicated = 0;
112   ip6_sr_main_t * sm = &sr_main;
113   int no_buffer_drops = 0;
114   vlib_buffer_free_list_t * fl;
115   unsigned socket_id = rte_socket_id();
116   vlib_buffer_main_t * bm = vm->buffer_main;
117
118   from = vlib_frame_vector_args (frame);
119   n_left_from = frame->n_vectors;
120   next_index = node->cached_next_index;
121
122   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
123
124   while (n_left_from > 0)
125     {
126       u32 n_left_to_next;
127
128       vlib_get_next_frame (vm, node, next_index,
129                            to_next, n_left_to_next);
130
131       while (n_left_from > 0 && n_left_to_next > 0)
132         {
133           u32 bi0, hdr_bi0;
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;
142           int i;
143
144           bi0 = from[0];
145
146           b0 = vlib_get_buffer (vm, bi0);
147           orig_b0 = b0;
148
149           pol0 = pool_elt_at_index (sm->policies,
150                                     vnet_buffer(b0)->ip.save_protocol);
151
152           ip0 = vlib_buffer_get_current (b0);
153           /* Skip forward to the punch-in point */
154           vlib_buffer_advance (b0, sizeof(*ip0));
155
156           orig_mb0 = rte_mbuf_from_vlib_buffer (b0);
157
158           i16 delta0 = vlib_buffer_length_in_chain (vm, orig_b0)
159             - (i16) orig_mb0->pkt_len;
160
161           u16 new_data_len0 = (u16)((i16) orig_mb0->data_len + delta0);
162           u16 new_pkt_len0  = (u16)((i16) orig_mb0->pkt_len + delta0);
163
164           orig_mb0->data_len = new_data_len0;
165           orig_mb0->pkt_len = new_pkt_len0;
166           orig_mb0->data_off = (u16)(RTE_PKTMBUF_HEADROOM + b0->current_data);
167
168           /*
169             Before entering loop determine if we can allocate:
170             - all the new HEADER RTE_MBUFs and assign them to a vector
171             - all the clones
172
173             if successful, then iterate over vectors of resources
174
175            */
176           num_replicas = vec_len (pol0->tunnel_indices);
177
178           if (PREDICT_FALSE(num_replicas == 0))
179             {
180               b0->error = node->errors[SR_REPLICATE_ERROR_NO_REPLICAS];
181               goto do_trace0;
182             }
183
184           vec_reset_length (hdr_vec);
185           vec_reset_length (rte_mbuf_vec);
186
187           for (i=0; i < num_replicas; i++)
188             {
189               hdr_mb0 = rte_pktmbuf_alloc(bm->pktmbuf_pools[socket_id]);
190
191               if (i < (num_replicas - 1) )
192                 /* Not the last tunnel to process */
193                 clone0 = rte_pktmbuf_clone
194                     (orig_mb0, bm->pktmbuf_pools[socket_id]);
195               else
196                   /* Last tunnel to process, use original MB */
197                 clone0 = orig_mb0;
198               
199               
200               if (PREDICT_FALSE( !clone0 || !hdr_mb0 ))
201                 {
202                   b0->error = node->errors[SR_REPLICATE_ERROR_NO_BUFFERS];
203                   
204                   vec_foreach_index (i, rte_mbuf_vec) 
205                     { 
206                       rte_pktmbuf_free(rte_mbuf_vec[i]); 
207                     } 
208                   vec_free (rte_mbuf_vec);
209                   
210                   vec_foreach_index (i, hdr_vec) 
211                     { 
212                       rte_pktmbuf_free(hdr_vec[i]); 
213                     } 
214                   vec_free (hdr_vec);
215
216                   goto do_trace0;
217                 }
218
219               vec_add1 (hdr_vec, hdr_mb0);
220               vec_add1 (rte_mbuf_vec, clone0);
221
222             }
223
224           for (i=0; i < num_replicas; i++)
225             {
226               vlib_buffer_t * hdr_b0;
227
228               t0 = vec_elt_at_index (sm->tunnels, pol0->tunnel_indices[i]);
229
230               /* Our replicas */
231               hdr_mb0 = hdr_vec[i];
232               clone0 = rte_mbuf_vec[i];
233
234               hdr_mb0->data_len = sizeof (*ip0) + vec_len (t0->rewrite);
235               hdr_mb0->pkt_len = hdr_mb0->data_len +
236                   vlib_buffer_length_in_chain (vm, orig_b0);
237
238               hdr_b0 = vlib_buffer_from_rte_mbuf (hdr_mb0);
239
240               vlib_buffer_init_for_free_list (hdr_b0, fl);
241
242               memcpy (hdr_b0->data, ip0, sizeof (*ip0));
243               memcpy (hdr_b0->data + sizeof (*ip0), t0->rewrite,
244                       vec_len (t0->rewrite));
245
246               hdr_b0->current_data = 0;
247               hdr_b0->current_length = sizeof (*ip0) + vec_len (t0->rewrite);
248               hdr_b0->flags = orig_b0->flags | VLIB_BUFFER_NEXT_PRESENT;
249
250
251               hdr_b0->total_length_not_including_first_buffer =
252                 hdr_mb0->pkt_len - hdr_b0->current_length;
253
254               hdr_ip0 = (ip6_header_t *) hdr_b0->data;
255               hdr_ip0->payload_length = clib_host_to_net_u16(hdr_mb0->data_len);
256               hdr_sr0 = (ip6_sr_header_t *) (hdr_ip0+1);
257               hdr_sr0->protocol = hdr_ip0->protocol;
258               hdr_ip0->protocol = 43;
259
260               /* Rewrite the ip6 dst address */
261               hdr_ip0->dst_address.as_u64[0] = t0->first_hop.as_u64[0];
262               hdr_ip0->dst_address.as_u64[1] = t0->first_hop.as_u64[1];
263
264               sr_fix_hmac (sm, hdr_ip0, hdr_sr0);
265
266               /* prepend new header to invariant piece */
267               hdr_mb0->next = clone0;
268               hdr_b0->next_buffer = vlib_get_buffer_index (vm, vlib_buffer_from_rte_mbuf (clone0));
269
270               /* update header's fields */
271               hdr_mb0->pkt_len = (uint16_t)(hdr_mb0->data_len + clone0->pkt_len);
272               hdr_mb0->nb_segs = (uint8_t)(clone0->nb_segs + 1);
273
274               /* copy metadata from source packet*/
275               hdr_mb0->port = clone0->port;
276               hdr_mb0->vlan_tci = clone0->vlan_tci;
277               hdr_mb0->vlan_tci_outer = clone0->vlan_tci_outer;
278               hdr_mb0->tx_offload = clone0->tx_offload;
279               hdr_mb0->hash = clone0->hash;
280
281               hdr_mb0->ol_flags = clone0->ol_flags;
282
283               __rte_mbuf_sanity_check(hdr_mb0, 1);
284
285               hdr_bi0 = vlib_get_buffer_index (vm, hdr_b0);
286
287               to_next[0] = hdr_bi0;
288               to_next += 1;
289               n_left_to_next -= 1;
290
291               if (n_left_to_next == 0)
292                 {
293                   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
294                   vlib_get_next_frame (vm, node, next_index,
295                                        to_next, n_left_to_next);
296
297                 }
298               pkts_replicated++;
299             }
300
301           from += 1;
302           n_left_from -= 1;
303
304         do_trace0:
305           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
306             {
307               sr_replicate_trace_t *tr = vlib_add_trace (vm, node,
308                                                        b0, sizeof (*tr));
309               tr->tunnel_index = t0 - sm->tunnels;
310               if (hdr_ip0)
311                 {
312                   memcpy (tr->src.as_u8, hdr_ip0->src_address.as_u8,
313                       sizeof (tr->src.as_u8));
314                   memcpy (tr->dst.as_u8, hdr_ip0->dst_address.as_u8,
315                       sizeof (tr->dst.as_u8));
316                 }
317               if (hdr_ip0->payload_length)
318                 tr->length = clib_net_to_host_u16(hdr_ip0->payload_length);
319               else
320                 tr->length = 0;
321               tr->next_index = next_index;
322               memcpy (tr->sr, hdr_sr0, sizeof (tr->sr));
323             }
324
325         }
326
327       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
328     }
329
330   vlib_node_increment_counter (vm, sr_replicate_node.index,
331                                SR_REPLICATE_ERROR_REPLICATED, pkts_replicated);
332
333   vlib_node_increment_counter (vm, sr_replicate_node.index,
334                                SR_REPLICATE_ERROR_NO_BUFFER_DROPS, no_buffer_drops);
335
336   return frame->n_vectors;
337 }
338
339 VLIB_REGISTER_NODE (sr_replicate_node) = {
340   .function = sr_replicate_node_fn,
341   .name = "sr-replicate",
342   .vector_size = sizeof (u32),
343   .format_trace = format_sr_replicate_trace,
344   .type = VLIB_NODE_TYPE_INTERNAL,
345
346   .n_errors = ARRAY_LEN(sr_replicate_error_strings),
347   .error_strings = sr_replicate_error_strings,
348
349   .n_next_nodes = SR_REPLICATE_N_NEXT,
350
351   .next_nodes = {
352         [SR_REPLICATE_NEXT_IP6_LOOKUP] = "ip6-lookup",
353   },
354 };
355
356 clib_error_t *sr_replicate_init (vlib_main_t *vm)
357 {
358   sr_replicate_main_t *msm = &sr_replicate_main;
359
360   msm->vlib_main = vm;
361   msm->vnet_main = vnet_get_main();
362
363   return 0;
364 }
365
366 VLIB_INIT_FUNCTION(sr_replicate_init);