Initial commit of vpp code.
[vpp.git] / vnet / vnet / devices / ssvm / ssvm_eth.c
1 /*
2  * Copyright (c) 2015 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 #include "ssvm_eth.h"
16
17 ssvm_eth_main_t ssvm_eth_main;
18
19 #define foreach_ssvm_eth_tx_func_error          \
20 _(RING_FULL, "Tx packet drops (ring full)")     \
21 _(NO_BUFFERS, "Tx packet drops (no buffers)")   \
22 _(ADMIN_DOWN, "Tx packet drops (admin down)")
23
24 typedef enum {
25 #define _(f,s) SSVM_ETH_TX_ERROR_##f,
26   foreach_ssvm_eth_tx_func_error
27 #undef _
28   SSVM_ETH_TX_N_ERROR,
29 } ssvm_eth_tx_func_error_t;
30
31 static u32 ssvm_eth_flag_change (vnet_main_t * vnm, 
32                                  vnet_hw_interface_t * hi,
33                                  u32 flags);
34
35 int ssvm_eth_create (ssvm_eth_main_t * em, u8 * name, int is_master)
36 {
37   ssvm_private_t * intfc;
38   void * oldheap;
39   clib_error_t * e;
40   unix_shared_memory_queue_t * q;
41   ssvm_shared_header_t * sh;
42   ssvm_eth_queue_elt_t * elts;
43   u32 * elt_indices;
44   u8 enet_addr[6];
45   int i, rv;
46
47   vec_add2 (em->intfcs, intfc, 1);
48
49   intfc->ssvm_size = em->segment_size;
50   intfc->i_am_master = 1;
51   intfc->name = name;
52   if (is_master == 0)
53     {
54       rv = ssvm_slave_init (intfc, 20 /* timeout in seconds */);
55       if (rv < 0)
56         return rv;
57       goto create_vnet_interface;
58     }
59
60   intfc->requested_va = em->next_base_va;
61   em->next_base_va += em->segment_size;
62   rv = ssvm_master_init (intfc, intfc - em->intfcs /* master index */);
63
64   if (rv < 0)
65     return rv;
66   
67   /* OK, segment created, set up queues and so forth.  */
68   
69   sh = intfc->sh;
70   oldheap = ssvm_push_heap (sh);
71
72   q = unix_shared_memory_queue_init (em->queue_elts, sizeof (u32),
73                                      0 /* consumer pid not interesting */,
74                                      0 /* signal not sent */);
75   sh->opaque [TO_MASTER_Q_INDEX] = (void *)q;
76   q = unix_shared_memory_queue_init (em->queue_elts, sizeof (u32),
77                                      0 /* consumer pid not interesting */,
78                                      0 /* signal not sent */);
79   sh->opaque [TO_SLAVE_Q_INDEX] = (void *)q;
80   
81   /* 
82    * Preallocate the requested number of buffer chunks
83    * There must be a better way to do this, etc.
84    * Add some slop to avoid pool reallocation, which will not go well
85    */
86   elts = 0;
87   elt_indices = 0;
88
89   vec_validate_aligned (elts, em->nbuffers - 1, CLIB_CACHE_LINE_BYTES);
90   vec_validate_aligned (elt_indices, em->nbuffers - 1, CLIB_CACHE_LINE_BYTES);
91   
92   for (i = 0; i < em->nbuffers; i++)
93     elt_indices[i] = i;
94
95   sh->opaque [CHUNK_POOL_INDEX] = (void *) elts;
96   sh->opaque [CHUNK_POOL_FREELIST_INDEX] = (void *) elt_indices;
97   sh->opaque [CHUNK_POOL_NFREE] = (void *) em->nbuffers;
98   
99   ssvm_pop_heap (oldheap);
100
101  create_vnet_interface:
102
103   sh = intfc->sh;
104
105   memset (enet_addr, 0, sizeof (enet_addr));
106   enet_addr[0] = 2;
107   enet_addr[1] = 0xFE;
108   enet_addr[2] = is_master;
109   enet_addr[5] = sh->master_index;
110   
111   e = ethernet_register_interface
112     (em->vnet_main, ssvm_eth_device_class.index,
113      intfc - em->intfcs,
114      /* ethernet address */ enet_addr,
115      &intfc->vlib_hw_if_index, 
116      ssvm_eth_flag_change);
117
118   if (e)
119     {
120       clib_error_report (e);
121       /* $$$$ unmap offending region? */
122       return VNET_API_ERROR_INVALID_INTERFACE;
123     }
124
125   /* Declare link up */
126   vnet_hw_interface_set_flags (em->vnet_main, intfc->vlib_hw_if_index, 
127                                VNET_HW_INTERFACE_FLAG_LINK_UP);
128
129   /* Let the games begin... */
130   if (is_master)
131       sh->ready = 1;
132   return 0;
133 }
134
135 static clib_error_t *
136 ssvm_config (vlib_main_t * vm, unformat_input_t * input)
137 {
138   u8 * name;
139   int is_master = 1;
140   int i, rv;
141   ssvm_eth_main_t * em = &ssvm_eth_main;
142
143   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
144     {
145       if (unformat (input, "base-va %llx", &em->next_base_va))
146         ;
147       else if (unformat (input, "segment-size %lld", &em->segment_size))
148         em->segment_size = 1ULL << (max_log2 (em->segment_size));
149       else if (unformat (input, "nbuffers %lld", &em->nbuffers))
150         ;
151       else if (unformat (input, "queue-elts %lld", &em->queue_elts))
152         ;
153       else if (unformat (input, "slave"))
154         is_master = 0;
155       else if (unformat (input, "%s", &name))
156         vec_add1 (em->names, name);
157       else
158         break;
159     }
160
161   /* No configured instances, we're done... */
162   if (vec_len (em->names) == 0)
163       return 0;
164
165   for (i = 0; i < vec_len (em->names); i++)
166     {
167       rv = ssvm_eth_create (em, em->names[i], is_master);
168       if (rv < 0)
169         return clib_error_return (0, "ssvm_eth_create '%s' failed, error %d",
170                                   em->names[i], rv);
171     }
172
173   vlib_node_set_state (vm, ssvm_eth_input_node.index, VLIB_NODE_STATE_POLLING);
174
175   return 0;
176 }
177
178 VLIB_CONFIG_FUNCTION (ssvm_config, "ssvm_eth");
179
180
181 static clib_error_t * ssvm_eth_init (vlib_main_t * vm)
182 {
183   ssvm_eth_main_t * em = &ssvm_eth_main;
184
185   if (((sizeof(ssvm_eth_queue_elt_t) / CLIB_CACHE_LINE_BYTES) 
186        * CLIB_CACHE_LINE_BYTES) != sizeof(ssvm_eth_queue_elt_t))
187     clib_warning ("ssvm_eth_queue_elt_t size %d not a multiple of %d",
188                   sizeof(ssvm_eth_queue_elt_t), CLIB_CACHE_LINE_BYTES);
189
190   em->vlib_main = vm;
191   em->vnet_main = vnet_get_main();
192   em->elog_main = &vm->elog_main;
193
194   /* default config param values... */
195
196   em->next_base_va = 0x600000000ULL;
197   /* 
198    * Allocate 2 full superframes in each dir (256 x 2 x 2 x 2048 bytes),
199    * 2mb; double that so we have plenty of space... 4mb
200    */
201   em->segment_size = 8<<20;
202   em->nbuffers = 1024;
203   em->queue_elts = 512;
204   return 0;
205 }
206
207 VLIB_INIT_FUNCTION (ssvm_eth_init);
208
209 static char * ssvm_eth_tx_func_error_strings[] = {
210 #define _(n,s) s,
211     foreach_ssvm_eth_tx_func_error
212 #undef _
213 };
214
215 static u8 * format_ssvm_eth_device_name (u8 * s, va_list * args)
216 {
217   u32 i = va_arg (*args, u32);
218
219   s = format (s, "ssvmEthernet%d", i);
220   return s;
221 }
222
223 static u8 * format_ssvm_eth_device (u8 * s, va_list * args)
224 {
225   s = format (s, "SSVM Ethernet");
226   return s;
227 }
228
229 static u8 * format_ssvm_eth_tx_trace (u8 * s, va_list * args)
230 {
231   s = format (s, "Unimplemented...");
232   return s;
233 }
234
235
236 static uword
237 ssvm_eth_interface_tx (vlib_main_t * vm,
238                        vlib_node_runtime_t * node,
239                        vlib_frame_t * f)
240 {
241   ssvm_eth_main_t * em = &ssvm_eth_main;
242   vnet_interface_output_runtime_t * rd = (void *) node->runtime_data;
243   ssvm_private_t * intfc = vec_elt_at_index (em->intfcs, rd->dev_instance);
244   ssvm_shared_header_t * sh = intfc->sh;
245   unix_shared_memory_queue_t * q;
246   u32 * from;
247   u32 n_left;
248   ssvm_eth_queue_elt_t * elts, * elt, * prev_elt;
249   u32 my_pid = intfc->my_pid;
250   vlib_buffer_t * b0;
251   u32 bi0;
252   u32 size_this_buffer;
253   u32 chunks_this_buffer;
254   u8 i_am_master = intfc->i_am_master;
255   u32 elt_index;
256   int is_ring_full, interface_down;
257   int i;
258   volatile u32 *queue_lock;
259   u32 n_to_alloc = VLIB_FRAME_SIZE;
260   u32 n_allocated, n_present_in_cache, n_available;
261   u32 * elt_indices;
262   
263   if (i_am_master)
264     q = (unix_shared_memory_queue_t *)sh->opaque [TO_SLAVE_Q_INDEX];
265   else
266     q = (unix_shared_memory_queue_t *)sh->opaque [TO_MASTER_Q_INDEX];
267
268   queue_lock = (u32 *) q;
269
270   from = vlib_frame_vector_args (f);
271   n_left = f->n_vectors;
272   is_ring_full = 0;
273   interface_down = 0;
274
275   n_present_in_cache = vec_len (em->chunk_cache);
276
277   /* admin / link up/down check */
278   if ((u64)(sh->opaque [MASTER_ADMIN_STATE_INDEX]) == 0 ||
279       (u64)(sh->opaque [SLAVE_ADMIN_STATE_INDEX]) == 0)
280     {
281       interface_down = 1;
282       goto out;
283     }
284
285   ssvm_lock (sh, my_pid, 1);
286
287   elts = (ssvm_eth_queue_elt_t *) (sh->opaque [CHUNK_POOL_INDEX]);
288   elt_indices = (u32 *) (sh->opaque [CHUNK_POOL_FREELIST_INDEX]);
289   n_available = (u32) (u64) (sh->opaque [CHUNK_POOL_NFREE]);
290
291   if (n_present_in_cache < n_left*2)
292     {
293       vec_validate (em->chunk_cache, 
294                     n_to_alloc + n_present_in_cache - 1);
295
296       n_allocated = n_to_alloc < n_available ? n_to_alloc : n_available;
297
298       if (PREDICT_TRUE(n_allocated > 0))
299         {
300           memcpy (&em->chunk_cache[n_present_in_cache],
301                   &elt_indices[n_available - n_allocated],
302                   sizeof(u32) * n_allocated);
303         }
304
305       n_present_in_cache += n_allocated;
306       n_available -= n_allocated;
307       sh->opaque [CHUNK_POOL_NFREE] = (void *) (u64) n_available;
308       _vec_len (em->chunk_cache) = n_present_in_cache;
309     }
310
311   ssvm_unlock (sh);
312
313   while (n_left)
314     {
315       bi0 = from[0];
316       b0 = vlib_get_buffer (vm, bi0);
317       
318       size_this_buffer = vlib_buffer_length_in_chain (vm, b0);
319       chunks_this_buffer = (size_this_buffer + (SSVM_BUFFER_SIZE - 1))
320         / SSVM_BUFFER_SIZE;
321
322       /* If we're not going to be able to enqueue the buffer, tail drop. */
323       if (q->cursize >= q->maxsize)
324         {
325           is_ring_full = 1;
326           break;
327         }
328
329       prev_elt = 0;
330       elt_index = ~0;
331       for (i = 0; i < chunks_this_buffer; i++)
332         {
333           if (PREDICT_FALSE (n_present_in_cache == 0))
334             goto out;
335
336           elt_index = em->chunk_cache[--n_present_in_cache];
337           elt = elts + elt_index;
338
339           elt->type = SSVM_PACKET_TYPE;
340           elt->flags = 0;
341           elt->total_length_not_including_first_buffer = 
342             b0->total_length_not_including_first_buffer;
343           elt->length_this_buffer = b0->current_length;
344           elt->current_data_hint = b0->current_data;
345           elt->owner = !i_am_master;
346           elt->tag = 1;
347           
348           memcpy (elt->data, b0->data + b0->current_data, b0->current_length);
349           
350           if (PREDICT_FALSE (prev_elt != 0))
351             prev_elt->next_index = elt - elts;
352             
353           if (PREDICT_FALSE(i < (chunks_this_buffer-1)))
354             {
355               elt->flags = SSVM_BUFFER_NEXT_PRESENT;
356               ASSERT (b0->flags & VLIB_BUFFER_NEXT_PRESENT);
357               b0 = vlib_get_buffer (vm, b0->next_buffer);
358             }
359           prev_elt = elt;
360         }
361
362       while (__sync_lock_test_and_set (queue_lock, 1))
363           ;
364       
365       unix_shared_memory_queue_add_raw (q, (u8 *)&elt_index);
366       CLIB_MEMORY_BARRIER();
367       *queue_lock = 0;
368
369       from++;
370       n_left--;
371     }
372
373  out:
374   if (PREDICT_FALSE(n_left))
375     {
376       if (is_ring_full)
377         vlib_error_count (vm, node->node_index, SSVM_ETH_TX_ERROR_RING_FULL, 
378                           n_left);
379       else if (interface_down)
380         vlib_error_count (vm, node->node_index, SSVM_ETH_TX_ERROR_ADMIN_DOWN, 
381                           n_left);
382       else
383         vlib_error_count (vm, node->node_index, SSVM_ETH_TX_ERROR_NO_BUFFERS,
384                           n_left);
385
386       vlib_buffer_free (vm, from, n_left);
387     }
388   else
389       vlib_buffer_free (vm, vlib_frame_vector_args (f), f->n_vectors);
390
391   if (PREDICT_TRUE(vec_len(em->chunk_cache)))
392       _vec_len(em->chunk_cache) = n_present_in_cache;
393
394   return f->n_vectors;
395 }
396
397 static void ssvm_eth_clear_hw_interface_counters (u32 instance)
398 {
399   /* Nothing for now */
400 }
401
402 static clib_error_t *
403 ssvm_eth_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
404 {
405   vnet_hw_interface_t * hif = vnet_get_hw_interface (vnm, hw_if_index);
406   uword is_up = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
407   ssvm_eth_main_t * em = &ssvm_eth_main;
408   ssvm_private_t * intfc = vec_elt_at_index (em->intfcs, hif->dev_instance);
409   ssvm_shared_header_t * sh;
410
411   /* publish link-state in shared-memory, to discourage buffer-wasting */
412   sh = intfc->sh;
413   if (intfc->i_am_master)
414     sh->opaque [MASTER_ADMIN_STATE_INDEX] = (void *) is_up;
415   else
416     sh->opaque [SLAVE_ADMIN_STATE_INDEX] = (void *) is_up;
417     
418   return 0;
419 }
420
421 static clib_error_t *
422 ssvm_eth_subif_add_del_function (vnet_main_t * vnm,
423                                  u32 hw_if_index,
424                                  struct vnet_sw_interface_t * st,
425                                  int is_add)
426 {
427   /* Nothing for now */
428   return 0;
429 }
430
431 /*
432  * Dynamically redirect all pkts from a specific interface
433  * to the specified node
434  */
435 static void 
436 ssvm_eth_set_interface_next_node (vnet_main_t *vnm, u32 hw_if_index,
437                                   u32 node_index)
438 {
439   ssvm_eth_main_t * em = &ssvm_eth_main;
440   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
441   ssvm_private_t * intfc = pool_elt_at_index (em->intfcs, hw->dev_instance);
442   
443   /* Shut off redirection */
444   if (node_index == ~0)
445     {
446       intfc->per_interface_next_index = node_index;
447       return;
448     }
449   
450   intfc->per_interface_next_index = 
451     vlib_node_add_next (em->vlib_main, ssvm_eth_input_node.index, node_index);
452 }
453
454 static u32 ssvm_eth_flag_change (vnet_main_t * vnm, 
455                                  vnet_hw_interface_t * hi,
456                                  u32 flags)
457 {
458     /* nothing for now */
459     return 0;
460 }
461
462 VNET_DEVICE_CLASS (ssvm_eth_device_class) = {
463   .name = "ssvm-eth",
464   .tx_function = ssvm_eth_interface_tx,
465   .tx_function_n_errors = SSVM_ETH_TX_N_ERROR,
466   .tx_function_error_strings = ssvm_eth_tx_func_error_strings,
467   .format_device_name = format_ssvm_eth_device_name,
468   .format_device = format_ssvm_eth_device,
469   .format_tx_trace = format_ssvm_eth_tx_trace,
470   .clear_counters = ssvm_eth_clear_hw_interface_counters,
471   .admin_up_down_function = ssvm_eth_interface_admin_up_down,
472   .subif_add_del_function = ssvm_eth_subif_add_del_function,
473   .rx_redirect_to_node = ssvm_eth_set_interface_next_node,
474   .no_flatten_output_chains = 1,
475 };