2 * Copyright (c) 2018 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/devices/pipe/pipe.h>
18 #include <vppinfra/sparse_vec.h>
22 * @brief Pipe Interfaces.
24 * A pipe interface, like the UNIX pipe, is a pair of vpp interfaces
27 const static pipe_t PIPE_INVALID = {
33 * Various 'module' level variables
35 typedef struct pipe_main_t_
38 * Allocated pipe instances
43 * the per-swif-index array of pipes. Each end of the pipe is stored against
44 * its respective sw_if_index
49 static pipe_main_t pipe_main;
52 * The pipe rewrite is the same size as an ethernet header (since it
53 * is an ethernet interface and the DP is optimised for writing
54 * sizeof(ethernet_header_t) rewrites. However, there are no MAC addresses
55 * since pipes don't have them.
58 pipe_build_rewrite (vnet_main_t * vnm,
60 vnet_link_t link_type, const void *dst_address)
68 #define _(a,b) case VNET_LINK_##a: type = ETHERNET_TYPE_##b; break
78 vec_validate (rewrite, sizeof (ethernet_header_t));
80 h = (ethernet_header_t *) rewrite;
81 h->type = clib_host_to_net_u16 (type);
87 VNET_HW_INTERFACE_CLASS (pipe_hw_interface_class) = {
89 .build_rewrite = pipe_build_rewrite,
90 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
95 pipe_get (u32 sw_if_index)
97 vec_validate_init_empty (pipe_main.pipes, sw_if_index, PIPE_INVALID);
99 return (&pipe_main.pipes[sw_if_index]);
103 unformat_pipe_interface (unformat_input_t * input, va_list * args)
105 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
106 u32 *result = va_arg (*args, u32 *);
108 ethernet_main_t *em = ðernet_main;
109 ethernet_interface_t *eif;
111 if (!unformat_user (input, unformat_vnet_hw_interface, vnm, &hw_if_index))
114 eif = ethernet_get_interface (em, hw_if_index);
117 *result = hw_if_index;
123 #define VNET_PIPE_TX_NEXT_ETHERNET_INPUT VNET_INTERFACE_TX_N_NEXT
126 * The TX function bounces the packets back to pipe-rx with the TX interface
130 pipe_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
132 u32 n_left_from, n_left_to_next, n_copy, *from, *to_next;
133 u32 next_index = VNET_PIPE_TX_NEXT_ETHERNET_INPUT;
134 u32 i, sw_if_index = 0;
138 n_left_from = frame->n_vectors;
139 from = vlib_frame_vector_args (frame);
141 while (n_left_from > 0)
143 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
145 n_copy = clib_min (n_left_from, n_left_to_next);
147 clib_memcpy_fast (to_next, from, n_copy * sizeof (from[0]));
148 n_left_to_next -= n_copy;
149 n_left_from -= n_copy;
153 b = vlib_get_buffer (vm, from[i]);
154 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
156 pipe = &pipe_main.pipes[sw_if_index];
157 // Set up RX index to be recv'd by the other end of the pipe
158 vnet_buffer (b)->sw_if_index[VLIB_RX] = pipe->sw_if_index;
159 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
165 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
168 return frame->n_vectors;
172 format_pipe_name (u8 * s, va_list * args)
174 u32 dev_instance = va_arg (*args, u32);
175 return format (s, "pipe%d", dev_instance);
178 static clib_error_t *
179 pipe_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
181 vnet_hw_interface_t *hi;
184 u32 hw_flags = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
185 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
186 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
189 hi = vnet_get_hw_interface (vnm, hw_if_index);
190 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
192 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
200 VNET_DEVICE_CLASS (pipe_device_class) = {
202 .format_device_name = format_pipe_name,
203 .tx_function = pipe_tx,
204 .admin_up_down_function = pipe_admin_up_down,
208 #define foreach_pipe_rx_next \
209 _ (DROP, "error-drop")
211 typedef enum pipe_rx_next_t_
213 #define _(s,n) PIPE_RX_NEXT_##s,
219 typedef struct pipe_rx_trace_t_
225 format_pipe_rx_trace (u8 * s, va_list * va)
227 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
228 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
229 pipe_rx_trace_t *t = va_arg (*va, pipe_rx_trace_t *);
231 s = format (s, "%U", format_ethernet_header, t->packet_data);
237 * The pipe-rx node is a sibling of ethernet-input so steal it's
238 * next node mechanism
240 static_always_inline void
241 pipe_determine_next_node (ethernet_main_t * em,
244 vlib_buffer_t * b0, pipe_rx_next_t * next0)
248 *next0 = em->l2_next;
250 else if (type0 == ETHERNET_TYPE_IP4)
252 *next0 = em->l3_next.input_next_ip4;
254 else if (type0 == ETHERNET_TYPE_IP6)
256 *next0 = em->l3_next.input_next_ip6;
258 else if (type0 == ETHERNET_TYPE_MPLS)
260 *next0 = em->l3_next.input_next_mpls;
263 else if (em->redirect_l3)
265 // L3 Redirect is on, the cached common next nodes will be
266 // pointing to the redirect node, catch the uncommon types here
267 *next0 = em->redirect_l3_next;
271 // uncommon ethertype, check table
273 i0 = sparse_vec_index (em->l3_next.input_next_by_type, type0);
274 *next0 = vec_elt (em->l3_next.input_next_by_type, i0);
276 // The table is not populated with LLC values, so check that now.
279 *next0 = PIPE_RX_NEXT_DROP;
284 static_always_inline uword
285 pipe_rx (vlib_main_t * vm,
286 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
288 u32 n_left_from, next_index, *from, *to_next;
291 from = vlib_frame_vector_args (from_frame);
292 n_left_from = from_frame->n_vectors;
294 if (node->flags & VLIB_NODE_FLAG_TRACE)
295 vlib_trace_frame_buffers_only (vm, node,
299 sizeof (pipe_rx_trace_t));
301 next_index = node->cached_next_index;
303 while (n_left_from > 0)
305 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
307 while (n_left_from >= 4 && n_left_to_next >= 2)
309 u32 bi0, sw_if_index0, bi1, sw_if_index1;
310 pipe_rx_next_t next0, next1;
311 ethernet_header_t *e0, *e1;
312 vlib_buffer_t *b0, *b1;
313 pipe_t *pipe0, *pipe1;
317 // Prefetch next iteration
319 vlib_buffer_t *p2, *p3;
321 p2 = vlib_get_buffer (vm, from[2]);
322 p3 = vlib_get_buffer (vm, from[3]);
323 vlib_prefetch_buffer_header (p2, STORE);
324 vlib_prefetch_buffer_header (p3, STORE);
325 clib_prefetch_load (p2->data);
326 clib_prefetch_load (p3->data);
338 b0 = vlib_get_buffer (vm, bi0);
339 b1 = vlib_get_buffer (vm, bi1);
341 e0 = vlib_buffer_get_current (b0);
342 e1 = vlib_buffer_get_current (b1);
343 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
344 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
345 type0 = clib_net_to_host_u16 (e0->type);
346 type1 = clib_net_to_host_u16 (e1->type);
347 pipe0 = &pipe_main.pipes[sw_if_index0];
348 pipe1 = &pipe_main.pipes[sw_if_index1];
350 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
351 vnet_buffer (b1)->l2_hdr_offset = b1->current_data;
353 vnet_buffer (b0)->l3_hdr_offset =
354 vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
355 vnet_buffer (b1)->l3_hdr_offset =
356 vnet_buffer (b1)->l2_hdr_offset + sizeof (ethernet_header_t);
358 VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
359 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
361 VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
362 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
364 is_l20 = pipe0->subint.flags & SUBINT_CONFIG_L2;
365 is_l21 = pipe1->subint.flags & SUBINT_CONFIG_L2;
368 * from discussion with Neale - we do not support the tagged traffic.
369 * So assume a simple ethernet header
371 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
372 vnet_buffer (b1)->l2.l2_len = sizeof (ethernet_header_t);
373 vlib_buffer_advance (b0, is_l20 ? 0 : sizeof (ethernet_header_t));
374 vlib_buffer_advance (b1, is_l21 ? 0 : sizeof (ethernet_header_t));
376 pipe_determine_next_node (ðernet_main, is_l20, type0, b0,
378 pipe_determine_next_node (ðernet_main, is_l21, type1, b1,
381 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
382 to_next, n_left_to_next,
383 bi0, bi1, next0, next1);
385 while (n_left_from > 0 && n_left_to_next > 0)
387 u32 bi0, sw_if_index0;
389 pipe_rx_next_t next0;
390 ethernet_header_t *e0;
402 b0 = vlib_get_buffer (vm, bi0);
404 e0 = vlib_buffer_get_current (b0);
405 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
406 type0 = clib_net_to_host_u16 (e0->type);
407 pipe0 = &pipe_main.pipes[sw_if_index0];
409 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
410 vnet_buffer (b0)->l3_hdr_offset =
411 vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
413 VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
414 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
416 is_l20 = pipe0->subint.flags & SUBINT_CONFIG_L2;
418 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
419 vlib_buffer_advance (b0, is_l20 ? 0 : sizeof (ethernet_header_t));
421 pipe_determine_next_node (ðernet_main, is_l20, type0, b0,
424 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
425 to_next, n_left_to_next,
429 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
432 return from_frame->n_vectors;
436 VLIB_REGISTER_NODE (pipe_rx_node) = {
439 /* Takes a vector of packets. */
440 .vector_size = sizeof (u32),
441 .format_trace = format_pipe_rx_trace,
443 .sibling_of = "ethernet-input",
448 * Maintain a bitmap of allocated pipe instance numbers.
450 #define PIPE_MAX_INSTANCE (16 * 1024)
453 pipe_instance_alloc (u8 is_specified, u32 want)
456 * Check for dynamically allocaetd instance number.
462 bit = clib_bitmap_first_clear (pipe_main.instances);
463 if (bit >= PIPE_MAX_INSTANCE)
467 pipe_main.instances = clib_bitmap_set (pipe_main.instances, bit, 1);
474 if (want >= PIPE_MAX_INSTANCE)
482 if (clib_bitmap_get (pipe_main.instances, want))
488 * Grant allocation request.
490 pipe_main.instances = clib_bitmap_set (pipe_main.instances, want, 1);
496 pipe_instance_free (u32 instance)
498 if (instance >= PIPE_MAX_INSTANCE)
503 if (clib_bitmap_get (pipe_main.instances, instance) == 0)
508 pipe_main.instances = clib_bitmap_set (pipe_main.instances, instance, 0);
512 static clib_error_t *
513 pipe_create_sub_interface (vnet_hw_interface_t * hi,
514 u32 sub_id, u32 * sw_if_index)
516 vnet_sw_interface_t template;
518 clib_memset (&template, 0, sizeof (template));
519 template.type = VNET_SW_INTERFACE_TYPE_PIPE;
520 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
521 template.sup_sw_if_index = hi->sw_if_index;
522 template.sub.id = sub_id;
524 return (vnet_create_sw_interface (vnet_get_main (),
525 &template, sw_if_index));
529 vnet_create_pipe_interface (u8 is_specified,
531 u32 * parent_sw_if_index, u32 pipe_sw_if_index[2])
533 vnet_main_t *vnm = vnet_get_main ();
534 vlib_main_t *vm = vlib_get_main ();
535 vnet_eth_interface_registration_t eir = {};
540 vnet_hw_interface_t *hi;
547 ASSERT (parent_sw_if_index);
549 clib_memset (address, 0, sizeof (address));
552 * Allocate a pipe instance. Either select one dynamically
553 * or try to use the desired user_instance number.
555 instance = pipe_instance_alloc (is_specified, user_instance);
558 return VNET_API_ERROR_INVALID_REGISTRATION;
562 * Default MAC address (0000:0000:0000 + instance) is allocated
564 address[5] = instance;
566 eir.dev_class_index = pipe_device_class.index;
567 eir.dev_instance = instance;
568 eir.address = address;
569 hw_if_index = vnet_eth_register_interface (vnm, &eir);
571 hi = vnet_get_hw_interface (vnm, hw_if_index);
572 *parent_sw_if_index = hi->sw_if_index;
573 slot = vlib_node_add_named_next_with_slot (vm, hi->tx_node_index,
575 VNET_PIPE_TX_NEXT_ETHERNET_INPUT);
576 ASSERT (slot == VNET_PIPE_TX_NEXT_ETHERNET_INPUT);
579 * create two sub-interfaces, one for each end of the pipe.
581 error = pipe_create_sub_interface (hi, 0, &pipe_sw_if_index[0]);
586 error = pipe_create_sub_interface (hi, 1, &pipe_sw_if_index[1]);
591 hash_set (hi->sub_interface_sw_if_index_by_id, 0, pipe_sw_if_index[0]);
592 hash_set (hi->sub_interface_sw_if_index_by_id, 1, pipe_sw_if_index[1]);
594 vec_validate_init_empty (pipe_main.pipes, pipe_sw_if_index[0],
596 vec_validate_init_empty (pipe_main.pipes, pipe_sw_if_index[1],
599 pipe_main.pipes[pipe_sw_if_index[0]].sw_if_index = pipe_sw_if_index[1];
600 pipe_main.pipes[pipe_sw_if_index[1]].sw_if_index = pipe_sw_if_index[0];
605 clib_error_report (error);
609 typedef struct pipe_hw_walk_ctx_t_
613 } pipe_hw_walk_ctx_t;
616 pipe_hw_walk (vnet_main_t * vnm, u32 hw_if_index, void *args)
618 vnet_hw_interface_t *hi;
619 pipe_hw_walk_ctx_t *ctx;
622 hi = vnet_get_hw_interface (vnm, hw_if_index);
624 if (hi->dev_class_index == pipe_device_class.index)
626 u32 pipe_sw_if_index[2], id, sw_if_index;
629 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
632 pipe_sw_if_index[id] = sw_if_index;
636 ctx->cb (hi->sw_if_index, pipe_sw_if_index, hi->dev_instance, ctx->ctx);
639 return (WALK_CONTINUE);
643 pipe_walk (pipe_cb_fn_t fn, void *ctx)
645 pipe_hw_walk_ctx_t wctx = {
652 vnet_hw_interface_walk (vnet_get_main (), pipe_hw_walk, &wctx);
655 static clib_error_t *
656 create_pipe_interfaces (vlib_main_t * vm,
657 unformat_input_t * input, vlib_cli_command_t * cmd)
661 u32 pipe_sw_if_index[2];
663 u32 user_instance = 0;
665 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
667 if (unformat (input, "instance %d", &user_instance))
673 rv = vnet_create_pipe_interface (is_specified, user_instance,
674 &sw_if_index, pipe_sw_if_index);
677 return clib_error_return (0, "vnet_create_pipe_interface failed");
679 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
680 vnet_get_main (), sw_if_index);
685 * Create a pipe interface.
688 * The following two command syntaxes are equivalent:
689 * @cliexcmd{pipe create-interface [mac <mac-addr>] [instance <instance>]}
690 * Example of how to create a pipe interface:
691 * @cliexcmd{pipe create}
694 VLIB_CLI_COMMAND (pipe_create_interface_command, static) = {
695 .path = "pipe create",
696 .short_help = "pipe create [instance <instance>]",
697 .function = create_pipe_interfaces,
702 vnet_delete_pipe_interface (u32 sw_if_index)
704 vnet_main_t *vnm = vnet_get_main ();
705 vnet_sw_interface_t *si;
706 vnet_hw_interface_t *hi;
710 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
711 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
713 si = vnet_get_sw_interface (vnm, sw_if_index);
714 hw_if_index = si->hw_if_index;
715 hi = vnet_get_hw_interface (vnm, hw_if_index);
716 instance = hi->dev_instance;
718 if (pipe_instance_free (instance) < 0)
720 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
724 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
726 vnet_delete_sub_interface(sw_if_index);
727 pipe_main.pipes[sw_if_index] = PIPE_INVALID;
731 ethernet_delete_interface (vnm, hw_if_index);
736 static clib_error_t *
737 delete_pipe_interfaces (vlib_main_t * vm,
738 unformat_input_t * input, vlib_cli_command_t * cmd)
740 vnet_main_t *vnm = vnet_get_main ();
741 u32 sw_if_index = ~0;
744 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
746 if (unformat (input, "%U",
747 unformat_vnet_sw_interface, vnm, &sw_if_index))
753 if (sw_if_index == ~0)
754 return clib_error_return (0, "interface not specified");
756 rv = vnet_delete_pipe_interface (sw_if_index);
759 return clib_error_return (0, "vnet_delete_pipe_interface failed");
765 * Delete a pipe interface.
768 * The following two command syntaxes are equivalent:
769 * @cliexcmd{pipe delete intfc <interface>}
770 * Example of how to delete a pipe interface:
771 * @cliexcmd{pipe delete-interface intfc loop0}
774 VLIB_CLI_COMMAND (pipe_delete_interface_command, static) = {
775 .path = "pipe delete",
776 .short_help = "pipe delete <interface>",
777 .function = delete_pipe_interfaces,
782 * fd.io coding-style-patch-verification: ON
785 * eval: (c-set-style "gnu")