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 interfaces
27 const static pipe_t PIPE_INVALID = {
33 * Various 'module' lavel 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 againt
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) rewirtes. Hwoever, 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;
135 u32 n_pkts = 0, n_bytes = 0;
136 u32 thread_index = vm->thread_index;
137 vnet_main_t *vnm = vnet_get_main ();
138 vnet_interface_main_t *im = &vnm->interface_main;
142 n_left_from = frame->n_vectors;
143 from = vlib_frame_args (frame);
145 while (n_left_from > 0)
147 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
149 n_copy = clib_min (n_left_from, n_left_to_next);
151 clib_memcpy (to_next, from, n_copy * sizeof (from[0]));
152 n_left_to_next -= n_copy;
153 n_left_from -= n_copy;
157 b = vlib_get_buffer (vm, from[i]);
158 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
160 pipe = &pipe_main.pipes[sw_if_index];
161 // Set up RX index to be recv'd by the other end of the pipe
162 vnet_buffer (b)->sw_if_index[VLIB_RX] = pipe->sw_if_index;
163 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
167 n_bytes += vlib_buffer_length_in_chain (vm, b);
171 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
173 /* increment TX interface stat */
174 vlib_increment_combined_counter (im->combined_sw_if_counters +
175 VNET_INTERFACE_COUNTER_TX,
176 thread_index, sw_if_index, n_pkts,
184 format_pipe_name (u8 * s, va_list * args)
186 u32 dev_instance = va_arg (*args, u32);
187 return format (s, "pipe%d", dev_instance);
190 static clib_error_t *
191 pipe_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
193 vnet_hw_interface_t *hi;
196 u32 hw_flags = ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
197 VNET_HW_INTERFACE_FLAG_LINK_UP : 0);
198 vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
201 hi = vnet_get_hw_interface (vnm, hw_if_index);
202 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
204 vnet_sw_interface_set_flags (vnm, sw_if_index, flags);
212 VNET_DEVICE_CLASS (pipe_device_class) = {
214 .format_device_name = format_pipe_name,
215 .tx_function = pipe_tx,
216 .admin_up_down_function = pipe_admin_up_down,
220 #define foreach_pipe_rx_next \
221 _ (DROP, "error-drop")
223 typedef enum pipe_rx_next_t_
225 #define _(s,n) PIPE_RX_NEXT_##s,
231 typedef struct pipe_rx_trace_t_
237 format_pipe_rx_trace (u8 * s, va_list * va)
239 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
240 CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
241 pipe_rx_trace_t *t = va_arg (*va, pipe_rx_trace_t *);
243 s = format (s, "%U", format_ethernet_header, t->packet_data);
249 * The pipe-rx node is a sibling of ethernet-input so steal it's
250 * next node mechanism
252 static_always_inline void
253 pipe_determine_next_node (ethernet_main_t * em,
256 vlib_buffer_t * b0, pipe_rx_next_t * next0)
260 *next0 = em->l2_next;
262 else if (type0 == ETHERNET_TYPE_IP4)
264 *next0 = em->l3_next.input_next_ip4;
266 else if (type0 == ETHERNET_TYPE_IP6)
268 *next0 = em->l3_next.input_next_ip6;
270 else if (type0 == ETHERNET_TYPE_MPLS)
272 *next0 = em->l3_next.input_next_mpls;
275 else if (em->redirect_l3)
277 // L3 Redirect is on, the cached common next nodes will be
278 // pointing to the redirect node, catch the uncommon types here
279 *next0 = em->redirect_l3_next;
283 // uncommon ethertype, check table
285 i0 = sparse_vec_index (em->l3_next.input_next_by_type, type0);
286 *next0 = vec_elt (em->l3_next.input_next_by_type, i0);
288 // The table is not populated with LLC values, so check that now.
291 *next0 = PIPE_RX_NEXT_DROP;
296 static_always_inline uword
297 pipe_rx (vlib_main_t * vm,
298 vlib_node_runtime_t * node, vlib_frame_t * from_frame)
300 u32 n_left_from, next_index, *from, *to_next;
303 from = vlib_frame_vector_args (from_frame);
304 n_left_from = from_frame->n_vectors;
306 if (node->flags & VLIB_NODE_FLAG_TRACE)
307 vlib_trace_frame_buffers_only (vm, node,
311 sizeof (pipe_rx_trace_t));
313 next_index = node->cached_next_index;
315 while (n_left_from > 0)
317 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
319 while (n_left_from >= 4 && n_left_to_next >= 2)
321 u32 bi0, sw_if_index0, bi1, sw_if_index1;
322 pipe_rx_next_t next0, next1;
323 ethernet_header_t *e0, *e1;
324 vlib_buffer_t *b0, *b1;
325 pipe_t *pipe0, *pipe1;
329 // Prefetch next iteration
331 vlib_buffer_t *p2, *p3;
333 p2 = vlib_get_buffer (vm, from[2]);
334 p3 = vlib_get_buffer (vm, from[3]);
335 vlib_prefetch_buffer_header (p2, STORE);
336 vlib_prefetch_buffer_header (p3, STORE);
337 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, LOAD);
338 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, LOAD);
350 b0 = vlib_get_buffer (vm, bi0);
351 b1 = vlib_get_buffer (vm, bi1);
353 e0 = vlib_buffer_get_current (b0);
354 e1 = vlib_buffer_get_current (b1);
355 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
356 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
357 type0 = clib_net_to_host_u16 (e0->type);
358 type1 = clib_net_to_host_u16 (e1->type);
359 pipe0 = &pipe_main.pipes[sw_if_index0];
360 pipe1 = &pipe_main.pipes[sw_if_index1];
362 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
363 vnet_buffer (b1)->l2_hdr_offset = b1->current_data;
365 vnet_buffer (b0)->l3_hdr_offset =
366 vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
367 vnet_buffer (b1)->l3_hdr_offset =
368 vnet_buffer (b1)->l2_hdr_offset + sizeof (ethernet_header_t);
370 VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
371 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
373 VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
374 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
376 is_l20 = pipe0->subint.flags & SUBINT_CONFIG_L2;
377 is_l21 = pipe1->subint.flags & SUBINT_CONFIG_L2;
380 * from discussion with Neale - we do not support the tagged traffic.
381 * So assume a simple ethernet header
383 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
384 vnet_buffer (b1)->l2.l2_len = sizeof (ethernet_header_t);
385 vlib_buffer_advance (b0, is_l20 ? 0 : sizeof (ethernet_header_t));
386 vlib_buffer_advance (b1, is_l21 ? 0 : sizeof (ethernet_header_t));
388 pipe_determine_next_node (ðernet_main, is_l20, type0, b0,
390 pipe_determine_next_node (ðernet_main, is_l21, type1, b1,
393 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
394 to_next, n_left_to_next,
395 bi0, bi1, next0, next1);
397 while (n_left_from > 0 && n_left_to_next > 0)
399 u32 bi0, sw_if_index0;
401 pipe_rx_next_t next0;
402 ethernet_header_t *e0;
414 b0 = vlib_get_buffer (vm, bi0);
416 e0 = vlib_buffer_get_current (b0);
417 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
418 type0 = clib_net_to_host_u16 (e0->type);
419 pipe0 = &pipe_main.pipes[sw_if_index0];
421 vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
422 vnet_buffer (b0)->l3_hdr_offset =
423 vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
425 VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
426 VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
428 is_l20 = pipe0->subint.flags & SUBINT_CONFIG_L2;
430 vnet_buffer (b0)->l2.l2_len = sizeof (ethernet_header_t);
431 vlib_buffer_advance (b0, is_l20 ? 0 : sizeof (ethernet_header_t));
433 pipe_determine_next_node (ðernet_main, is_l20, type0, b0,
436 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
437 to_next, n_left_to_next,
441 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
444 return from_frame->n_vectors;
448 VLIB_REGISTER_NODE (pipe_rx_node) = {
451 /* Takes a vector of packets. */
452 .vector_size = sizeof (u32),
453 .format_trace = format_pipe_rx_trace,
455 .sibling_of = "ethernet-input",
460 * Maintain a bitmap of allocated pipe instance numbers.
462 #define PIPE_MAX_INSTANCE (16 * 1024)
465 pipe_instance_alloc (u8 is_specified, u32 want)
468 * Check for dynamically allocaetd instance number.
474 bit = clib_bitmap_first_clear (pipe_main.instances);
475 if (bit >= PIPE_MAX_INSTANCE)
479 pipe_main.instances = clib_bitmap_set (pipe_main.instances, bit, 1);
486 if (want >= PIPE_MAX_INSTANCE)
494 if (clib_bitmap_get (pipe_main.instances, want))
500 * Grant allocation request.
502 pipe_main.instances = clib_bitmap_set (pipe_main.instances, want, 1);
508 pipe_instance_free (u32 instance)
510 if (instance >= PIPE_MAX_INSTANCE)
515 if (clib_bitmap_get (pipe_main.instances, instance) == 0)
520 pipe_main.instances = clib_bitmap_set (pipe_main.instances, instance, 0);
524 static clib_error_t *
525 pipe_create_sub_interface (vnet_hw_interface_t * hi,
526 u32 sub_id, u32 * sw_if_index)
528 vnet_sw_interface_t template;
530 memset (&template, 0, sizeof (template));
531 template.type = VNET_SW_INTERFACE_TYPE_PIPE;
532 template.flood_class = VNET_FLOOD_CLASS_NORMAL;
533 template.sup_sw_if_index = hi->sw_if_index;
534 template.sub.id = sub_id;
536 return (vnet_create_sw_interface (vnet_get_main (),
537 &template, sw_if_index));
541 vnet_create_pipe_interface (u8 is_specified,
543 u32 * parent_sw_if_index, u32 pipe_sw_if_index[2])
545 vnet_main_t *vnm = vnet_get_main ();
546 vlib_main_t *vm = vlib_get_main ();
551 vnet_hw_interface_t *hi;
558 ASSERT (parent_sw_if_index);
560 memset (address, 0, sizeof (address));
563 * Allocate a pipe instance. Either select one dynamically
564 * or try to use the desired user_instance number.
566 instance = pipe_instance_alloc (is_specified, user_instance);
569 return VNET_API_ERROR_INVALID_REGISTRATION;
573 * Default MAC address (0000:0000:0000 + instance) is allocated
575 address[5] = instance;
577 error = ethernet_register_interface (vnm, pipe_device_class.index,
578 instance, address, &hw_if_index,
579 /* flag change */ 0);
583 rv = VNET_API_ERROR_INVALID_REGISTRATION;
587 hi = vnet_get_hw_interface (vnm, hw_if_index);
588 *parent_sw_if_index = hi->sw_if_index;
589 slot = vlib_node_add_named_next_with_slot (vm, hi->tx_node_index,
591 VNET_PIPE_TX_NEXT_ETHERNET_INPUT);
592 ASSERT (slot == VNET_PIPE_TX_NEXT_ETHERNET_INPUT);
595 * create two sub-interfaces, one for each end of the pipe.
597 error = pipe_create_sub_interface (hi, 0, &pipe_sw_if_index[0]);
602 error = pipe_create_sub_interface (hi, 1, &pipe_sw_if_index[1]);
607 hash_set (hi->sub_interface_sw_if_index_by_id, 0, pipe_sw_if_index[0]);
608 hash_set (hi->sub_interface_sw_if_index_by_id, 1, pipe_sw_if_index[1]);
610 vec_validate_init_empty (pipe_main.pipes, pipe_sw_if_index[0],
612 vec_validate_init_empty (pipe_main.pipes, pipe_sw_if_index[1],
615 pipe_main.pipes[pipe_sw_if_index[0]].sw_if_index = pipe_sw_if_index[1];
616 pipe_main.pipes[pipe_sw_if_index[1]].sw_if_index = pipe_sw_if_index[0];
621 clib_error_report (error);
625 typedef struct pipe_hw_walk_ctx_t_
629 } pipe_hw_walk_ctx_t;
632 pipe_hw_walk (vnet_main_t * vnm, u32 hw_if_index, void *args)
634 vnet_hw_interface_t *hi;
635 pipe_hw_walk_ctx_t *ctx;
638 hi = vnet_get_hw_interface (vnm, hw_if_index);
640 if (hi->dev_class_index == pipe_device_class.index)
642 u32 pipe_sw_if_index[2], id, sw_if_index;
645 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
648 pipe_sw_if_index[id] = sw_if_index;
652 ctx->cb (hi->sw_if_index, pipe_sw_if_index, hi->dev_instance, ctx->ctx);
655 return (WALK_CONTINUE);
659 pipe_walk (pipe_cb_fn_t fn, void *ctx)
661 pipe_hw_walk_ctx_t wctx = {
668 vnet_hw_interface_walk (vnet_get_main (), pipe_hw_walk, &wctx);
671 static clib_error_t *
672 create_pipe_interfaces (vlib_main_t * vm,
673 unformat_input_t * input, vlib_cli_command_t * cmd)
677 u32 pipe_sw_if_index[2];
679 u32 user_instance = 0;
681 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
683 if (unformat (input, "instance %d", &user_instance))
689 rv = vnet_create_pipe_interface (is_specified, user_instance,
690 &sw_if_index, pipe_sw_if_index);
693 return clib_error_return (0, "vnet_create_pipe_interface failed");
695 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
696 vnet_get_main (), sw_if_index);
701 * Create a pipe interface.
704 * The following two command syntaxes are equivalent:
705 * @cliexcmd{pipe create-interface [mac <mac-addr>] [instance <instance>]}
706 * Example of how to create a pipe interface:
707 * @cliexcmd{pipe create}
710 VLIB_CLI_COMMAND (pipe_create_interface_command, static) = {
711 .path = "pipe create",
712 .short_help = "pipe create [instance <instance>]",
713 .function = create_pipe_interfaces,
718 vnet_delete_pipe_interface (u32 sw_if_index)
720 vnet_main_t *vnm = vnet_get_main ();
721 vnet_sw_interface_t *si;
722 vnet_hw_interface_t *hi;
726 if (pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
727 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
729 si = vnet_get_sw_interface (vnm, sw_if_index);
730 hw_if_index = si->hw_if_index;
731 hi = vnet_get_hw_interface (vnm, hw_if_index);
732 instance = hi->dev_instance;
734 if (pipe_instance_free (instance) < 0)
736 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
740 hash_foreach (id, sw_if_index, hi->sub_interface_sw_if_index_by_id,
742 vnet_delete_sub_interface(sw_if_index);
743 pipe_main.pipes[sw_if_index] = PIPE_INVALID;
747 ethernet_delete_interface (vnm, hw_if_index);
752 static clib_error_t *
753 delete_pipe_interfaces (vlib_main_t * vm,
754 unformat_input_t * input, vlib_cli_command_t * cmd)
756 vnet_main_t *vnm = vnet_get_main ();
757 u32 sw_if_index = ~0;
760 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
762 if (unformat (input, "%U",
763 unformat_vnet_sw_interface, vnm, &sw_if_index))
769 if (sw_if_index == ~0)
770 return clib_error_return (0, "interface not specified");
772 rv = vnet_delete_pipe_interface (sw_if_index);
775 return clib_error_return (0, "vnet_delete_pipe_interface failed");
781 * Delete a pipe interface.
784 * The following two command syntaxes are equivalent:
785 * @cliexcmd{pipe delete intfc <interface>}
786 * Example of how to delete a pipe interface:
787 * @cliexcmd{pipe delete-interface intfc loop0}
790 VLIB_CLI_COMMAND (pipe_delete_interface_command, static) = {
791 .path = "pipe delete",
792 .short_help = "pipe delete <interface>",
793 .function = delete_pipe_interfaces,
798 * fd.io coding-style-patch-verification: ON
801 * eval: (c-set-style "gnu")