diff options
Diffstat (limited to 'extras/router-plugin/rtinject/tap_inject_node.c')
-rw-r--r-- | extras/router-plugin/rtinject/tap_inject_node.c | 172 |
1 files changed, 87 insertions, 85 deletions
diff --git a/extras/router-plugin/rtinject/tap_inject_node.c b/extras/router-plugin/rtinject/tap_inject_node.c index 73c296451..b888b9223 100644 --- a/extras/router-plugin/rtinject/tap_inject_node.c +++ b/extras/router-plugin/rtinject/tap_inject_node.c @@ -15,15 +15,16 @@ */ #include "tap_inject.h" -#include <sys/uio.h> #include <netinet/in.h> +#include <sys/uio.h> #include <vnet/ethernet/arp_packet.h> vlib_node_registration_t tap_inject_rx_node; vlib_node_registration_t tap_inject_tx_node; vlib_node_registration_t tap_inject_neighbor_node; -enum { +enum +{ NEXT_NEIGHBOR_ARP, NEXT_NEIGHBOR_ICMP6, }; @@ -34,7 +35,7 @@ enum { dpo_type_t tap_inject_dpo_type; static inline void -tap_inject_tap_send_buffer (int fd, vlib_buffer_t * b) +tap_inject_tap_send_buffer (int fd, vlib_buffer_t *b) { struct iovec iov; ssize_t n_bytes; @@ -51,10 +52,10 @@ tap_inject_tap_send_buffer (int fd, vlib_buffer_t * b) } static uword -tap_inject_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f) +tap_inject_tx (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f) { - vlib_buffer_t * b; - u32 * pkts; + vlib_buffer_t *b; + u32 *pkts; u32 fd; u32 i; @@ -66,7 +67,7 @@ tap_inject_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f) fd = tap_inject_lookup_tap_fd (vnet_buffer (b)->sw_if_index[VLIB_RX]); if (fd == ~0) - continue; + continue; /* Re-wind the buffer to the start of the Ethernet header. */ vlib_buffer_advance (b, -b->current_data); @@ -85,20 +86,19 @@ VLIB_REGISTER_NODE (tap_inject_tx_node) = { .type = VLIB_NODE_TYPE_INTERNAL, }; - static uword -tap_inject_neighbor (vlib_main_t * vm, - vlib_node_runtime_t * node, vlib_frame_t * f) +tap_inject_neighbor (vlib_main_t *vm, vlib_node_runtime_t *node, + vlib_frame_t *f) { - vlib_buffer_t * b; - u32 * pkts; + vlib_buffer_t *b; + u32 *pkts; u32 fd; u32 i; u32 bi; u32 next_index = node->cached_next_index; u32 next = ~0; u32 n_left; - u32 * to_next; + u32 *to_next; pkts = vlib_frame_vector_args (f); @@ -109,10 +109,10 @@ tap_inject_neighbor (vlib_main_t * vm, fd = tap_inject_lookup_tap_fd (vnet_buffer (b)->sw_if_index[VLIB_RX]); if (fd == ~0) - { - vlib_buffer_free (vm, &bi, 1); - continue; - } + { + vlib_buffer_free (vm, &bi, 1); + continue; + } /* Re-wind the buffer to the start of the Ethernet header. */ vlib_buffer_advance (b, -b->current_data); @@ -121,32 +121,32 @@ tap_inject_neighbor (vlib_main_t * vm, /* Send the buffer to a neighbor node too? */ { - ethernet_header_t * eth = vlib_buffer_get_current (b); - u16 ether_type = htons (eth->type); - - if (ether_type == ETHERNET_TYPE_ARP) - { - ethernet_arp_header_t * arp = (void *)(eth + 1); - - if (arp->opcode == ntohs (ETHERNET_ARP_OPCODE_reply)) - next = NEXT_NEIGHBOR_ARP; - } - else if (ether_type == ETHERNET_TYPE_IP6) - { - ip6_header_t * ip = (void *)(eth + 1); - icmp46_header_t * icmp = (void *)(ip + 1); - - if (ip->protocol == IP_PROTOCOL_ICMP6 && - icmp->type == ICMP6_neighbor_advertisement) - next = NEXT_NEIGHBOR_ICMP6; - } + ethernet_header_t *eth = vlib_buffer_get_current (b); + u16 ether_type = htons (eth->type); + + if (ether_type == ETHERNET_TYPE_ARP) + { + ethernet_arp_header_t *arp = (void *) (eth + 1); + + if (arp->opcode == ntohs (ETHERNET_ARP_OPCODE_reply)) + next = NEXT_NEIGHBOR_ARP; + } + else if (ether_type == ETHERNET_TYPE_IP6) + { + ip6_header_t *ip = (void *) (eth + 1); + icmp46_header_t *icmp = (void *) (ip + 1); + + if (ip->protocol == IP_PROTOCOL_ICMP6 && + icmp->type == ICMP6_neighbor_advertisement) + next = NEXT_NEIGHBOR_ICMP6; + } } if (next == ~0) - { - vlib_buffer_free (vm, &bi, 1); - continue; - } + { + vlib_buffer_free (vm, &bi, 1); + continue; + } /* ARP and ICMP6 expect to start processing after the Ethernet header. */ vlib_buffer_advance (b, sizeof (ethernet_header_t)); @@ -156,39 +156,41 @@ tap_inject_neighbor (vlib_main_t * vm, *(to_next++) = bi; --n_left; - vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, - n_left, bi, next); + vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next, n_left, + bi, next); vlib_put_next_frame (vm, node, next_index, n_left); } return f->n_vectors; } -VLIB_REGISTER_NODE (tap_inject_neighbor_node) = { - .function = tap_inject_neighbor, - .name = "tap-inject-neighbor", - .vector_size = sizeof (u32), - .type = VLIB_NODE_TYPE_INTERNAL, - .n_next_nodes = 2, - .next_nodes = { - [NEXT_NEIGHBOR_ARP] = "arp-input", - [NEXT_NEIGHBOR_ICMP6] = "icmp6-neighbor-solicitation", - }, +VLIB_REGISTER_NODE(tap_inject_neighbor_node) = { + .function = tap_inject_neighbor, + .name = "tap-inject-neighbor", + .vector_size = sizeof(u32), + .type = VLIB_NODE_TYPE_INTERNAL, + .n_next_nodes = 2, + .next_nodes = + { + [NEXT_NEIGHBOR_ARP] = "arp-input", + [NEXT_NEIGHBOR_ICMP6] = "icmp6-neighbor-solicitation", + }, }; - #define MTU 1500 -#define MTU_BUFFERS ((MTU + vlib_buffer_get_default_data_size(vm) - 1) / vlib_buffer_get_default_data_size(vm)) +#define MTU_BUFFERS \ + ((MTU + vlib_buffer_get_default_data_size (vm) - 1) / \ + vlib_buffer_get_default_data_size (vm)) #define NUM_BUFFERS_TO_ALLOC 32 static inline uword -tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) +tap_rx (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f, int fd) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); u32 sw_if_index; struct iovec iov[MTU_BUFFERS]; u32 bi[MTU_BUFFERS]; - vlib_buffer_t * b; + vlib_buffer_t *b; ssize_t n_bytes; ssize_t n_bytes_left; u32 i, j; @@ -202,25 +204,23 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) { u32 len = vec_len (im->rx_buffers); - - u8 index = vlib_buffer_pool_get_default_for_numa (vm,0); - len = vlib_buffer_alloc_from_pool(vm, - &im->rx_buffers[len], NUM_BUFFERS_TO_ALLOC, - index); + u8 index = vlib_buffer_pool_get_default_for_numa (vm, 0); + len = vlib_buffer_alloc_from_pool (vm, &im->rx_buffers[len], + NUM_BUFFERS_TO_ALLOC, index); _vec_len (im->rx_buffers) += len; if (vec_len (im->rx_buffers) < MTU_BUFFERS) - { - clib_warning ("failed to allocate buffers"); - return 0; - } + { + clib_warning ("failed to allocate buffers"); + return 0; + } } /* Fill buffers from the end of the list to make it easier to resize. */ for (i = 0, j = vec_len (im->rx_buffers) - 1; i < MTU_BUFFERS; ++i, --j) { - vlib_buffer_t * b; + vlib_buffer_t *b; bi[i] = im->rx_buffers[j]; @@ -253,7 +253,8 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) b->current_length = n_bytes; /* If necessary, configure any remaining buffers in the chain. */ - for (i = 1; n_bytes_left > 0; ++i, n_bytes_left -= VLIB_BUFFER_DEFAULT_DATA_SIZE) + for (i = 1; n_bytes_left > 0; + ++i, n_bytes_left -= VLIB_BUFFER_DEFAULT_DATA_SIZE) { b = vlib_get_buffer (vm, bi[i - 1]); b->current_length = VLIB_BUFFER_DEFAULT_DATA_SIZE; @@ -268,9 +269,9 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) /* Get the packet to the output node. */ { - vnet_hw_interface_t * hw; - vlib_frame_t * new_frame; - u32 * to_next; + vnet_hw_interface_t *hw; + vlib_frame_t *new_frame; + u32 *to_next; hw = vnet_get_hw_interface (vnet_get_main (), sw_if_index); @@ -286,20 +287,20 @@ tap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f, int fd) } static uword -tap_inject_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * f) +tap_inject_rx (vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f) { - tap_inject_main_t * im = tap_inject_get_main (); - u32 * fd; + tap_inject_main_t *im = tap_inject_get_main (); + u32 *fd; uword count = 0; vec_foreach (fd, im->rx_file_descriptors) { if (tap_rx (vm, node, f, *fd) != 1) - { - clib_warning ("rx failed"); - count = 0; - break; - } + { + clib_warning ("rx failed"); + count = 0; + break; + } ++count; } @@ -320,7 +321,7 @@ VLIB_REGISTER_NODE (tap_inject_rx_node) = { * @brief no-op lock function. */ static void -tap_inject_dpo_lock (dpo_id_t * dpo) +tap_inject_dpo_lock (dpo_id_t *dpo) { } @@ -328,12 +329,12 @@ tap_inject_dpo_lock (dpo_id_t * dpo) * @brief no-op unlock function. */ static void -tap_inject_dpo_unlock (dpo_id_t * dpo) +tap_inject_dpo_unlock (dpo_id_t *dpo) { } u8 * -format_tap_inject_dpo (u8 * s, va_list * args) +format_tap_inject_dpo (u8 *s, va_list *args) { return (format (s, "tap-inject:[%d]", 0)); } @@ -355,15 +356,16 @@ const static char *const *const tap_inject_nodes[DPO_PROTO_NUM] = { }; static clib_error_t * -tap_inject_init (vlib_main_t * vm) +tap_inject_init (vlib_main_t *vm) { - tap_inject_main_t * im = tap_inject_get_main (); + tap_inject_main_t *im = tap_inject_get_main (); im->rx_node_index = tap_inject_rx_node.index; im->tx_node_index = tap_inject_tx_node.index; im->neighbor_node_index = tap_inject_neighbor_node.index; - tap_inject_dpo_type = dpo_register_new_type (&tap_inject_vft, tap_inject_nodes); + tap_inject_dpo_type = + dpo_register_new_type (&tap_inject_vft, tap_inject_nodes); vec_alloc (im->rx_buffers, NUM_BUFFERS_TO_ALLOC); vec_reset_length (im->rx_buffers); |