From 1b0c983726d4a6abe5b86b1cfad1d3280285748f Mon Sep 17 00:00:00 2001 From: sharath reddy Date: Wed, 29 Nov 2017 20:08:11 +0530 Subject: VPP-273 Coding standards cleanup - vnet/vnet/unix Change-Id: Ibac5a4588e66f6d3ad42dd2583e1e84b7d2314c4 Signed-off-by: sharath reddy --- src/vnet/unix/gdb_funcs.c | 156 ++++---- src/vnet/unix/pcap.c | 62 ++-- src/vnet/unix/pcap.h | 50 +-- src/vnet/unix/pcap2pg.c | 115 +++--- src/vnet/unix/tapcli.c | 923 +++++++++++++++++++++++++--------------------- src/vnet/unix/tapcli.h | 20 +- src/vnet/unix/tuntap.c | 399 ++++++++++---------- src/vnet/unix/tuntap.h | 16 +- 8 files changed, 947 insertions(+), 794 deletions(-) (limited to 'src') diff --git a/src/vnet/unix/gdb_funcs.c b/src/vnet/unix/gdb_funcs.c index 40d0d663472..41ae3bdca67 100644 --- a/src/vnet/unix/gdb_funcs.c +++ b/src/vnet/unix/gdb_funcs.c @@ -31,7 +31,8 @@ * @return length - u32 * */ -u32 vl(void *p) +u32 +vl (void *p) { return vec_len (p); } @@ -44,9 +45,10 @@ u32 vl(void *p) * @return number - uword * */ -uword pe (void *v) +uword +pe (void *v) { - return (pool_elts(v)); + return (pool_elts (v)); } /** @@ -58,7 +60,8 @@ uword pe (void *v) * @return 0|1 - int * */ -int pifi (void *p, u32 index) +int +pifi (void *p, u32 index) { return pool_is_free_index (p, index); } @@ -70,7 +73,8 @@ int pifi (void *p, u32 index) * @param n - u32 - number of bytes to format * */ -void debug_hex_bytes (u8 *s, u32 n) +void +debug_hex_bytes (u8 * s, u32 n) { fformat (stderr, "%U\n", format_hex_bytes, s, n); } @@ -79,39 +83,39 @@ void debug_hex_bytes (u8 *s, u32 n) * @brief GDB callable function: vlib_dump_frame_ownership * */ -void vlib_dump_frame_ownership (void) +void +vlib_dump_frame_ownership (void) { - vlib_main_t * vm = vlib_get_main(); - vlib_node_main_t * nm = &vm->node_main; - vlib_node_runtime_t * this_node_runtime; - vlib_next_frame_t * nf; + vlib_main_t *vm = vlib_get_main (); + vlib_node_main_t *nm = &vm->node_main; + vlib_node_runtime_t *this_node_runtime; + vlib_next_frame_t *nf; u32 first_nf_index; u32 index; - vec_foreach(this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) - { - first_nf_index = this_node_runtime->next_frame_index; - - for (index = first_nf_index; index < first_nf_index + - this_node_runtime->n_next_nodes; index++) - { - vlib_node_runtime_t * owned_runtime; - nf = vec_elt_at_index (vm->node_main.next_frames, index); - if (nf->flags & VLIB_FRAME_OWNER) - { - owned_runtime = vec_elt_at_index (nm->nodes_by_type[0], - nf->node_runtime_index); - fformat(stderr, - "%s next index %d owns enqueue rights to %s\n", - nm->nodes[this_node_runtime->node_index]->name, - index - first_nf_index, - nm->nodes[owned_runtime->node_index]->name); - fformat (stderr, " nf index %d nf->frame_index %d\n", - nf - vm->node_main.next_frames, - nf->frame_index); - } - } - } + vec_foreach (this_node_runtime, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) + { + first_nf_index = this_node_runtime->next_frame_index; + + for (index = first_nf_index; index < first_nf_index + + this_node_runtime->n_next_nodes; index++) + { + vlib_node_runtime_t *owned_runtime; + nf = vec_elt_at_index (vm->node_main.next_frames, index); + if (nf->flags & VLIB_FRAME_OWNER) + { + owned_runtime = vec_elt_at_index (nm->nodes_by_type[0], + nf->node_runtime_index); + fformat (stderr, + "%s next index %d owns enqueue rights to %s\n", + nm->nodes[this_node_runtime->node_index]->name, + index - first_nf_index, + nm->nodes[owned_runtime->node_index]->name); + fformat (stderr, " nf index %d nf->frame_index %d\n", + nf - vm->node_main.next_frames, nf->frame_index); + } + } + } } /** @@ -121,45 +125,49 @@ void vlib_dump_frame_ownership (void) * * @param index - u32 */ -void vlib_runtime_index_to_node_name (u32 index) +void +vlib_runtime_index_to_node_name (u32 index) { - vlib_main_t * vm = vlib_get_main(); - vlib_node_main_t * nm = &vm->node_main; + vlib_main_t *vm = vlib_get_main (); + vlib_node_main_t *nm = &vm->node_main; if (index > vec_len (nm->nodes)) { - fformat(stderr, "%d out of range, max %d\n", vec_len(nm->nodes)); + fformat (stderr, "%d out of range, max %d\n", vec_len (nm->nodes)); return; } - fformat(stderr, "node runtime index %d name %s\n", index, nm->nodes[index]->name); + fformat (stderr, "node runtime index %d name %s\n", index, + nm->nodes[index]->name); } -void gdb_show_errors (int verbose) +void +gdb_show_errors (int verbose) { extern vlib_cli_command_t vlib_cli_show_errors; unformat_input_t input; - vlib_main_t * vm = vlib_get_main(); + vlib_main_t *vm = vlib_get_main (); if (verbose == 0) unformat_init_string (&input, "verbose 0", 9); else if (verbose == 1) unformat_init_string (&input, "verbose 1", 9); - else + else { - fformat(stderr, "verbose not 0 or 1\n"); + fformat (stderr, "verbose not 0 or 1\n"); return; } - vlib_cli_show_errors.function (vm, &input, 0 /* cmd */); + vlib_cli_show_errors.function (vm, &input, 0 /* cmd */ ); unformat_free (&input); -} +} -void gdb_show_session (int verbose) +void +gdb_show_session (int verbose) { extern vlib_cli_command_t vlib_cli_show_session_command; unformat_input_t input; - vlib_main_t * vm = vlib_get_main(); + vlib_main_t *vm = vlib_get_main (); if (verbose == 0) unformat_init_string (&input, "verbose 0", 9); @@ -167,15 +175,15 @@ void gdb_show_session (int verbose) unformat_init_string (&input, "verbose 1", 9); else if (verbose == 2) unformat_init_string (&input, "verbose 2", 9); - else + else { - fformat(stderr, "verbose not 0 - 2\n"); + fformat (stderr, "verbose not 0 - 2\n"); return; } - vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */); + vlib_cli_show_session_command.function (vm, &input, 0 /* cmd */ ); unformat_free (&input); -} +} /** * @brief GDB callable function: show_gdb_command_fn - show gdb @@ -186,8 +194,7 @@ void gdb_show_session (int verbose) */ static clib_error_t * show_gdb_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { vlib_cli_output (vm, "vl(p) returns vec_len(p)"); vlib_cli_output (vm, "vb(b) returns vnet_buffer(b) [opaque]"); @@ -203,35 +210,50 @@ show_gdb_command_fn (vlib_main_t * vm, return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_gdb_funcs_command, static) = { .path = "show gdb", .short_help = "Describe functions which can be called from gdb", .function = show_gdb_command_fn, }; +/* *INDENT-ON* */ -vnet_buffer_opaque_t *vb (void *vb_arg) +vnet_buffer_opaque_t * +vb (void *vb_arg) { - vlib_buffer_t *b = (vlib_buffer_t *)vb_arg; - vnet_buffer_opaque_t *rv; - - rv = vnet_buffer (b); + vlib_buffer_t *b = (vlib_buffer_t *) vb_arg; + vnet_buffer_opaque_t *rv; + + rv = vnet_buffer (b); - return rv; + return rv; } -vnet_buffer_opaque2_t *vb2 (void *vb_arg) +vnet_buffer_opaque2_t * +vb2 (void *vb_arg) { - vlib_buffer_t *b = (vlib_buffer_t *)vb_arg; - vnet_buffer_opaque2_t *rv; - - rv = vnet_buffer2(b); + vlib_buffer_t *b = (vlib_buffer_t *) vb_arg; + vnet_buffer_opaque2_t *rv; + + rv = vnet_buffer2 (b); - return rv; + return rv; } /* Cafeteria plan, maybe you don't want these functions */ -clib_error_t * -gdb_func_init (vlib_main_t * vm) { return 0; } +clib_error_t * +gdb_func_init (vlib_main_t * vm) +{ + return 0; +} VLIB_INIT_FUNCTION (gdb_func_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/pcap.c b/src/vnet/unix/pcap.c index bba225f74ab..0832b16e753 100644 --- a/src/vnet/unix/pcap.c +++ b/src/vnet/unix/pcap.c @@ -88,20 +88,22 @@ pcap_close (pcap_main_t * pm) clib_error_t * pcap_write (pcap_main_t * pm) { - clib_error_t * error = 0; + clib_error_t *error = 0; - if (! (pm->flags & PCAP_MAIN_INIT_DONE)) + if (!(pm->flags & PCAP_MAIN_INIT_DONE)) { pcap_file_header_t fh; int n; - if (! pm->file_name) + if (!pm->file_name) pm->file_name = "/tmp/vnet.pcap"; - pm->file_descriptor = open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664); + pm->file_descriptor = + open (pm->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0664); if (pm->file_descriptor < 0) { - error = clib_error_return_unix (0, "failed to open `%s'", pm->file_name); + error = + clib_error_return_unix (0, "failed to open `%s'", pm->file_name); goto done; } @@ -121,9 +123,13 @@ pcap_write (pcap_main_t * pm) if (n != sizeof (fh)) { if (n < 0) - error = clib_error_return_unix (0, "write file header `%s'", pm->file_name); + error = + clib_error_return_unix (0, "write file header `%s'", + pm->file_name); else - error = clib_error_return (0, "short write of file header `%s'", pm->file_name); + error = + clib_error_return (0, "short write of file header `%s'", + pm->file_name); goto done; } } @@ -133,15 +139,16 @@ pcap_write (pcap_main_t * pm) int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written; n = write (pm->file_descriptor, - vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written), n); + vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written), + n); if (n < 0 && unix_error_is_fatal (errno)) { error = clib_error_return_unix (0, "write `%s'", pm->file_name); goto done; } - pm->n_pcap_data_written += n; - } + pm->n_pcap_data_written += n; + } if (pm->n_pcap_data_written >= vec_len (pm->pcap_data)) { @@ -150,9 +157,9 @@ pcap_write (pcap_main_t * pm) } if (pm->n_packets_captured >= pm->n_packets_to_capture) - pcap_close(pm); + pcap_close (pm); - done: +done: if (error) { if (pm->file_descriptor >= 0) @@ -167,9 +174,10 @@ pcap_write (pcap_main_t * pm) * @return rc - clib_error_t * */ -clib_error_t * pcap_read (pcap_main_t * pm) +clib_error_t * +pcap_read (pcap_main_t * pm) { - clib_error_t * error = 0; + clib_error_t *error = 0; int fd, need_swap, n; pcap_file_header_t fh; pcap_packet_header_t ph; @@ -183,7 +191,8 @@ clib_error_t * pcap_read (pcap_main_t * pm) if (read (fd, &fh, sizeof (fh)) != sizeof (fh)) { - error = clib_error_return_unix (0, "read file header `%s'", pm->file_name); + error = + clib_error_return_unix (0, "read file header `%s'", pm->file_name); goto done; } @@ -194,7 +203,7 @@ clib_error_t * pcap_read (pcap_main_t * pm) #define _(t,f) fh.f = clib_byte_swap_##t (fh.f); foreach_pcap_file_header; #undef _ - } + } if (fh.magic != 0xa1b2c3d4) { @@ -206,7 +215,7 @@ clib_error_t * pcap_read (pcap_main_t * pm) pm->max_packet_bytes = 0; while ((n = read (fd, &ph, sizeof (ph))) != 0) { - u8 * data; + u8 *data; if (need_swap) { @@ -216,7 +225,8 @@ clib_error_t * pcap_read (pcap_main_t * pm) } data = vec_new (u8, ph.n_bytes_in_packet); - if (read (fd, data, ph.n_packet_bytes_stored_in_file) != ph.n_packet_bytes_stored_in_file) + if (read (fd, data, ph.n_packet_bytes_stored_in_file) != + ph.n_packet_bytes_stored_in_file) { error = clib_error_return (0, "short read `%s'", pm->file_name); goto done; @@ -226,16 +236,26 @@ clib_error_t * pcap_read (pcap_main_t * pm) pm->min_packet_bytes = pm->max_packet_bytes = ph.n_bytes_in_packet; else { - pm->min_packet_bytes = clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet); - pm->max_packet_bytes = clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet); + pm->min_packet_bytes = + clib_min (pm->min_packet_bytes, ph.n_bytes_in_packet); + pm->max_packet_bytes = + clib_max (pm->max_packet_bytes, ph.n_bytes_in_packet); } vec_add1 (pm->packets_read, data); } - done: +done: if (fd >= 0) close (fd); return error; } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/pcap.h b/src/vnet/unix/pcap.h index 6aaf32bef7e..e3fac458af4 100644 --- a/src/vnet/unix/pcap.h +++ b/src/vnet/unix/pcap.h @@ -61,7 +61,8 @@ _ (ip, 12) \ _ (hdlc, 104) -typedef enum { +typedef enum +{ #define _(f,n) PCAP_PACKET_TYPE_##f = (n), foreach_vnet_pcap_packet_type #undef _ @@ -89,7 +90,8 @@ typedef enum { _ (u32, packet_type) /** File header struct */ -typedef struct { +typedef struct +{ #define _(t, f) t f; foreach_pcap_file_header #undef _ @@ -107,11 +109,11 @@ typedef struct { _ (u32, n_bytes_in_packet) /** Packet header. */ -typedef struct { +typedef struct +{ #define _(t, f) t f; foreach_pcap_packet_header #undef _ - /** Packet data follows. */ u8 data[0]; } pcap_packet_header_t; @@ -119,9 +121,10 @@ typedef struct { /** * @brief PCAP main state data structure */ -typedef struct { +typedef struct +{ /** File name of pcap output. */ - char * file_name; + char *file_name; /** Number of packets to capture. */ u32 n_packets_to_capture; @@ -143,20 +146,20 @@ typedef struct { u32 n_pcap_data_written; /** Vector of pcap data. */ - u8 * pcap_data; + u8 *pcap_data; /** Packets read from file. */ - u8 ** packets_read; + u8 **packets_read; /** Min/Max Packet bytes */ u32 min_packet_bytes, max_packet_bytes; } pcap_main_t; /** Write out data to output file. */ -clib_error_t * pcap_write (pcap_main_t * pm); +clib_error_t *pcap_write (pcap_main_t * pm); /** Read data from file. */ -clib_error_t * pcap_read (pcap_main_t * pm); +clib_error_t *pcap_read (pcap_main_t * pm); /** * @brief Add packet @@ -171,17 +174,15 @@ clib_error_t * pcap_read (pcap_main_t * pm); */ static inline void * pcap_add_packet (pcap_main_t * pm, - f64 time_now, - u32 n_bytes_in_trace, - u32 n_bytes_in_packet) + f64 time_now, u32 n_bytes_in_trace, u32 n_bytes_in_packet) { - pcap_packet_header_t * h; - u8 * d; + pcap_packet_header_t *h; + u8 *d; vec_add2 (pm->pcap_data, d, sizeof (h[0]) + n_bytes_in_trace); h = (void *) (d); h->time_in_sec = time_now; - h->time_in_usec = 1e6*(time_now - h->time_in_sec); + h->time_in_usec = 1e6 * (time_now - h->time_in_sec); h->n_packet_bytes_stored_in_file = n_bytes_in_trace; h->n_bytes_in_packet = n_bytes_in_packet; pm->n_packets_captured++; @@ -199,14 +200,13 @@ pcap_add_packet (pcap_main_t * pm, */ static inline void pcap_add_buffer (pcap_main_t * pm, - vlib_main_t * vm, u32 buffer_index, - u32 n_bytes_in_trace) + vlib_main_t * vm, u32 buffer_index, u32 n_bytes_in_trace) { - vlib_buffer_t * b = vlib_get_buffer (vm, buffer_index); + vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); u32 n = vlib_buffer_length_in_chain (vm, b); i32 n_left = clib_min (n_bytes_in_trace, n); f64 time_now = vlib_time_now (vm); - void * d; + void *d; d = pcap_add_packet (pm, time_now, n_left, n); while (1) @@ -222,9 +222,17 @@ pcap_add_buffer (pcap_main_t * pm, } /** Flush output vector. */ - if (vec_len (pm->pcap_data) >= 64*1024 + if (vec_len (pm->pcap_data) >= 64 * 1024 || pm->n_packets_captured >= pm->n_packets_to_capture) pcap_write (pm); } #endif /* included_vnet_pcap_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/pcap2pg.c b/src/vnet/unix/pcap2pg.c index 217a61f4cb4..46d6bfcd0c7 100644 --- a/src/vnet/unix/pcap2pg.c +++ b/src/vnet/unix/pcap2pg.c @@ -28,12 +28,10 @@ pcap_main_t pcap_main; /** * @brief char * to seed a PG file */ -static char * pg_fmt = +static char *pg_fmt = "packet-generator new {\n" " name s%d\n" - " limit 1\n" - " size %d-%d\n" - " node ethernet-input\n"; + " limit 1\n" " size %d-%d\n" " node ethernet-input\n"; /** @@ -43,9 +41,10 @@ static char * pg_fmt = * @param i - int * @param *pkt - u8 */ -void stream_boilerplate (FILE *ofp, int i, u8 * pkt) +void +stream_boilerplate (FILE * ofp, int i, u8 * pkt) { - fformat(ofp, pg_fmt, i, vec_len(pkt), vec_len(pkt)); + fformat (ofp, pg_fmt, i, vec_len (pkt), vec_len (pkt)); } /** @@ -57,7 +56,8 @@ void stream_boilerplate (FILE *ofp, int i, u8 * pkt) * @return rc - int * */ -int pcap2pg (pcap_main_t * pm, FILE *ofp) +int +pcap2pg (pcap_main_t * pm, FILE * ofp) { int i, j; u8 *pkt; @@ -65,11 +65,11 @@ int pcap2pg (pcap_main_t * pm, FILE *ofp) for (i = 0; i < vec_len (pm->packets_read); i++) { int offset; - ethernet_header_t * h; + ethernet_header_t *h; u64 ethertype; pkt = pm->packets_read[i]; - h = (ethernet_header_t *)pkt; + h = (ethernet_header_t *) pkt; stream_boilerplate (ofp, i, pkt); @@ -83,36 +83,34 @@ int pcap2pg (pcap_main_t * pm, FILE *ofp) * This transforms captured 802.1q VLAN packets into * regular Ethernet packets. */ - if (ethertype == 0x8100 /* 802.1q vlan */) - { - u16 * vlan_ethertype = (u16 *)(h+1); - ethertype = clib_net_to_host_u16(vlan_ethertype[0]); - offset = 18; - } + if (ethertype == 0x8100 /* 802.1q vlan */ ) + { + u16 *vlan_ethertype = (u16 *) (h + 1); + ethertype = clib_net_to_host_u16 (vlan_ethertype[0]); + offset = 18; + } else - offset = 14; + offset = 14; fformat (ofp, - " 0x%04x: %02x%02x.%02x%02x.%02x%02x" - " -> %02x%02x.%02x%02x.%02x%02x\n", - ethertype, - h->src_address[0], - h->src_address[1], - h->src_address[2], - h->src_address[3], - h->src_address[4], - h->src_address[5], - h->dst_address[0], - h->dst_address[1], - h->dst_address[2], - h->dst_address[3], - h->dst_address[4], - h->dst_address[5]); + " 0x%04x: %02x%02x.%02x%02x.%02x%02x" + " -> %02x%02x.%02x%02x.%02x%02x\n", + ethertype, + h->src_address[0], + h->src_address[1], + h->src_address[2], + h->src_address[3], + h->src_address[4], + h->src_address[5], + h->dst_address[0], + h->dst_address[1], + h->dst_address[2], + h->dst_address[3], h->dst_address[4], h->dst_address[5]); fformat (ofp, " hex 0x"); for (j = offset; j < vec_len (pkt); j++) - fformat (ofp, "%02x", pkt[j]); + fformat (ofp, "%02x", pkt[j]); fformat (ofp, " }\n"); fformat (ofp, "}\n\n"); @@ -124,37 +122,38 @@ int pcap2pg (pcap_main_t * pm, FILE *ofp) * @brief pcap2pg. * usage: pcap2pg -i [-o ] */ -int main (int argc, char **argv) +int +main (int argc, char **argv) { unformat_input_t input; - pcap_main_t * pm = &pcap_main; - u8 * input_file = 0, * output_file = 0; - FILE * ofp; - clib_error_t * error; + pcap_main_t *pm = &pcap_main; + u8 *input_file = 0, *output_file = 0; + FILE *ofp; + clib_error_t *error; unformat_init_command_line (&input, argv); while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) { - if (unformat(&input, "-i %s", &input_file) - || unformat (&input, "input %s", &input_file)) - ; + if (unformat (&input, "-i %s", &input_file) + || unformat (&input, "input %s", &input_file)) + ; else if (unformat (&input, "-o %s", &output_file) - || unformat (&input, "output %s", &output_file)) - ; - else - { - usage: - fformat(stderr, - "usage: pcap2pg -i [-o ]\n"); - exit (1); - } + || unformat (&input, "output %s", &output_file)) + ; + else + { + usage: + fformat (stderr, + "usage: pcap2pg -i [-o ]\n"); + exit (1); + } } if (input_file == 0) goto usage; - - pm->file_name = (char *)input_file; + + pm->file_name = (char *) input_file; error = pcap_read (pm); if (error) @@ -165,18 +164,26 @@ int main (int argc, char **argv) if (output_file) { - ofp = fopen ((char *)output_file, "rw"); + ofp = fopen ((char *) output_file, "rw"); if (ofp == NULL) - clib_unix_warning ("Couldn't create '%s'", output_file); + clib_unix_warning ("Couldn't create '%s'", output_file); exit (1); } else { ofp = stdout; } - + pcap2pg (pm, ofp); fclose (ofp); exit (0); } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/tapcli.c b/src/vnet/unix/tapcli.c index 3ad8338a200..10a86a457e8 100644 --- a/src/vnet/unix/tapcli.c +++ b/src/vnet/unix/tapcli.c @@ -49,12 +49,13 @@ static vnet_hw_interface_class_t tapcli_interface_class; static vlib_node_registration_t tapcli_rx_node; static void tapcli_nopunt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame); + vlib_node_runtime_t * node, + vlib_frame_t * frame); /** * @brief Struct for the tapcli interface */ -typedef struct { +typedef struct +{ u32 unix_fd; u32 clib_file_index; u32 provision_fd; @@ -71,7 +72,8 @@ typedef struct { /** * @brief Struct for RX trace */ -typedef struct { +typedef struct +{ u16 sw_if_index; } tapcli_rx_trace_t; @@ -84,14 +86,14 @@ typedef struct { * @return *s - u8 - formatted string * */ -u8 * format_tapcli_rx_trace (u8 * s, va_list * va) +u8 * +format_tapcli_rx_trace (u8 * s, va_list * va) { CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); - vnet_main_t * vnm = vnet_get_main(); - tapcli_rx_trace_t * t = va_arg (*va, tapcli_rx_trace_t *); - s = format (s, "%U", format_vnet_sw_if_index_name, - vnm, t->sw_if_index); + vnet_main_t *vnm = vnet_get_main (); + tapcli_rx_trace_t *t = va_arg (*va, tapcli_rx_trace_t *); + s = format (s, "%U", format_vnet_sw_if_index_name, vnm, t->sw_if_index); return s; } @@ -102,18 +104,19 @@ typedef struct { /** Vector of VLIB rx buffers to use. We allocate them in blocks of VLIB_FRAME_SIZE (256). */ - u32 * rx_buffers; + u32 *rx_buffers; /** Vector of iovecs for readv/writev calls. */ - struct iovec * iovecs; + struct iovec *iovecs; } tapcli_per_thread_t; /** * @brief TAPCLI main state struct */ -typedef struct { +typedef struct +{ /** per thread variables */ - tapcli_per_thread_t * threads; + tapcli_per_thread_t *threads; /** tap device destination MAC address. Required, or Linux drops pkts */ u8 ether_dst_mac[6]; @@ -122,30 +125,30 @@ typedef struct { u32 mtu_bytes, mtu_buffers; /** Vector of tap interfaces */ - tapcli_interface_t * tapcli_interfaces; + tapcli_interface_t *tapcli_interfaces; /** Vector of deleted tap interfaces */ - u32 * tapcli_inactive_interfaces; + u32 *tapcli_inactive_interfaces; /** Bitmap of tap interfaces with pending reads */ - uword * pending_read_bitmap; + uword *pending_read_bitmap; /** Hash table to find tapcli interface given hw_if_index */ - uword * tapcli_interface_index_by_sw_if_index; + uword *tapcli_interface_index_by_sw_if_index; /** Hash table to find tapcli interface given unix fd */ - uword * tapcli_interface_index_by_unix_fd; + uword *tapcli_interface_index_by_unix_fd; /** renumbering table */ - u32 * show_dev_instance_by_real_dev_instance; + u32 *show_dev_instance_by_real_dev_instance; /** 1 => disable CLI */ int is_disabled; /** convenience - vlib_main_t */ - vlib_main_t * vlib_main; + vlib_main_t *vlib_main; /** convenience - vnet_main_t */ - vnet_main_t * vnet_main; + vnet_main_t *vnet_main; } tapcli_main_t; static tapcli_main_t tapcli_main; @@ -165,48 +168,46 @@ static tapcli_main_t tapcli_main; * */ static uword -tapcli_tx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) +tapcli_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { - u32 * buffers = vlib_frame_args (frame); + u32 *buffers = vlib_frame_args (frame); uword n_packets = frame->n_vectors; - tapcli_main_t * tm = &tapcli_main; - tapcli_interface_t * ti; + tapcli_main_t *tm = &tapcli_main; + tapcli_interface_t *ti; int i; u16 thread_index = vlib_get_thread_index (); for (i = 0; i < n_packets; i++) { - struct iovec * iov; - vlib_buffer_t * b; + struct iovec *iov; + vlib_buffer_t *b; uword l; - vnet_hw_interface_t * hw; - uword * p; + vnet_hw_interface_t *hw; + uword *p; u32 tx_sw_if_index; b = vlib_get_buffer (vm, buffers[i]); - tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_TX]; - if (tx_sw_if_index == (u32)~0) - tx_sw_if_index = vnet_buffer(b)->sw_if_index[VLIB_RX]; - - ASSERT(tx_sw_if_index != (u32)~0); + tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX]; + if (tx_sw_if_index == (u32) ~ 0) + tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; + + ASSERT (tx_sw_if_index != (u32) ~ 0); /* Use the sup intfc to finesse vlan subifs */ hw = vnet_get_sup_hw_interface (tm->vnet_main, tx_sw_if_index); tx_sw_if_index = hw->sw_if_index; - p = hash_get (tm->tapcli_interface_index_by_sw_if_index, - tx_sw_if_index); + p = hash_get (tm->tapcli_interface_index_by_sw_if_index, + tx_sw_if_index); if (p == 0) - { - clib_warning ("sw_if_index %d unknown", tx_sw_if_index); - /* $$$ leak, but this should never happen... */ - continue; - } + { + clib_warning ("sw_if_index %d unknown", tx_sw_if_index); + /* $$$ leak, but this should never happen... */ + continue; + } else - ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]); + ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]); /* Re-set iovecs if present. */ if (tm->threads[thread_index].iovecs) @@ -219,15 +220,17 @@ tapcli_tx (vlib_main_t * vm, if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) { - do { - b = vlib_get_buffer (vm, b->next_buffer); + do + { + b = vlib_get_buffer (vm, b->next_buffer); - vec_add2 (tm->threads[thread_index].iovecs, iov, 1); + vec_add2 (tm->threads[thread_index].iovecs, iov, 1); - iov->iov_base = b->data + b->current_data; - iov->iov_len = b->current_length; - l += b->current_length; - } while (b->flags & VLIB_BUFFER_NEXT_PRESENT); + iov->iov_base = b->data + b->current_data; + iov->iov_len = b->current_length; + l += b->current_length; + } + while (b->flags & VLIB_BUFFER_NEXT_PRESENT); } if (writev (ti->unix_fd, tm->threads[thread_index].iovecs, @@ -235,17 +238,19 @@ tapcli_tx (vlib_main_t * vm, clib_unix_warning ("writev"); } - vlib_buffer_free(vm, vlib_frame_vector_args(frame), frame->n_vectors); + vlib_buffer_free (vm, vlib_frame_vector_args (frame), frame->n_vectors); return n_packets; } +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (tapcli_tx_node,static) = { .function = tapcli_tx, .name = "tapcli-tx", .type = VLIB_NODE_TYPE_INTERNAL, .vector_size = 4, }; +/* *INDENT-ON* */ /** * @brief Dispatch tapcli RX node function for node tap_cli_rx @@ -258,141 +263,164 @@ VLIB_REGISTER_NODE (tapcli_tx_node,static) = { * @return n_packets - uword * */ -static uword tapcli_rx_iface(vlib_main_t * vm, - vlib_node_runtime_t * node, - tapcli_interface_t * ti) +static uword +tapcli_rx_iface (vlib_main_t * vm, + vlib_node_runtime_t * node, tapcli_interface_t * ti) { - tapcli_main_t * tm = &tapcli_main; + tapcli_main_t *tm = &tapcli_main; const uword buffer_size = VLIB_BUFFER_DATA_SIZE; u32 n_trace = vlib_get_trace_count (vm, node); u8 set_trace = 0; u16 thread_index = vlib_get_thread_index (); vnet_main_t *vnm; - vnet_sw_interface_t * si; + vnet_sw_interface_t *si; u8 admin_down; u32 next = node->cached_next_index; u32 n_left_to_next, next_index; u32 *to_next; - vnm = vnet_get_main(); + vnm = vnet_get_main (); si = vnet_get_sw_interface (vnm, ti->sw_if_index); admin_down = !(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP); - vlib_get_next_frame(vm, node, next, to_next, n_left_to_next); - - while (n_left_to_next) { // Fill at most one vector - vlib_buffer_t *b_first, *b, *prev; - u32 bi_first, bi; - word n_bytes_in_packet; - int j, n_bytes_left; - - if (PREDICT_FALSE(vec_len(tm->threads[thread_index].rx_buffers) < - tm->mtu_buffers)) { - uword len = vec_len(tm->threads[thread_index].rx_buffers); - _vec_len(tm->threads[thread_index].rx_buffers) += - vlib_buffer_alloc_from_free_list(vm, &tm->threads[thread_index].rx_buffers[len], - VLIB_FRAME_SIZE - len, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); - if (PREDICT_FALSE(vec_len(tm->threads[thread_index].rx_buffers) < - tm->mtu_buffers)) { - vlib_node_increment_counter(vm, tapcli_rx_node.index, - TAPCLI_ERROR_BUFFER_ALLOC, - tm->mtu_buffers - - vec_len(tm->threads[thread_index].rx_buffers)); - break; - } - } - - uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1; - - /* Allocate RX buffers from end of rx_buffers. - Turn them into iovecs to pass to readv. */ - vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1); - for (j = 0; j < tm->mtu_buffers; j++) { - b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx - j]); - tm->threads[thread_index].iovecs[j].iov_base = b->data; - tm->threads[thread_index].iovecs[j].iov_len = buffer_size; - } + vlib_get_next_frame (vm, node, next, to_next, n_left_to_next); - n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs, - tm->mtu_buffers); - n_bytes_in_packet = n_bytes_left; - if (n_bytes_left <= 0) { - if (errno != EAGAIN) { - vlib_node_increment_counter(vm, tapcli_rx_node.index, - TAPCLI_ERROR_READ, 1); - } - break; - } + while (n_left_to_next) + { // Fill at most one vector + vlib_buffer_t *b_first, *b, *prev; + u32 bi_first, bi; + word n_bytes_in_packet; + int j, n_bytes_left; - bi_first = tm->threads[thread_index].rx_buffers[i_rx]; - b = b_first = vlib_get_buffer (vm, - tm->threads[thread_index].rx_buffers[i_rx]); - prev = NULL; + if (PREDICT_FALSE (vec_len (tm->threads[thread_index].rx_buffers) < + tm->mtu_buffers)) + { + uword len = vec_len (tm->threads[thread_index].rx_buffers); + _vec_len (tm->threads[thread_index].rx_buffers) += + vlib_buffer_alloc_from_free_list (vm, + &tm->threads[thread_index]. + rx_buffers[len], + VLIB_FRAME_SIZE - len, + VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); + if (PREDICT_FALSE + (vec_len (tm->threads[thread_index].rx_buffers) < + tm->mtu_buffers)) + { + vlib_node_increment_counter (vm, tapcli_rx_node.index, + TAPCLI_ERROR_BUFFER_ALLOC, + tm->mtu_buffers - + vec_len (tm->threads + [thread_index]. + rx_buffers)); + break; + } + } - while (1) { - b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size; - n_bytes_left -= buffer_size; + uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1; - if (prev) { - prev->next_buffer = bi; - prev->flags |= VLIB_BUFFER_NEXT_PRESENT; - } - prev = b; + /* Allocate RX buffers from end of rx_buffers. + Turn them into iovecs to pass to readv. */ + vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1); + for (j = 0; j < tm->mtu_buffers; j++) + { + b = + vlib_get_buffer (vm, + tm->threads[thread_index].rx_buffers[i_rx - j]); + tm->threads[thread_index].iovecs[j].iov_base = b->data; + tm->threads[thread_index].iovecs[j].iov_len = buffer_size; + } - /* last segment */ + n_bytes_left = readv (ti->unix_fd, tm->threads[thread_index].iovecs, + tm->mtu_buffers); + n_bytes_in_packet = n_bytes_left; if (n_bytes_left <= 0) - break; + { + if (errno != EAGAIN) + { + vlib_node_increment_counter (vm, tapcli_rx_node.index, + TAPCLI_ERROR_READ, 1); + } + break; + } - i_rx--; - bi = tm->threads[thread_index].rx_buffers[i_rx]; - b = vlib_get_buffer (vm, bi); - } + bi_first = tm->threads[thread_index].rx_buffers[i_rx]; + b = b_first = vlib_get_buffer (vm, + tm->threads[thread_index]. + rx_buffers[i_rx]); + prev = NULL; - _vec_len (tm->threads[thread_index].rx_buffers) = i_rx; + while (1) + { + b->current_length = + n_bytes_left < buffer_size ? n_bytes_left : buffer_size; + n_bytes_left -= buffer_size; + + if (prev) + { + prev->next_buffer = bi; + prev->flags |= VLIB_BUFFER_NEXT_PRESENT; + } + prev = b; + + /* last segment */ + if (n_bytes_left <= 0) + break; + + i_rx--; + bi = tm->threads[thread_index].rx_buffers[i_rx]; + b = vlib_get_buffer (vm, bi); + } - b_first->total_length_not_including_first_buffer = - (n_bytes_in_packet > buffer_size) ? n_bytes_in_packet - buffer_size : 0; - b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; + _vec_len (tm->threads[thread_index].rx_buffers) = i_rx; - VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b_first); + b_first->total_length_not_including_first_buffer = + (n_bytes_in_packet > + buffer_size) ? n_bytes_in_packet - buffer_size : 0; + b_first->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID; - vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index; - vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32)~0; + VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b_first); - b_first->error = node->errors[TAPCLI_ERROR_NONE]; - next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; - next_index = (ti->per_interface_next_index != ~0) ? - ti->per_interface_next_index : next_index; - next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index; + vnet_buffer (b_first)->sw_if_index[VLIB_RX] = ti->sw_if_index; + vnet_buffer (b_first)->sw_if_index[VLIB_TX] = (u32) ~ 0; - to_next[0] = bi_first; - to_next++; - n_left_to_next--; + b_first->error = node->errors[TAPCLI_ERROR_NONE]; + next_index = VNET_DEVICE_INPUT_NEXT_ETHERNET_INPUT; + next_index = (ti->per_interface_next_index != ~0) ? + ti->per_interface_next_index : next_index; + next_index = admin_down ? VNET_DEVICE_INPUT_NEXT_DROP : next_index; - vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index, b_first); + to_next[0] = bi_first; + to_next++; + n_left_to_next--; - vlib_validate_buffer_enqueue_x1 (vm, node, next, - to_next, n_left_to_next, - bi_first, next_index); + vnet_feature_start_device_input_x1 (ti->sw_if_index, &next_index, + b_first); - /* Interface counters for tapcli interface. */ - if (PREDICT_TRUE(!admin_down)) { - vlib_increment_combined_counter ( - vnet_main.interface_main.combined_sw_if_counters - + VNET_INTERFACE_COUNTER_RX, - thread_index, ti->sw_if_index, - 1, n_bytes_in_packet); + vlib_validate_buffer_enqueue_x1 (vm, node, next, + to_next, n_left_to_next, + bi_first, next_index); - if (PREDICT_FALSE(n_trace > 0)) { - vlib_trace_buffer (vm, node, next_index, - b_first, /* follow_chain */ 1); - n_trace--; - set_trace = 1; - tapcli_rx_trace_t *t0 = vlib_add_trace (vm, node, b_first, sizeof (*t0)); - t0->sw_if_index = si->sw_if_index; - } + /* Interface counters for tapcli interface. */ + if (PREDICT_TRUE (!admin_down)) + { + vlib_increment_combined_counter (vnet_main.interface_main. + combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + thread_index, ti->sw_if_index, 1, + n_bytes_in_packet); + + if (PREDICT_FALSE (n_trace > 0)) + { + vlib_trace_buffer (vm, node, next_index, + b_first, /* follow_chain */ 1); + n_trace--; + set_trace = 1; + tapcli_rx_trace_t *t0 = + vlib_add_trace (vm, node, b_first, sizeof (*t0)); + t0->sw_if_index = si->sw_if_index; + } + } } - } vlib_put_next_frame (vm, node, next, n_left_to_next); if (set_trace) vlib_set_trace_count (vm, node, n_trace); @@ -413,44 +441,46 @@ static uword tapcli_rx_iface(vlib_main_t * vm, * */ static uword -tapcli_rx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) +tapcli_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { - tapcli_main_t * tm = &tapcli_main; - static u32 * ready_interface_indices; - tapcli_interface_t * ti; + tapcli_main_t *tm = &tapcli_main; + static u32 *ready_interface_indices; + tapcli_interface_t *ti; int i; u32 total_count = 0; vec_reset_length (ready_interface_indices); + /* *INDENT-OFF* */ clib_bitmap_foreach (i, tm->pending_read_bitmap, ({ vec_add1 (ready_interface_indices, i); })); + /* *INDENT-ON* */ if (vec_len (ready_interface_indices) == 0) return 0; - for (i = 0; i < vec_len(ready_interface_indices); i++) - { - tm->pending_read_bitmap = - clib_bitmap_set (tm->pending_read_bitmap, - ready_interface_indices[i], 0); + for (i = 0; i < vec_len (ready_interface_indices); i++) + { + tm->pending_read_bitmap = + clib_bitmap_set (tm->pending_read_bitmap, + ready_interface_indices[i], 0); - ti = vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]); - total_count += tapcli_rx_iface(vm, node, ti); - } - return total_count; //This might return more than 256. + ti = + vec_elt_at_index (tm->tapcli_interfaces, ready_interface_indices[i]); + total_count += tapcli_rx_iface (vm, node, ti); + } + return total_count; //This might return more than 256. } /** TAPCLI error strings */ -static char * tapcli_rx_error_strings[] = { +static char *tapcli_rx_error_strings[] = { #define _(sym,string) string, foreach_tapcli_error #undef _ }; +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (tapcli_rx_node, static) = { .function = tapcli_rx, .name = "tapcli-rx", @@ -462,6 +492,7 @@ VLIB_REGISTER_NODE (tapcli_rx_node, static) = { .error_strings = tapcli_rx_error_strings, .format_trace = format_tapcli_rx_trace, }; +/* *INDENT-ON* */ /** @@ -472,11 +503,12 @@ VLIB_REGISTER_NODE (tapcli_rx_node, static) = { * @return error - clib_error_t * */ -static clib_error_t * tapcli_read_ready (clib_file_t * uf) +static clib_error_t * +tapcli_read_ready (clib_file_t * uf) { - vlib_main_t * vm = vlib_get_main(); - tapcli_main_t * tm = &tapcli_main; - uword * p; + vlib_main_t *vm = vlib_get_main (); + tapcli_main_t *tm = &tapcli_main; + uword *p; /** Schedule the rx node */ vlib_node_set_interrupt_pending (vm, tapcli_rx_node.index); @@ -486,7 +518,7 @@ static clib_error_t * tapcli_read_ready (clib_file_t * uf) /** Mark the specific tap interface ready-to-read */ if (p) tm->pending_read_bitmap = clib_bitmap_set (tm->pending_read_bitmap, - p[0], 1); + p[0], 1); else clib_warning ("fd %d not in hash table", uf->file_descriptor); @@ -513,16 +545,16 @@ tapcli_config (vlib_main_t * vm, unformat_input_t * input) if (unformat (input, "mtu %d", &tm->mtu_bytes)) ; else if (unformat (input, "disable")) - tm->is_disabled = 1; + tm->is_disabled = 1; else - return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); + return clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); } if (tm->is_disabled) return 0; - if (geteuid()) + if (geteuid ()) { clib_warning ("tapcli disabled: must be superuser"); tm->is_disabled = 1; @@ -543,15 +575,15 @@ tapcli_config (vlib_main_t * vm, unformat_input_t * input) * @return rc - int * */ -static int tap_name_renumber (vnet_hw_interface_t * hi, - u32 new_dev_instance) +static int +tap_name_renumber (vnet_hw_interface_t * hi, u32 new_dev_instance) { tapcli_main_t *tm = &tapcli_main; vec_validate_init_empty (tm->show_dev_instance_by_real_dev_instance, - hi->dev_instance, ~0); + hi->dev_instance, ~0); - tm->show_dev_instance_by_real_dev_instance [hi->dev_instance] = + tm->show_dev_instance_by_real_dev_instance[hi->dev_instance] = new_dev_instance; return 0; @@ -569,19 +601,20 @@ VLIB_CONFIG_FUNCTION (tapcli_config, "tapcli"); */ static void tapcli_nopunt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { - u32 * buffers = vlib_frame_args (frame); + u32 *buffers = vlib_frame_args (frame); uword n_packets = frame->n_vectors; vlib_buffer_free (vm, buffers, n_packets); vlib_frame_free (vm, node, frame); } +/* *INDENT-OFF* */ VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = { .name = "tapcli", .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, }; +/* *INDENT-ON* */ /** * @brief Formatter for TAPCLI interface name @@ -592,11 +625,12 @@ VNET_HW_INTERFACE_CLASS (tapcli_interface_class,static) = { * @return *s - formatted string * */ -static u8 * format_tapcli_interface_name (u8 * s, va_list * args) +static u8 * +format_tapcli_interface_name (u8 * s, va_list * args) { u32 i = va_arg (*args, u32); u32 show_dev_instance = ~0; - tapcli_main_t * tm = &tapcli_main; + tapcli_main_t *tm = &tapcli_main; if (i < vec_len (tm->show_dev_instance_by_real_dev_instance)) show_dev_instance = tm->show_dev_instance_by_real_dev_instance[i]; @@ -618,14 +652,13 @@ static u8 * format_tapcli_interface_name (u8 * s, va_list * args) * @return rc - u32 * */ -static u32 tapcli_flag_change (vnet_main_t * vnm, - vnet_hw_interface_t * hw, - u32 flags) +static u32 +tapcli_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hw, u32 flags) { tapcli_main_t *tm = &tapcli_main; tapcli_interface_t *ti; - ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance); + ti = vec_elt_at_index (tm->tapcli_interfaces, hw->dev_instance); if (flags & ETHERNET_INTERFACE_FLAG_MTU) { @@ -633,7 +666,7 @@ static u32 tapcli_flag_change (vnet_main_t * vnm, tm->mtu_bytes = hw->max_packet_bytes; tm->mtu_buffers = (tm->mtu_bytes + (buffer_size - 1)) / buffer_size; } - else + else { struct ifreq ifr; u32 want_promisc; @@ -642,27 +675,27 @@ static u32 tapcli_flag_change (vnet_main_t * vnm, /* get flags, modify to bring up interface... */ if (ioctl (ti->provision_fd, SIOCGIFFLAGS, &ifr) < 0) - { - clib_unix_warning ("Couldn't get interface flags for %s", hw->name); - return 0; - } + { + clib_unix_warning ("Couldn't get interface flags for %s", hw->name); + return 0; + } want_promisc = (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) != 0; if (want_promisc == ti->is_promisc) - return 0; + return 0; if (flags & ETHERNET_INTERFACE_FLAG_ACCEPT_ALL) - ifr.ifr_flags |= IFF_PROMISC; + ifr.ifr_flags |= IFF_PROMISC; else - ifr.ifr_flags &= ~(IFF_PROMISC); + ifr.ifr_flags &= ~(IFF_PROMISC); /* get flags, modify to bring up interface... */ if (ioctl (ti->provision_fd, SIOCSIFFLAGS, &ifr) < 0) - { - clib_unix_warning ("Couldn't set interface flags for %s", hw->name); - return 0; - } + { + clib_unix_warning ("Couldn't set interface flags for %s", hw->name); + return 0; + } ti->is_promisc = want_promisc; } @@ -678,9 +711,9 @@ static u32 tapcli_flag_change (vnet_main_t * vnm, * @param node_index - u32 * */ -static void tapcli_set_interface_next_node (vnet_main_t *vnm, - u32 hw_if_index, - u32 node_index) +static void +tapcli_set_interface_next_node (vnet_main_t * vnm, + u32 hw_if_index, u32 node_index) { tapcli_main_t *tm = &tapcli_main; tapcli_interface_t *ti; @@ -725,6 +758,7 @@ tapcli_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) return 0; } +/* *INDENT-OFF* */ VNET_DEVICE_CLASS (tapcli_dev_class,static) = { .name = "tapcli", .tx_function = tapcli_tx, @@ -733,6 +767,7 @@ VNET_DEVICE_CLASS (tapcli_dev_class,static) = { .name_renumber = tap_name_renumber, .admin_up_down_function = tapcli_interface_admin_up_down, }; +/* *INDENT-ON* */ /** * @brief Dump TAP interfaces @@ -742,20 +777,23 @@ VNET_DEVICE_CLASS (tapcli_dev_class,static) = { * @return rc - int * */ -int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids) +int +vnet_tap_dump_ifs (tapcli_interface_details_t ** out_tapids) { - tapcli_main_t * tm = &tapcli_main; - tapcli_interface_t * ti; + tapcli_main_t *tm = &tapcli_main; + tapcli_interface_t *ti; - tapcli_interface_details_t * r_tapids = NULL; - tapcli_interface_details_t * tapid = NULL; + tapcli_interface_details_t *r_tapids = NULL; + tapcli_interface_details_t *tapid = NULL; - vec_foreach (ti, tm->tapcli_interfaces) { + vec_foreach (ti, tm->tapcli_interfaces) + { if (!ti->active) - continue; - vec_add2(r_tapids, tapid, 1); + continue; + vec_add2 (r_tapids, tapid, 1); tapid->sw_if_index = ti->sw_if_index; - strncpy((char *)tapid->dev_name, ti->ifr.ifr_name, sizeof (ti->ifr.ifr_name)-1); + strncpy ((char *) tapid->dev_name, ti->ifr.ifr_name, + sizeof (ti->ifr.ifr_name) - 1); } *out_tapids = r_tapids; @@ -769,23 +807,26 @@ int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids) * @return interface - tapcli_interface_t * */ -static tapcli_interface_t *tapcli_get_new_tapif() +static tapcli_interface_t * +tapcli_get_new_tapif () { - tapcli_main_t * tm = &tapcli_main; + tapcli_main_t *tm = &tapcli_main; tapcli_interface_t *ti = NULL; - int inactive_cnt = vec_len(tm->tapcli_inactive_interfaces); + int inactive_cnt = vec_len (tm->tapcli_inactive_interfaces); // if there are any inactive ifaces - if (inactive_cnt > 0) { - // take last - u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1]; - if (vec_len(tm->tapcli_interfaces) > ti_idx) { - ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx); - clib_warning("reusing tap interface"); + if (inactive_cnt > 0) + { + // take last + u32 ti_idx = tm->tapcli_inactive_interfaces[inactive_cnt - 1]; + if (vec_len (tm->tapcli_interfaces) > ti_idx) + { + ti = vec_elt_at_index (tm->tapcli_interfaces, ti_idx); + clib_warning ("reusing tap interface"); + } + // "remove" from inactive list + _vec_len (tm->tapcli_inactive_interfaces) -= 1; } - // "remove" from inactive list - _vec_len(tm->tapcli_inactive_interfaces) -= 1; - } // ti was not retrieved from inactive ifaces - create new if (!ti) @@ -794,32 +835,33 @@ static tapcli_interface_t *tapcli_get_new_tapif() return ti; } -typedef struct +typedef struct { - ip6_address_t addr; - u32 mask_width; - unsigned int ifindex; + ip6_address_t addr; + u32 mask_width; + unsigned int ifindex; } ip6_ifreq_t; /** * @brief Connect a TAP interface * * @param vm - vlib_main_t - * @param ap - vnet_tap_connect_args_t + * @param ap - vnet_tap_connect_args_t * * @return rc - int * */ -int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) +int +vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t * ap) { - tapcli_main_t * tm = &tapcli_main; - tapcli_interface_t * ti = NULL; + tapcli_main_t *tm = &tapcli_main; + tapcli_interface_t *ti = NULL; struct ifreq ifr; int flags; int dev_net_tun_fd; int dev_tap_fd = -1; - clib_error_t * error; - u8 hwaddr [6]; + clib_error_t *error; + u8 hwaddr[6]; int rv = 0; if (tm->is_disabled) @@ -833,17 +875,16 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) return VNET_API_ERROR_SYSCALL_ERROR_1; memset (&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1); + strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name) - 1); ifr.ifr_flags = flags; - if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0) + if (ioctl (dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0) { rv = VNET_API_ERROR_SYSCALL_ERROR_2; goto error; } - + /* Open a provisioning socket */ - if ((dev_tap_fd = socket(PF_PACKET, SOCK_RAW, - htons(ETH_P_ALL))) < 0 ) + if ((dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) { rv = VNET_API_ERROR_SYSCALL_ERROR_3; goto error; @@ -854,24 +895,25 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) struct ifreq ifr; struct sockaddr_ll sll; - memset (&ifr, 0, sizeof(ifr)); - strncpy (ifr.ifr_name, (char *) ap->intfc_name, sizeof (ifr.ifr_name)-1); - if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 ) + memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, (char *) ap->intfc_name, + sizeof (ifr.ifr_name) - 1); + if (ioctl (dev_tap_fd, SIOCGIFINDEX, &ifr) < 0) { - rv = VNET_API_ERROR_SYSCALL_ERROR_4; - goto error; + rv = VNET_API_ERROR_SYSCALL_ERROR_4; + goto error; } /* Bind the provisioning socket to the interface. */ - memset(&sll, 0, sizeof(sll)); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = ifr.ifr_ifindex; - sll.sll_protocol = htons(ETH_P_ALL); + memset (&sll, 0, sizeof (sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifr.ifr_ifindex; + sll.sll_protocol = htons (ETH_P_ALL); - if (bind(dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0) + if (bind (dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) { - rv = VNET_API_ERROR_SYSCALL_ERROR_5; - goto error; + rv = VNET_API_ERROR_SYSCALL_ERROR_5; + goto error; } } @@ -880,7 +922,7 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) int one = 1; if (ioctl (dev_net_tun_fd, FIONBIO, &one) < 0) { - rv = VNET_API_ERROR_SYSCALL_ERROR_6; + rv = VNET_API_ERROR_SYSCALL_ERROR_6; goto error; } } @@ -912,35 +954,35 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) /* ip4: mask defaults to /24 */ u32 mask = clib_host_to_net_u32 (0xFFFFFF00); - memset(&sin, 0, sizeof(sin)); + memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; /* sin.sin_port = 0; */ sin.sin_addr.s_addr = ap->ip4_address->as_u32; memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin)); if (ioctl (dev_tap_fd, SIOCSIFADDR, &ifr) < 0) - { - rv = VNET_API_ERROR_SYSCALL_ERROR_10; - goto error; - } + { + rv = VNET_API_ERROR_SYSCALL_ERROR_10; + goto error; + } if (ap->ip4_mask_width > 0 && ap->ip4_mask_width < 33) - { - mask = ~0; - mask <<= (32 - ap->ip4_mask_width); - } + { + mask = ~0; + mask <<= (32 - ap->ip4_mask_width); + } - mask = clib_host_to_net_u32(mask); + mask = clib_host_to_net_u32 (mask); sin.sin_family = AF_INET; sin.sin_port = 0; sin.sin_addr.s_addr = mask; memcpy (&ifr.ifr_ifru.ifru_addr, &sin, sizeof (sin)); if (ioctl (dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0) - { - rv = VNET_API_ERROR_SYSCALL_ERROR_10; - goto error; - } + { + rv = VNET_API_ERROR_SYSCALL_ERROR_10; + goto error; + } } if (ap->ip6_address_set) @@ -949,61 +991,60 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) ip6_ifreq_t ifr6; int sockfd6; - sockfd6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_IP); + sockfd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_IP); if (sockfd6 < 0) - { - rv = VNET_API_ERROR_SYSCALL_ERROR_10; - goto error; - } - - memset (&ifr2, 0, sizeof(ifr)); - strncpy (ifr2.ifr_name, (char *) ap->intfc_name, - sizeof (ifr2.ifr_name)-1); - if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0 ) - { - close (sockfd6); - rv = VNET_API_ERROR_SYSCALL_ERROR_4; - goto error; - } - + { + rv = VNET_API_ERROR_SYSCALL_ERROR_10; + goto error; + } + + memset (&ifr2, 0, sizeof (ifr)); + strncpy (ifr2.ifr_name, (char *) ap->intfc_name, + sizeof (ifr2.ifr_name) - 1); + if (ioctl (sockfd6, SIOCGIFINDEX, &ifr2) < 0) + { + close (sockfd6); + rv = VNET_API_ERROR_SYSCALL_ERROR_4; + goto error; + } + memcpy (&ifr6.addr, ap->ip6_address, sizeof (ip6_address_t)); ifr6.mask_width = ap->ip6_mask_width; ifr6.ifindex = ifr2.ifr_ifindex; - + if (ioctl (sockfd6, SIOCSIFADDR, &ifr6) < 0) - { - close (sockfd6); - clib_unix_warning ("ifr6"); - rv = VNET_API_ERROR_SYSCALL_ERROR_10; - goto error; - } + { + close (sockfd6); + clib_unix_warning ("ifr6"); + rv = VNET_API_ERROR_SYSCALL_ERROR_10; + goto error; + } close (sockfd6); } - ti = tapcli_get_new_tapif(); + ti = tapcli_get_new_tapif (); ti->per_interface_next_index = ~0; if (ap->hwaddr_arg != 0) - clib_memcpy(hwaddr, ap->hwaddr_arg, 6); + clib_memcpy (hwaddr, ap->hwaddr_arg, 6); else { - f64 now = vlib_time_now(vm); + f64 now = vlib_time_now (vm); u32 rnd; rnd = (u32) (now * 1e6); rnd = random_u32 (&rnd); - memcpy (hwaddr+2, &rnd, sizeof(rnd)); + memcpy (hwaddr + 2, &rnd, sizeof (rnd)); hwaddr[0] = 2; hwaddr[1] = 0xfe; } error = ethernet_register_interface - (tm->vnet_main, - tapcli_dev_class.index, - ti - tm->tapcli_interfaces /* device instance */, - hwaddr /* ethernet address */, - &ti->hw_if_index, - tapcli_flag_change); + (tm->vnet_main, + tapcli_dev_class.index, + ti - tm->tapcli_interfaces /* device instance */ , + hwaddr /* ethernet address */ , + &ti->hw_if_index, tapcli_flag_change); if (error) { @@ -1013,7 +1054,7 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) } { - clib_file_t template = {0}; + clib_file_t template = { 0 }; template.read_function = tapcli_read_ready; template.file_descriptor = dev_net_tun_fd; ti->clib_file_index = clib_file_add (&file_main, &template); @@ -1021,32 +1062,33 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) ti->provision_fd = dev_tap_fd; clib_memcpy (&ti->ifr, &ifr, sizeof (ifr)); } - + { - vnet_hw_interface_t * hw; + vnet_hw_interface_t *hw; hw = vnet_get_hw_interface (tm->vnet_main, ti->hw_if_index); hw->min_supported_packet_bytes = TAP_MTU_MIN; hw->max_supported_packet_bytes = TAP_MTU_MAX; - hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = hw->max_supported_packet_bytes - sizeof(ethernet_header_t); + hw->max_l3_packet_bytes[VLIB_RX] = hw->max_l3_packet_bytes[VLIB_TX] = + hw->max_supported_packet_bytes - sizeof (ethernet_header_t); ti->sw_if_index = hw->sw_if_index; if (ap->sw_if_indexp) *(ap->sw_if_indexp) = hw->sw_if_index; } ti->active = 1; - + hash_set (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index, - ti - tm->tapcli_interfaces); - + ti - tm->tapcli_interfaces); + hash_set (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd, - ti - tm->tapcli_interfaces); - + ti - tm->tapcli_interfaces); + return rv; - error: +error: close (dev_net_tun_fd); if (dev_tap_fd >= 0) - close (dev_tap_fd); + close (dev_tap_fd); return rv; } @@ -1064,13 +1106,14 @@ int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *ap) * @return rc - int * */ -int vnet_tap_connect_renumber (vlib_main_t * vm, - vnet_tap_connect_args_t *ap) +int +vnet_tap_connect_renumber (vlib_main_t * vm, vnet_tap_connect_args_t * ap) { - int rv = vnet_tap_connect(vm, ap); + int rv = vnet_tap_connect (vm, ap); if (!rv && ap->renumber) - vnet_interface_name_renumber (*(ap->sw_if_indexp), ap->custom_dev_instance); + vnet_interface_name_renumber (*(ap->sw_if_indexp), + ap->custom_dev_instance); return rv; } @@ -1083,26 +1126,28 @@ int vnet_tap_connect_renumber (vlib_main_t * vm, * @return rc - int * */ -static int tapcli_tap_disconnect (tapcli_interface_t *ti) +static int +tapcli_tap_disconnect (tapcli_interface_t * ti) { int rv = 0; - vnet_main_t * vnm = vnet_get_main(); - tapcli_main_t * tm = &tapcli_main; + vnet_main_t *vnm = vnet_get_main (); + tapcli_main_t *tm = &tapcli_main; u32 sw_if_index = ti->sw_if_index; // bring interface down vnet_sw_interface_set_flags (vnm, sw_if_index, 0); - if (ti->clib_file_index != ~0) { - clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index); - ti->clib_file_index = ~0; - } + if (ti->clib_file_index != ~0) + { + clib_file_del (&file_main, file_main.file_pool + ti->clib_file_index); + ti->clib_file_index = ~0; + } else - close(ti->unix_fd); + close (ti->unix_fd); hash_unset (tm->tapcli_interface_index_by_unix_fd, ti->unix_fd); hash_unset (tm->tapcli_interface_index_by_sw_if_index, ti->sw_if_index); - close(ti->provision_fd); + close (ti->provision_fd); ti->unix_fd = -1; ti->provision_fd = -1; @@ -1118,26 +1163,27 @@ static int tapcli_tap_disconnect (tapcli_interface_t *ti) * @return rc - int * */ -int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index) +int +vnet_tap_delete (vlib_main_t * vm, u32 sw_if_index) { int rv = 0; - tapcli_main_t * tm = &tapcli_main; + tapcli_main_t *tm = &tapcli_main; tapcli_interface_t *ti; uword *p = NULL; - p = hash_get (tm->tapcli_interface_index_by_sw_if_index, - sw_if_index); - if (p == 0) { - clib_warning ("sw_if_index %d unknown", sw_if_index); - return VNET_API_ERROR_INVALID_SW_IF_INDEX; - } + p = hash_get (tm->tapcli_interface_index_by_sw_if_index, sw_if_index); + if (p == 0) + { + clib_warning ("sw_if_index %d unknown", sw_if_index); + return VNET_API_ERROR_INVALID_SW_IF_INDEX; + } ti = vec_elt_at_index (tm->tapcli_interfaces, p[0]); // inactive ti->active = 0; - tapcli_tap_disconnect(ti); + tapcli_tap_disconnect (ti); // add to inactive list - vec_add1(tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces); + vec_add1 (tm->tapcli_inactive_interfaces, ti - tm->tapcli_interfaces); // reset renumbered iface if (p[0] < vec_len (tm->show_dev_instance_by_real_dev_instance)) @@ -1159,10 +1205,9 @@ int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index) */ static clib_error_t * tap_delete_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - tapcli_main_t * tm = &tapcli_main; + tapcli_main_t *tm = &tapcli_main; u32 sw_if_index = ~0; if (tm->is_disabled) @@ -1171,29 +1216,35 @@ tap_delete_command_fn (vlib_main_t * vm, } if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main, - &sw_if_index)) - ; + &sw_if_index)) + ; else return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); + format_unformat_error, input); int rc = vnet_tap_delete (vm, sw_if_index); - if (!rc) { - vlib_cli_output (vm, "Deleted."); - } else { - vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", rc); - } + if (!rc) + { + vlib_cli_output (vm, "Deleted."); + } + else + { + vlib_cli_output (vm, "Error during deletion of tap interface. (rc: %d)", + rc); + } return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_delete_command, static) = { .path = "tap delete", .short_help = "tap delete ", .function = tap_delete_command_fn, }; +/* *INDENT-ON* */ /** * @brief Modifies tap interface - can result in new interface being created @@ -1209,16 +1260,17 @@ VLIB_CLI_COMMAND (tap_delete_command, static) = { * @return rc - int * */ -int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t *ap) +int +vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t * ap) { - int rv = vnet_tap_delete (vm, ap->orig_sw_if_index); + int rv = vnet_tap_delete (vm, ap->orig_sw_if_index); - if (rv) - return rv; + if (rv) + return rv; - rv = vnet_tap_connect_renumber(vm, ap); + rv = vnet_tap_connect_renumber (vm, ap); - return rv; + return rv; } /** @@ -1233,16 +1285,15 @@ int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t *ap) */ static clib_error_t * tap_modify_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - u8 * intfc_name; - tapcli_main_t * tm = &tapcli_main; + u8 *intfc_name; + tapcli_main_t *tm = &tapcli_main; u32 sw_if_index = ~0; u32 new_sw_if_index = ~0; int user_hwaddr = 0; u8 hwaddr[6]; - vnet_tap_connect_args_t _a, *ap= &_a; + vnet_tap_connect_args_t _a, *ap = &_a; if (tm->is_disabled) { @@ -1250,24 +1301,23 @@ tap_modify_command_fn (vlib_main_t * vm, } if (unformat (input, "%U", unformat_vnet_sw_interface, tm->vnet_main, - &sw_if_index)) - ; + &sw_if_index)) + ; else return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); + format_unformat_error, input); if (unformat (input, "%s", &intfc_name)) ; else return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); + format_unformat_error, input); - if (unformat(input, "hwaddr %U", unformat_ethernet_address, - &hwaddr)) + if (unformat (input, "hwaddr %U", unformat_ethernet_address, &hwaddr)) user_hwaddr = 1; - memset (ap, 0, sizeof(*ap)); + memset (ap, 0, sizeof (*ap)); ap->orig_sw_if_index = sw_if_index; ap->intfc_name = intfc_name; ap->sw_if_indexp = &new_sw_if_index; @@ -1276,22 +1326,29 @@ tap_modify_command_fn (vlib_main_t * vm, int rc = vnet_tap_modify (vm, ap); - if (!rc) { - vlib_cli_output (vm, "Modified %U for Linux tap '%s'", - format_vnet_sw_if_index_name, tm->vnet_main, - *(ap->sw_if_indexp), ap->intfc_name); - } else { - vlib_cli_output (vm, "Error during modification of tap interface. (rc: %d)", rc); - } + if (!rc) + { + vlib_cli_output (vm, "Modified %U for Linux tap '%s'", + format_vnet_sw_if_index_name, tm->vnet_main, + *(ap->sw_if_indexp), ap->intfc_name); + } + else + { + vlib_cli_output (vm, + "Error during modification of tap interface. (rc: %d)", + rc); + } return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_modify_command, static) = { .path = "tap modify", .short_help = "tap modify [hwaddr ]", .function = tap_modify_command_fn, }; +/* *INDENT-ON* */ /** * @brief CLI function to connect TAP interface @@ -1305,13 +1362,12 @@ VLIB_CLI_COMMAND (tap_modify_command, static) = { */ static clib_error_t * tap_connect_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - u8 * intfc_name = 0; + u8 *intfc_name = 0; unformat_input_t _line_input, *line_input = &_line_input; - vnet_tap_connect_args_t _a, *ap= &_a; - tapcli_main_t * tm = &tapcli_main; + vnet_tap_connect_args_t _a, *ap = &_a; + tapcli_main_t *tm = &tapcli_main; u8 hwaddr[6]; u8 *hwaddr_arg = 0; u32 sw_if_index; @@ -1331,32 +1387,32 @@ tap_connect_command_fn (vlib_main_t * vm, while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { - if (unformat(line_input, "hwaddr %U", unformat_ethernet_address, - &hwaddr)) - hwaddr_arg = hwaddr; - + if (unformat (line_input, "hwaddr %U", unformat_ethernet_address, + &hwaddr)) + hwaddr_arg = hwaddr; + /* It is here for backward compatibility */ - else if (unformat(line_input, "hwaddr random")) - ; - + else if (unformat (line_input, "hwaddr random")) + ; + else if (unformat (line_input, "address %U/%d", - unformat_ip4_address, &ip4_address, &ip4_mask_width)) - ip4_address_set = 1; - + unformat_ip4_address, &ip4_address, &ip4_mask_width)) + ip4_address_set = 1; + else if (unformat (line_input, "address %U/%d", - unformat_ip6_address, &ip6_address, &ip6_mask_width)) - ip6_address_set = 1; - + unformat_ip6_address, &ip6_address, &ip6_mask_width)) + ip6_address_set = 1; + else if (unformat (line_input, "%s", &intfc_name)) - ; + ; else - { - error = clib_error_return (0, "unknown input `%U'", - format_unformat_error, line_input); - goto done; - } + { + error = clib_error_return (0, "unknown input `%U'", + format_unformat_error, line_input); + goto done; + } } - + if (intfc_name == 0) { error = clib_error_return (0, "interface name must be specified"); @@ -1382,64 +1438,65 @@ tap_connect_command_fn (vlib_main_t * vm, ap->sw_if_indexp = &sw_if_index; - int rv = vnet_tap_connect(vm, ap); + int rv = vnet_tap_connect (vm, ap); - switch (rv) + switch (rv) { case VNET_API_ERROR_SYSCALL_ERROR_1: error = clib_error_return (0, "Couldn't open /dev/net/tun"); goto done; - + case VNET_API_ERROR_SYSCALL_ERROR_2: - error = clib_error_return (0, "Error setting flags on '%s'", intfc_name); + error = + clib_error_return (0, "Error setting flags on '%s'", intfc_name); goto done; case VNET_API_ERROR_SYSCALL_ERROR_3: - error = clib_error_return (0, "Couldn't open provisioning socket"); + error = clib_error_return (0, "Couldn't open provisioning socket"); goto done; case VNET_API_ERROR_SYSCALL_ERROR_4: - error = clib_error_return (0, "Couldn't get if_index"); + error = clib_error_return (0, "Couldn't get if_index"); goto done; - + case VNET_API_ERROR_SYSCALL_ERROR_5: - error = clib_error_return (0, "Couldn't bind provisioning socket"); + error = clib_error_return (0, "Couldn't bind provisioning socket"); goto done; case VNET_API_ERROR_SYSCALL_ERROR_6: - error = clib_error_return (0, "Couldn't set device non-blocking flag"); + error = clib_error_return (0, "Couldn't set device non-blocking flag"); goto done; case VNET_API_ERROR_SYSCALL_ERROR_7: - error = clib_error_return (0, "Couldn't set device MTU"); + error = clib_error_return (0, "Couldn't set device MTU"); goto done; case VNET_API_ERROR_SYSCALL_ERROR_8: - error = clib_error_return (0, "Couldn't get interface flags"); + error = clib_error_return (0, "Couldn't get interface flags"); goto done; case VNET_API_ERROR_SYSCALL_ERROR_9: - error = clib_error_return (0, "Couldn't set intfc admin state up"); + error = clib_error_return (0, "Couldn't set intfc admin state up"); goto done; case VNET_API_ERROR_SYSCALL_ERROR_10: - error = clib_error_return (0, "Couldn't set intfc address/mask"); + error = clib_error_return (0, "Couldn't set intfc address/mask"); goto done; case VNET_API_ERROR_INVALID_REGISTRATION: - error = clib_error_return (0, "Invalid registration"); + error = clib_error_return (0, "Invalid registration"); goto done; case 0: break; default: - error = clib_error_return (0, "Unknown error: %d", rv); + error = clib_error_return (0, "Unknown error: %d", rv); goto done; } - vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, - vnet_get_main(), sw_if_index); + vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, + vnet_get_main (), sw_if_index); done: unformat_free (line_input); @@ -1447,12 +1504,14 @@ done: return error; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (tap_connect_command, static) = { .path = "tap connect", .short_help = - "tap connect [address /mw] [hwaddr ]", + "tap connect [address /mw] [hwaddr ]", .function = tap_connect_command_fn, }; +/* *INDENT-ON* */ /** * @brief TAPCLI main init @@ -1465,27 +1524,35 @@ VLIB_CLI_COMMAND (tap_connect_command, static) = { clib_error_t * tapcli_init (vlib_main_t * vm) { - tapcli_main_t * tm = &tapcli_main; - vlib_thread_main_t * m = vlib_get_thread_main (); - tapcli_per_thread_t * thread; + tapcli_main_t *tm = &tapcli_main; + vlib_thread_main_t *m = vlib_get_thread_main (); + tapcli_per_thread_t *thread; tm->vlib_main = vm; - tm->vnet_main = vnet_get_main(); + tm->vnet_main = vnet_get_main (); tm->mtu_bytes = TAP_MTU_DEFAULT; - tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof(uword)); + tm->tapcli_interface_index_by_sw_if_index = hash_create (0, sizeof (uword)); tm->tapcli_interface_index_by_unix_fd = hash_create (0, sizeof (uword)); vm->os_punt_frame = tapcli_nopunt_frame; vec_validate_aligned (tm->threads, m->n_vlib_mains - 1, CLIB_CACHE_LINE_BYTES); vec_foreach (thread, tm->threads) - { - thread->iovecs = 0; - thread->rx_buffers = 0; - vec_alloc(thread->rx_buffers, VLIB_FRAME_SIZE); - vec_reset_length(thread->rx_buffers); - } + { + thread->iovecs = 0; + thread->rx_buffers = 0; + vec_alloc (thread->rx_buffers, VLIB_FRAME_SIZE); + vec_reset_length (thread->rx_buffers); + } return 0; } VLIB_INIT_FUNCTION (tapcli_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/tapcli.h b/src/vnet/unix/tapcli.h index fcd82dbf25d..55a5a152b9c 100644 --- a/src/vnet/unix/tapcli.h +++ b/src/vnet/unix/tapcli.h @@ -30,23 +30,33 @@ _(BUFFER_ALLOC, "buffer allocation error") \ _(UNKNOWN, "unknown error") -typedef enum { +typedef enum +{ #define _(sym,str) TAPCLI_ERROR_##sym, foreach_tapcli_error #undef _ - TAPCLI_N_ERROR, - } tapcli_error_t; + TAPCLI_N_ERROR, +} tapcli_error_t; /** TAP CLI interface details struct */ -typedef struct { +typedef struct +{ u32 sw_if_index; u8 dev_name[64]; } tapcli_interface_details_t; -int vnet_tap_dump_ifs (tapcli_interface_details_t **out_tapids); +int vnet_tap_dump_ifs (tapcli_interface_details_t ** out_tapids); #define TAP_MTU_MIN 68 #define TAP_MTU_MAX 65535 #define TAP_MTU_DEFAULT 1500 #endif /* __included_tapcli_h__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/tuntap.c b/src/vnet/unix/tuntap.c index 6e2a53fefd2..5d785ac6f1c 100644 --- a/src/vnet/unix/tuntap.c +++ b/src/vnet/unix/tuntap.c @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * tuntap.c - kernel stack (reverse) punt/inject path * @@ -55,13 +55,14 @@ static vnet_device_class_t tuntap_dev_class; static vnet_hw_interface_class_t tuntap_interface_class; static void tuntap_punt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame); + vlib_node_runtime_t * node, + vlib_frame_t * frame); static void tuntap_nopunt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame); + vlib_node_runtime_t * node, + vlib_frame_t * frame); -typedef struct { +typedef struct +{ u32 sw_if_index; u8 is_v6; u8 addr[16]; @@ -74,18 +75,19 @@ typedef struct { /** Vector of VLIB rx buffers to use. We allocate them in blocks of VLIB_FRAME_SIZE (256). */ - u32 * rx_buffers; + u32 *rx_buffers; /** Vector of iovecs for readv/writev calls. */ - struct iovec * iovecs; + struct iovec *iovecs; } tuntap_per_thread_t; /** * @brief TUNTAP node main state */ -typedef struct { +typedef struct +{ /** per thread variables */ - tuntap_per_thread_t * threads; + tuntap_per_thread_t *threads; /** File descriptors for /dev/net/tun and provisioning socket. */ int dev_net_tun_fd, dev_tap_fd; @@ -104,7 +106,7 @@ typedef struct { u32 mtu_bytes, mtu_buffers; /** Linux interface name for tun device. */ - char * tun_name; + char *tun_name; /** Pool of subinterface addresses */ subif_address_t *subifs; @@ -142,13 +144,11 @@ static tuntap_main_t tuntap_main = { * */ static uword -tuntap_tx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) +tuntap_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { - u32 * buffers = vlib_frame_args (frame); + u32 *buffers = vlib_frame_args (frame); uword n_packets = frame->n_vectors; - tuntap_main_t * tm = &tuntap_main; + tuntap_main_t *tm = &tuntap_main; vnet_main_t *vnm = vnet_get_main (); vnet_interface_main_t *im = &vnm->interface_main; u32 n_bytes = 0; @@ -157,17 +157,17 @@ tuntap_tx (vlib_main_t * vm, for (i = 0; i < n_packets; i++) { - struct iovec * iov; - vlib_buffer_t * b; + struct iovec *iov; + vlib_buffer_t *b; uword l; b = vlib_get_buffer (vm, buffers[i]); if (tm->is_ether && (!tm->have_normal_interface)) - { - vlib_buffer_reset(b); - clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6); - } + { + vlib_buffer_reset (b); + clib_memcpy (vlib_buffer_get_current (b), tm->ether_dst_mac, 6); + } /* Re-set iovecs if present. */ if (tm->threads[thread_index].iovecs) @@ -180,15 +180,17 @@ tuntap_tx (vlib_main_t * vm, if (PREDICT_FALSE (b->flags & VLIB_BUFFER_NEXT_PRESENT)) { - do { - b = vlib_get_buffer (vm, b->next_buffer); + do + { + b = vlib_get_buffer (vm, b->next_buffer); - vec_add2 (tm->threads[thread_index].iovecs, iov, 1); + vec_add2 (tm->threads[thread_index].iovecs, iov, 1); - iov->iov_base = b->data + b->current_data; - iov->iov_len = b->current_length; - l += b->current_length; - } while (b->flags & VLIB_BUFFER_NEXT_PRESENT); + iov->iov_base = b->data + b->current_data; + iov->iov_len = b->current_length; + l += b->current_length; + } + while (b->flags & VLIB_BUFFER_NEXT_PRESENT); } if (writev (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, @@ -214,12 +216,14 @@ tuntap_tx (vlib_main_t * vm, return n_packets; } +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (tuntap_tx_node,static) = { .function = tuntap_tx, .name = "tuntap-tx", .type = VLIB_NODE_TYPE_INTERNAL, .vector_size = 4, }; +/* *INDENT-ON* */ /** * @brief TUNTAP receive node @@ -233,12 +237,10 @@ VLIB_REGISTER_NODE (tuntap_tx_node,static) = { * */ static uword -tuntap_rx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) +tuntap_rx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { - tuntap_main_t * tm = &tuntap_main; - vlib_buffer_t * b; + tuntap_main_t *tm = &tuntap_main; + vlib_buffer_t *b; u32 bi; const uword buffer_size = VLIB_BUFFER_DATA_SIZE; u16 thread_index = vlib_get_thread_index (); @@ -250,10 +252,13 @@ tuntap_rx (vlib_main_t * vm, if (n_left < VLIB_FRAME_SIZE / 2) { - if (! tm->threads[thread_index].rx_buffers) + if (!tm->threads[thread_index].rx_buffers) vec_alloc (tm->threads[thread_index].rx_buffers, VLIB_FRAME_SIZE); - n_alloc = vlib_buffer_alloc (vm, tm->threads[thread_index].rx_buffers + n_left, VLIB_FRAME_SIZE - n_left); + n_alloc = + vlib_buffer_alloc (vm, + tm->threads[thread_index].rx_buffers + n_left, + VLIB_FRAME_SIZE - n_left); _vec_len (tm->threads[thread_index].rx_buffers) = n_left + n_alloc; } } @@ -262,27 +267,31 @@ tuntap_rx (vlib_main_t * vm, Turn them into iovecs to pass to readv. */ { uword i_rx = vec_len (tm->threads[thread_index].rx_buffers) - 1; - vlib_buffer_t * b; + vlib_buffer_t *b; word i, n_bytes_left, n_bytes_in_packet; /** We should have enough buffers left for an MTU sized packet. */ - ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >= tm->mtu_buffers); + ASSERT (vec_len (tm->threads[thread_index].rx_buffers) >= + tm->mtu_buffers); vec_validate (tm->threads[thread_index].iovecs, tm->mtu_buffers - 1); for (i = 0; i < tm->mtu_buffers; i++) { - b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx - i]); + b = + vlib_get_buffer (vm, + tm->threads[thread_index].rx_buffers[i_rx - i]); tm->threads[thread_index].iovecs[i].iov_base = b->data; tm->threads[thread_index].iovecs[i].iov_len = buffer_size; } - n_bytes_left = readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, - tm->mtu_buffers); + n_bytes_left = + readv (tm->dev_net_tun_fd, tm->threads[thread_index].iovecs, + tm->mtu_buffers); n_bytes_in_packet = n_bytes_left; if (n_bytes_left <= 0) { - if (errno != EAGAIN) - clib_unix_warning ("readv %d", n_bytes_left); + if (errno != EAGAIN) + clib_unix_warning ("readv %d", n_bytes_left); return 0; } @@ -293,14 +302,15 @@ tuntap_rx (vlib_main_t * vm, b = vlib_get_buffer (vm, tm->threads[thread_index].rx_buffers[i_rx]); b->flags = 0; b->current_data = 0; - b->current_length = n_bytes_left < buffer_size ? n_bytes_left : buffer_size; + b->current_length = + n_bytes_left < buffer_size ? n_bytes_left : buffer_size; n_bytes_left -= buffer_size; if (n_bytes_left <= 0) - { - break; - } + { + break; + } i_rx--; b->flags |= VLIB_BUFFER_NEXT_PRESENT; @@ -309,11 +319,9 @@ tuntap_rx (vlib_main_t * vm, /** Interface counters for tuntap interface. */ vlib_increment_combined_counter - (vnet_main.interface_main.combined_sw_if_counters - + VNET_INTERFACE_COUNTER_RX, - thread_index, - tm->sw_if_index, - 1, n_bytes_in_packet); + (vnet_main.interface_main.combined_sw_if_counters + + VNET_INTERFACE_COUNTER_RX, + thread_index, tm->sw_if_index, 1, n_bytes_in_packet); _vec_len (tm->threads[thread_index].rx_buffers) = i_rx; } @@ -325,7 +333,7 @@ tuntap_rx (vlib_main_t * vm, uword n_trace = vlib_get_trace_count (vm, node); vnet_buffer (b)->sw_if_index[VLIB_RX] = tm->sw_if_index; - vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32)~0; + vnet_buffer (b)->sw_if_index[VLIB_TX] = (u32) ~ 0; /* * Turn this on if you run into @@ -333,7 +341,7 @@ tuntap_rx (vlib_main_t * vm, * which nodes they've visited... */ if (VLIB_BUFFER_TRACE_TRAJECTORY) - b->pre_data[0] = 0; + b->pre_data[0] = 0; b->error = node->errors[0]; @@ -343,26 +351,26 @@ tuntap_rx (vlib_main_t * vm, } else switch (b->data[0] & 0xf0) - { - case 0x40: - next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; - break; - case 0x60: - next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; - break; - default: - next_index = VNET_DEVICE_INPUT_NEXT_DROP; - break; - } + { + case 0x40: + next_index = VNET_DEVICE_INPUT_NEXT_IP4_INPUT; + break; + case 0x60: + next_index = VNET_DEVICE_INPUT_NEXT_IP6_INPUT; + break; + default: + next_index = VNET_DEVICE_INPUT_NEXT_DROP; + break; + } /* The linux kernel couldn't care less if our interface is up */ if (tm->have_normal_interface) { - vnet_main_t *vnm = vnet_get_main(); - vnet_sw_interface_t * si; - si = vnet_get_sw_interface (vnm, tm->sw_if_index); - if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) - next_index = VNET_DEVICE_INPUT_NEXT_DROP; + vnet_main_t *vnm = vnet_get_main (); + vnet_sw_interface_t *si; + si = vnet_get_sw_interface (vnm, tm->sw_if_index); + if (!(si->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) + next_index = VNET_DEVICE_INPUT_NEXT_DROP; } vnet_feature_start_device_input_x1 (tm->sw_if_index, &next_index, b); @@ -371,9 +379,8 @@ tuntap_rx (vlib_main_t * vm, if (n_trace > 0) { - vlib_trace_buffer (vm, node, next_index, - b, /* follow_chain */ 1); - vlib_set_trace_count (vm, node, n_trace - 1); + vlib_trace_buffer (vm, node, next_index, b, /* follow_chain */ 1); + vlib_set_trace_count (vm, node, n_trace - 1); } } @@ -383,10 +390,11 @@ tuntap_rx (vlib_main_t * vm, /** * @brief TUNTAP_RX error strings */ -static char * tuntap_rx_error_strings[] = { +static char *tuntap_rx_error_strings[] = { "unknown packet type", }; +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (tuntap_rx_node,static) = { .function = tuntap_rx, .name = "tuntap-rx", @@ -397,6 +405,7 @@ VLIB_REGISTER_NODE (tuntap_rx_node,static) = { .n_errors = 1, .error_strings = tuntap_rx_error_strings, }; +/* *INDENT-ON* */ /** * @brief Gets called when file descriptor is ready from epoll. @@ -405,9 +414,10 @@ VLIB_REGISTER_NODE (tuntap_rx_node,static) = { * * @return error - clib_error_t */ -static clib_error_t * tuntap_read_ready (clib_file_t * uf) +static clib_error_t * +tuntap_read_ready (clib_file_t * uf) { - vlib_main_t * vm = vlib_get_main(); + vlib_main_t *vm = vlib_get_main (); vlib_node_set_interrupt_pending (vm, tuntap_rx_node.index); return 0; } @@ -428,15 +438,15 @@ tuntap_exit (vlib_main_t * vm) int sfd; /* Not present. */ - if (! tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0) + if (!tm->dev_net_tun_fd || tm->dev_net_tun_fd < 0) return 0; sfd = socket (AF_INET, SOCK_STREAM, 0); if (sfd < 0) - clib_unix_warning("provisioning socket"); + clib_unix_warning ("provisioning socket"); - memset(&ifr, 0, sizeof (ifr)); - strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name)-1); + memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); /* get flags, modify to bring down interface... */ if (ioctl (sfd, SIOCGIFFLAGS, &ifr) < 0) @@ -450,9 +460,9 @@ tuntap_exit (vlib_main_t * vm) /* Turn off persistence */ if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 0) < 0) clib_unix_warning ("TUNSETPERSIST"); - close(tm->dev_tap_fd); + close (tm->dev_tap_fd); if (tm->dev_net_tun_fd >= 0) - close(tm->dev_net_tun_fd); + close (tm->dev_net_tun_fd); if (sfd >= 0) close (sfd); @@ -474,9 +484,9 @@ static clib_error_t * tuntap_config (vlib_main_t * vm, unformat_input_t * input) { tuntap_main_t *tm = &tuntap_main; - clib_error_t * error = 0; + clib_error_t *error = 0; struct ifreq ifr; - u8 * name; + u8 *name; int flags = IFF_TUN | IFF_NO_PI; int is_enabled = 0, is_ether = 0, have_normal_interface = 0; const uword buffer_size = VLIB_BUFFER_DATA_SIZE; @@ -486,15 +496,14 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) if (unformat (input, "mtu %d", &tm->mtu_bytes)) ; else if (unformat (input, "enable")) - is_enabled = 1; + is_enabled = 1; else if (unformat (input, "disable")) - is_enabled = 0; - else if (unformat (input, "ethernet") || - unformat (input, "ether")) - is_ether = 1; + is_enabled = 0; + else if (unformat (input, "ethernet") || unformat (input, "ether")) + is_ether = 1; else if (unformat (input, "have-normal-interface") || - unformat (input, "have-normal")) - have_normal_interface = 1; + unformat (input, "have-normal")) + have_normal_interface = 1; else if (unformat (input, "name %s", &name)) tm->tun_name = (char *) name; else @@ -508,11 +517,11 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) if (is_enabled == 0) return 0; - if (geteuid()) + if (geteuid ()) { clib_warning ("tuntap disabled: must be superuser"); return 0; - } + } tm->is_ether = is_ether; tm->have_normal_interface = have_normal_interface; @@ -527,14 +536,14 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) } memset (&ifr, 0, sizeof (ifr)); - strncpy(ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1); + strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); ifr.ifr_flags = flags; - if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *)&ifr) < 0) + if (ioctl (tm->dev_net_tun_fd, TUNSETIFF, (void *) &ifr) < 0) { error = clib_error_return_unix (0, "ioctl TUNSETIFF"); goto done; } - + /* Make it persistent, at least until we split. */ if (ioctl (tm->dev_net_tun_fd, TUNSETPERSIST, 1) < 0) { @@ -543,8 +552,7 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) } /* Open a provisioning socket */ - if ((tm->dev_tap_fd = socket(PF_PACKET, SOCK_RAW, - htons(ETH_P_ALL))) < 0 ) + if ((tm->dev_tap_fd = socket (PF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0) { error = clib_error_return_unix (0, "socket"); goto done; @@ -555,21 +563,21 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) struct ifreq ifr; struct sockaddr_ll sll; - memset (&ifr, 0, sizeof(ifr)); - strncpy (ifr.ifr_name, tm->tun_name, sizeof(ifr.ifr_name)-1); - if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0 ) + memset (&ifr, 0, sizeof (ifr)); + strncpy (ifr.ifr_name, tm->tun_name, sizeof (ifr.ifr_name) - 1); + if (ioctl (tm->dev_tap_fd, SIOCGIFINDEX, &ifr) < 0) { error = clib_error_return_unix (0, "ioctl SIOCGIFINDEX"); goto done; } /* Bind the provisioning socket to the interface. */ - memset(&sll, 0, sizeof(sll)); - sll.sll_family = AF_PACKET; - sll.sll_ifindex = ifr.ifr_ifindex; - sll.sll_protocol = htons(ETH_P_ALL); + memset (&sll, 0, sizeof (sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = ifr.ifr_ifindex; + sll.sll_protocol = htons (ETH_P_ALL); - if (bind(tm->dev_tap_fd, (struct sockaddr*) &sll, sizeof(sll)) < 0) + if (bind (tm->dev_tap_fd, (struct sockaddr *) &sll, sizeof (sll)) < 0) { error = clib_error_return_unix (0, "bind"); goto done; @@ -613,58 +621,54 @@ tuntap_config (vlib_main_t * vm, unformat_input_t * input) if (is_ether) { if (ioctl (tm->dev_tap_fd, SIOCGIFHWADDR, &ifr) < 0) - { - error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR"); - goto done; - } + { + error = clib_error_return_unix (0, "ioctl SIOCGIFHWADDR"); + goto done; + } else - clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6); + clib_memcpy (tm->ether_dst_mac, ifr.ifr_hwaddr.sa_data, 6); } if (have_normal_interface) { - vnet_main_t *vnm = vnet_get_main(); + vnet_main_t *vnm = vnet_get_main (); error = ethernet_register_interface - (vnm, - tuntap_dev_class.index, - 0 /* device instance */, - tm->ether_dst_mac /* ethernet address */, - &tm->hw_if_index, - 0 /* flag change */); + (vnm, tuntap_dev_class.index, 0 /* device instance */ , + tm->ether_dst_mac /* ethernet address */ , + &tm->hw_if_index, 0 /* flag change */ ); if (error) - clib_error_report (error); + clib_error_report (error); tm->sw_if_index = tm->hw_if_index; vm->os_punt_frame = tuntap_nopunt_frame; } else { - vnet_main_t *vnm = vnet_get_main(); - vnet_hw_interface_t * hi; - + vnet_main_t *vnm = vnet_get_main (); + vnet_hw_interface_t *hi; + vm->os_punt_frame = tuntap_punt_frame; - + tm->hw_if_index = vnet_register_interface - (vnm, - tuntap_dev_class.index, 0 /* device instance */, - tuntap_interface_class.index, 0); + (vnm, tuntap_dev_class.index, 0 /* device instance */ , + tuntap_interface_class.index, 0); hi = vnet_get_hw_interface (vnm, tm->hw_if_index); tm->sw_if_index = hi->sw_if_index; - + /* Interface is always up. */ - vnet_hw_interface_set_flags (vnm, tm->hw_if_index, - VNET_HW_INTERFACE_FLAG_LINK_UP); - vnet_sw_interface_set_flags (vnm, tm->sw_if_index, - VNET_SW_INTERFACE_FLAG_ADMIN_UP); + vnet_hw_interface_set_flags (vnm, tm->hw_if_index, + VNET_HW_INTERFACE_FLAG_LINK_UP); + vnet_sw_interface_set_flags (vnm, tm->sw_if_index, + VNET_SW_INTERFACE_FLAG_ADMIN_UP); } { - clib_file_t template = {0}; + clib_file_t template = { 0 }; template.read_function = tuntap_read_ready; template.file_descriptor = tm->dev_net_tun_fd; tm->clib_file_index = clib_file_add (&file_main, &template); } - done: +done: if (error) { if (tm->dev_net_tun_fd >= 0) @@ -694,27 +698,25 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, u32 sw_if_index, ip4_address_t * address, u32 address_length, - u32 if_address_index, - u32 is_delete) + u32 if_address_index, u32 is_delete) { - tuntap_main_t * tm = &tuntap_main; + tuntap_main_t *tm = &tuntap_main; struct ifreq ifr; - subif_address_t subif_addr, * ap; - uword * p; + subif_address_t subif_addr, *ap; + uword *p; /** Tuntap disabled, or using a "normal" interface. */ - if (tm->have_normal_interface || tm->dev_tap_fd < 0) + if (tm->have_normal_interface || tm->dev_tap_fd < 0) return; /* if the address is being applied to an interface that is not in * the same table/VRF as this tap, then ignore it. * If we don't do this overlapping address spaces in the diferent tables * breaks the linux host's routing tables */ - if (fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP4, - sw_if_index) != - fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP4, - tm->sw_if_index)) - return; + if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, + sw_if_index) != + fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP4, tm->sw_if_index)) + return; /** See if we already know about this subif */ memset (&subif_addr, 0, sizeof (subif_addr)); @@ -734,8 +736,8 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, /* Use subif pool index to select alias device. */ memset (&ifr, 0, sizeof (ifr)); - snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), - "%s:%d", tm->tun_name, (int)(ap - tm->subifs)); + snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), + "%s:%d", tm->tun_name, (int) (ap - tm->subifs)); /* the tuntap punt/inject is enabled for IPv4 RX so long as * any vpp interface has an IPv4 address. @@ -743,25 +745,25 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, */ ip4_sw_interface_enable_disable (tm->sw_if_index, !is_delete); - if (! is_delete) + if (!is_delete) { - struct sockaddr_in * sin; + struct sockaddr_in *sin; - sin = (struct sockaddr_in *)&ifr.ifr_addr; + sin = (struct sockaddr_in *) &ifr.ifr_addr; /* Set ipv4 address, netmask. */ sin->sin_family = AF_INET; clib_memcpy (&sin->sin_addr.s_addr, address, 4); if (ioctl (tm->dev_tap_fd, SIOCSIFADDR, &ifr) < 0) clib_unix_warning ("ioctl SIOCSIFADDR"); - + sin->sin_addr.s_addr = im->fib_masks[address_length]; if (ioctl (tm->dev_tap_fd, SIOCSIFNETMASK, &ifr) < 0) clib_unix_warning ("ioctl SIOCSIFNETMASK"); } else { - mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */); + mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ ); pool_put (tm->subifs, ap); } @@ -783,10 +785,11 @@ tuntap_ip4_add_del_interface_address (ip4_main_t * im, * including @c causes multiple definitions if * @c have_normal_interface || tm->dev_tap_fd < 0) + if (tm->have_normal_interface || tm->dev_tap_fd < 0) return; /* if the address is being applied to an interface that is not in * the same table/VRF as this tap, then ignore it. * If we don't do this overlapping address spaces in the diferent tables * breaks the linux host's routing tables */ - if (fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP6, - sw_if_index) != - fib_table_get_index_for_sw_if_index(FIB_PROTOCOL_IP6, - tm->sw_if_index)) - return; + if (fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, + sw_if_index) != + fib_table_get_index_for_sw_if_index (FIB_PROTOCOL_IP6, tm->sw_if_index)) + return; /* See if we already know about this subif */ memset (&subif_addr, 0, sizeof (subif_addr)); @@ -853,8 +854,8 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im, /* Use subif pool index to select alias device. */ memset (&ifr, 0, sizeof (ifr)); memset (&ifr6, 0, sizeof (ifr6)); - snprintf (ifr.ifr_name, sizeof(ifr.ifr_name), - "%s:%d", tm->tun_name, (int)(ap - tm->subifs)); + snprintf (ifr.ifr_name, sizeof (ifr.ifr_name), + "%s:%d", tm->tun_name, (int) (ap - tm->subifs)); /* the tuntap punt/inject is enabled for IPv6 RX so long as * any vpp interface has an IPv6 address. @@ -862,45 +863,45 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im, */ ip6_sw_interface_enable_disable (tm->sw_if_index, !is_delete); - if (! is_delete) + if (!is_delete) { int sockfd = socket (AF_INET6, SOCK_STREAM, 0); if (sockfd < 0) - clib_unix_warning ("get ifindex socket"); + clib_unix_warning ("get ifindex socket"); if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0) - clib_unix_warning ("get ifindex"); + clib_unix_warning ("get ifindex"); ifr6.ifr6_ifindex = ifr.ifr_ifindex; ifr6.ifr6_prefixlen = address_length; clib_memcpy (&ifr6.ifr6_addr, address, 16); if (ioctl (sockfd, SIOCSIFADDR, &ifr6) < 0) - clib_unix_warning ("set address"); + clib_unix_warning ("set address"); if (sockfd >= 0) - close (sockfd); + close (sockfd); } else { int sockfd = socket (AF_INET6, SOCK_STREAM, 0); if (sockfd < 0) - clib_unix_warning ("get ifindex socket"); + clib_unix_warning ("get ifindex socket"); if (ioctl (sockfd, SIOGIFINDEX, &ifr) < 0) - clib_unix_warning ("get ifindex"); + clib_unix_warning ("get ifindex"); ifr6.ifr6_ifindex = ifr.ifr_ifindex; ifr6.ifr6_prefixlen = address_length; clib_memcpy (&ifr6.ifr6_addr, address, 16); if (ioctl (sockfd, SIOCDIFADDR, &ifr6) < 0) - clib_unix_warning ("del address"); + clib_unix_warning ("del address"); if (sockfd >= 0) - close (sockfd); + close (sockfd); - mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */); + mhash_unset (&tm->subif_mhash, &subif_addr, 0 /* old value ptr */ ); pool_put (tm->subifs, ap); } } @@ -915,8 +916,7 @@ tuntap_ip6_add_del_interface_address (ip6_main_t * im, */ static void tuntap_punt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { tuntap_tx (vm, node, frame); vlib_frame_free (vm, node, frame); @@ -932,19 +932,20 @@ tuntap_punt_frame (vlib_main_t * vm, */ static void tuntap_nopunt_frame (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { - u32 * buffers = vlib_frame_args (frame); + u32 *buffers = vlib_frame_args (frame); uword n_packets = frame->n_vectors; vlib_buffer_free (vm, buffers, n_packets); vlib_frame_free (vm, node, frame); } +/* *INDENT-OFF* */ VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = { .name = "tuntap", .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, }; +/* *INDENT-ON* */ /** * @brief Format tun/tap interface name @@ -955,7 +956,8 @@ VNET_HW_INTERFACE_CLASS (tuntap_interface_class,static) = { * @return *s - u8 - formatted string * */ -static u8 * format_tuntap_interface_name (u8 * s, va_list * args) +static u8 * +format_tuntap_interface_name (u8 * s, va_list * args) { u32 i = va_arg (*args, u32); @@ -975,11 +977,10 @@ static u8 * format_tuntap_interface_name (u8 * s, va_list * args) */ static uword tuntap_intfc_tx (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { - tuntap_main_t * tm = &tuntap_main; - u32 * buffers = vlib_frame_args (frame); + tuntap_main_t *tm = &tuntap_main; + u32 *buffers = vlib_frame_args (frame); uword n_buffers = frame->n_vectors; /* Normal interface transmit happens only on the normal interface... */ @@ -990,11 +991,13 @@ tuntap_intfc_tx (vlib_main_t * vm, return n_buffers; } +/* *INDENT-OFF* */ VNET_DEVICE_CLASS (tuntap_dev_class,static) = { .name = "tuntap", .tx_function = tuntap_intfc_tx, .format_device_name = format_tuntap_interface_name, }; +/* *INDENT-ON* */ /** * @brief tun/tap node init @@ -1007,19 +1010,19 @@ VNET_DEVICE_CLASS (tuntap_dev_class,static) = { static clib_error_t * tuntap_init (vlib_main_t * vm) { - clib_error_t * error; - ip4_main_t * im4 = &ip4_main; - ip6_main_t * im6 = &ip6_main; + clib_error_t *error; + ip4_main_t *im4 = &ip4_main; + ip6_main_t *im6 = &ip6_main; ip4_add_del_interface_address_callback_t cb4; ip6_add_del_interface_address_callback_t cb6; - tuntap_main_t * tm = &tuntap_main; - vlib_thread_main_t * m = vlib_get_thread_main (); + tuntap_main_t *tm = &tuntap_main; + vlib_thread_main_t *m = vlib_get_thread_main (); error = vlib_call_init_function (vm, ip4_init); if (error) return error; - mhash_init (&tm->subif_mhash, sizeof (u32), sizeof(subif_address_t)); + mhash_init (&tm->subif_mhash, sizeof (u32), sizeof (subif_address_t)); cb4.function = tuntap_ip4_add_del_interface_address; cb4.function_opaque = 0; @@ -1035,3 +1038,11 @@ tuntap_init (vlib_main_t * vm) } VLIB_INIT_FUNCTION (tuntap_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/unix/tuntap.h b/src/vnet/unix/tuntap.h index 7c2d5510bcb..46e7a606a2b 100644 --- a/src/vnet/unix/tuntap.h +++ b/src/vnet/unix/tuntap.h @@ -54,16 +54,24 @@ typedef struct } vnet_tap_connect_args_t; /** Connect a tap interface */ -int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t *args); +int vnet_tap_connect (vlib_main_t * vm, vnet_tap_connect_args_t * args); /** Connect / renumber a tap interface */ int vnet_tap_connect_renumber (vlib_main_t * vm, - vnet_tap_connect_args_t *args); + vnet_tap_connect_args_t * args); /** Modify a tap interface */ -int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t *args); +int vnet_tap_modify (vlib_main_t * vm, vnet_tap_connect_args_t * args); /** delete a tap interface */ -int vnet_tap_delete(vlib_main_t *vm, u32 sw_if_index); +int vnet_tap_delete (vlib_main_t * vm, u32 sw_if_index); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ -- cgit 1.2.3-korg