diff options
author | 2016-07-08 08:13:45 -0400 | |
---|---|---|
committer | 2016-07-08 14:18:22 +0000 | |
commit | 9b8ffd99fb35d37250cfb7094a0a31515f1bb577 (patch) | |
tree | 604ebcf86671a86b9e3395dda8d9fda50db2fcbc /vlib/vlib/unix | |
parent | 0557a91ca727cee963a8179808d2d2108564ec56 (diff) |
fd-io-styleify pass
Change-Id: If2d57a213fc2fec996db26df332910c3d2105f97
Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'vlib/vlib/unix')
-rw-r--r-- | vlib/vlib/unix/cj.c | 146 | ||||
-rw-r--r-- | vlib/vlib/unix/cj.h | 33 | ||||
-rw-r--r-- | vlib/vlib/unix/cli.c | 2502 | ||||
-rw-r--r-- | vlib/vlib/unix/input.c | 97 | ||||
-rw-r--r-- | vlib/vlib/unix/main.c | 398 | ||||
-rw-r--r-- | vlib/vlib/unix/mc_socket.c | 619 | ||||
-rw-r--r-- | vlib/vlib/unix/mc_socket.h | 53 | ||||
-rw-r--r-- | vlib/vlib/unix/physmem.c | 330 | ||||
-rw-r--r-- | vlib/vlib/unix/physmem.h | 17 | ||||
-rw-r--r-- | vlib/vlib/unix/plugin.c | 177 | ||||
-rw-r--r-- | vlib/vlib/unix/plugin.h | 30 | ||||
-rw-r--r-- | vlib/vlib/unix/unix.h | 74 | ||||
-rw-r--r-- | vlib/vlib/unix/util.c | 61 |
13 files changed, 2404 insertions, 2133 deletions
diff --git a/vlib/vlib/unix/cj.c b/vlib/vlib/unix/cj.c index 782ddce3099..553b450ffaa 100644 --- a/vlib/vlib/unix/cj.c +++ b/vlib/vlib/unix/cj.c @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * cj.c * @@ -25,65 +25,68 @@ cj_main_t cj_main; void -cj_log (u32 type, void * data0, void * data1) +cj_log (u32 type, void *data0, void *data1) { u64 new_tail; - cj_main_t * cjm = &cj_main; - cj_record_t * r; + cj_main_t *cjm = &cj_main; + cj_record_t *r; if (cjm->enable == 0) return; new_tail = __sync_add_and_fetch (&cjm->tail, 1); - r = (cj_record_t *) &(cjm->records[new_tail & (cjm->num_records - 1)]); + r = (cj_record_t *) & (cjm->records[new_tail & (cjm->num_records - 1)]); r->time = vlib_time_now (cjm->vlib_main); - r->cpu = os_get_cpu_number(); + r->cpu = os_get_cpu_number (); r->type = type; - r->data[0] = pointer_to_uword(data0); - r->data[1] = pointer_to_uword(data1); + r->data[0] = pointer_to_uword (data0); + r->data[1] = pointer_to_uword (data1); } -void cj_stop(void) +void +cj_stop (void) { - cj_main_t * cjm = &cj_main; + cj_main_t *cjm = &cj_main; cjm->enable = 0; } -clib_error_t * cj_init (vlib_main_t * vm) +clib_error_t * +cj_init (vlib_main_t * vm) { - cj_main_t * cjm = &cj_main; + cj_main_t *cjm = &cj_main; cjm->vlib_main = vm; return 0; } + VLIB_INIT_FUNCTION (cj_init); static clib_error_t * cj_config (vlib_main_t * vm, unformat_input_t * input) { - cj_main_t * cjm = &cj_main; + cj_main_t *cjm = &cj_main; int matched = 0; int enable = 0; - while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT) + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "records %d", &cjm->num_records)) - matched = 1; + matched = 1; else if (unformat (input, "on")) - enable = 1; + enable = 1; else - return clib_error_return (0, "cj_config: unknown input '%U'", - format_unformat_error, input); + return clib_error_return (0, "cj_config: unknown input '%U'", + format_unformat_error, input); } if (matched == 0) return 0; cjm->num_records = max_pow2 (cjm->num_records); - vec_validate (cjm->records, cjm->num_records-1); + vec_validate (cjm->records, cjm->num_records - 1); memset (cjm->records, 0xff, cjm->num_records * sizeof (cj_record_t)); cjm->tail = ~0; cjm->enable = enable; @@ -93,112 +96,119 @@ cj_config (vlib_main_t * vm, unformat_input_t * input) VLIB_CONFIG_FUNCTION (cj_config, "cj"); -void cj_enable_disable (int is_enable) +void +cj_enable_disable (int is_enable) { - cj_main_t * cjm = &cj_main; - + cj_main_t *cjm = &cj_main; + if (cjm->num_records) cjm->enable = is_enable; else vlib_cli_output (cjm->vlib_main, "CJ not configured..."); } -static inline void cj_dump_one_record (cj_record_t * r) +static inline void +cj_dump_one_record (cj_record_t * r) { fprintf (stderr, "[%d]: %10.6f T%02d %llx %llx\n", - r->cpu, r->time, r->type, (long long unsigned int) r->data[0], - (long long unsigned int) r->data[1]); + r->cpu, r->time, r->type, (long long unsigned int) r->data[0], + (long long unsigned int) r->data[1]); } -static void cj_dump_internal (u8 filter0_enable, u64 filter0, - u8 filter1_enable, u64 filter1) +static void +cj_dump_internal (u8 filter0_enable, u64 filter0, + u8 filter1_enable, u64 filter1) { - cj_main_t * cjm = &cj_main; - cj_record_t * r; + cj_main_t *cjm = &cj_main; + cj_record_t *r; u32 i, index; - + if (cjm->num_records == 0) { fprintf (stderr, "CJ not configured...\n"); return; } - if (cjm->tail == (u64)~0) + if (cjm->tail == (u64) ~ 0) { fprintf (stderr, "No data collected...\n"); return; } /* Has the trace wrapped? */ - index = (cjm->tail+1) & (cjm->num_records - 1); + index = (cjm->tail + 1) & (cjm->num_records - 1); r = &(cjm->records[index]); - if (r->cpu != (u32)~0) + if (r->cpu != (u32) ~ 0) { - /* Yes, dump from tail + 1 to the end */ + /* Yes, dump from tail + 1 to the end */ for (i = index; i < cjm->num_records; i++) - { - if (filter0_enable && (r->data[0] != filter0)) - goto skip; - if (filter1_enable && (r->data[1] != filter1)) - goto skip; - cj_dump_one_record (r); - skip: - r++; - } + { + if (filter0_enable && (r->data[0] != filter0)) + goto skip; + if (filter1_enable && (r->data[1] != filter1)) + goto skip; + cj_dump_one_record (r); + skip: + r++; + } } /* dump from the beginning through the final tail */ r = cjm->records; for (i = 0; i <= cjm->tail; i++) { if (filter0_enable && (r->data[0] != filter0)) - goto skip2; + goto skip2; if (filter1_enable && (r->data[1] != filter1)) - goto skip2; + goto skip2; cj_dump_one_record (r); skip2: r++; } } -void cj_dump (void) +void +cj_dump (void) { cj_dump_internal (0, 0, 0, 0); } -void cj_dump_filter_data0 (u64 filter0) +void +cj_dump_filter_data0 (u64 filter0) { - cj_dump_internal (1/* enable f0 */, filter0, 0, 0); + cj_dump_internal (1 /* enable f0 */ , filter0, 0, 0); } -void cj_dump_filter_data1 (u64 filter1) +void +cj_dump_filter_data1 (u64 filter1) { - cj_dump_internal (0, 0, 1 /* enable f1 */, filter1); + cj_dump_internal (0, 0, 1 /* enable f1 */ , filter1); } -void cj_dump_filter_data12 (u64 filter0, u64 filter1) +void +cj_dump_filter_data12 (u64 filter0, u64 filter1) { cj_dump_internal (1, filter0, 1, filter1); } static clib_error_t * cj_command_fn (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { int is_enable = -1; int is_dump = -1; - while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { - if (unformat (input, "enable") || unformat (input, "on")) - is_enable = 1; - else if (unformat (input, "disable") || unformat (input, "off")) - is_enable = 0; - else if (unformat (input, "dump")) - is_dump = 1; - else - return clib_error_return (0, "unknown input `%U'", - format_unformat_error, input); + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "enable") || unformat (input, "on")) + is_enable = 1; + else if (unformat (input, "disable") || unformat (input, "off")) + is_enable = 0; + else if (unformat (input, "dump")) + is_dump = 1; + else + return clib_error_return (0, "unknown input `%U'", + format_unformat_error, input); } if (is_enable >= 0) @@ -210,9 +220,19 @@ cj_command_fn (vlib_main_t * vm, return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cj_command,static) = { .path = "cj", .short_help = "cj", .function = cj_command_fn, }; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/cj.h b/vlib/vlib/unix/cj.h index 3c37f2bf22f..67626afee2b 100644 --- a/vlib/vlib/unix/cj.h +++ b/vlib/vlib/unix/cj.h @@ -1,4 +1,4 @@ -/* +/* *------------------------------------------------------------------ * cj.h * @@ -20,25 +20,27 @@ #ifndef __included_cj_h__ #define __included_cj_h__ -typedef struct { +typedef struct +{ f64 time; u32 cpu; u32 type; u64 data[2]; } cj_record_t; -typedef struct { +typedef struct +{ volatile u64 tail; - cj_record_t * records; + cj_record_t *records; u32 num_records; volatile u32 enable; - - vlib_main_t * vlib_main; + + vlib_main_t *vlib_main; } cj_main_t; -void cj_log (u32 type, void * data0, void * data1); +void cj_log (u32 type, void *data0, void *data1); -/* +/* * Supply in application main, so we can log from any library... * Declare a weak reference in the library, off you go. */ @@ -60,9 +62,18 @@ cj_global_log (unsigned type, void * data0, void * data1) \ } #define CJ_GLOBAL_LOG_PROTOTYPE -void cj_global_log (unsigned type, void * data0, void * data1) \ - __attribute__ ((weak)); \ +void +cj_global_log (unsigned type, void *data0, void *data1) +__attribute__ ((weak)); -void cj_stop(void); +void cj_stop (void); #endif /* __included_cj_h__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/cli.c b/vlib/vlib/unix/cli.c index 5136da516fc..58aa6dfc6bf 100644 --- a/vlib/vlib/unix/cli.c +++ b/vlib/vlib/unix/cli.c @@ -92,32 +92,36 @@ /** A CLI banner line. */ -typedef struct { - u8 * line; /**< The line to print. */ - u32 length; /**< The length of the line without terminating NUL. */ +typedef struct +{ + u8 *line; /**< The line to print. */ + u32 length; /**< The length of the line without terminating NUL. */ } unix_cli_banner_t; #define _(a) { .line = (u8 *)(a), .length = sizeof(a) - 1 } /** Plain welcome banner. */ static unix_cli_banner_t unix_cli_banner[] = { -_(" _______ _ _ _____ ___ \n"), -_(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"), -_(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"), -_(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"), -_("\n") + _(" _______ _ _ _____ ___ \n"), + _(" __/ __/ _ \\ (_)__ | | / / _ \\/ _ \\\n"), + _(" _/ _// // / / / _ \\ | |/ / ___/ ___/\n"), + _(" /_/ /____(_)_/\\___/ |___/_/ /_/ \n"), + _("\n") }; + /** ANSI color welcome banner. */ static unix_cli_banner_t unix_cli_banner_color[] = { -_(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"), -_(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"), -_(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"), -_(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"), -_("\n") + _(ANSI_BRED " _______ _ " ANSI_RESET " _ _____ ___ \n"), + _(ANSI_BRED " __/ __/ _ \\ (_)__ " ANSI_RESET " | | / / _ \\/ _ \\\n"), + _(ANSI_BRED " _/ _// // / / / _ \\" ANSI_RESET " | |/ / ___/ ___/\n"), + _(ANSI_BRED " /_/ /____(_)_/\\___/" ANSI_RESET " |___/_/ /_/ \n"), + _("\n") }; + #undef _ /** Pager line index */ -typedef struct { +typedef struct +{ /** Index into pager_vector */ u32 line; @@ -130,20 +134,21 @@ typedef struct { /** Unix CLI session. */ -typedef struct { +typedef struct +{ /** The file index held by unix.c */ u32 unix_file_index; /** Vector of output pending write to file descriptor. */ - u8 * output_vector; + u8 *output_vector; /** Vector of input saved by Unix input node to be processed by CLI process. */ - u8 * input_vector; + u8 *input_vector; u8 has_history; - u8 ** command_history; - u8 * current_command; + u8 **command_history; + u8 *current_command; i32 excursion; /** Maximum number of history entries this session will store. */ @@ -152,7 +157,7 @@ typedef struct { /** Current command line counter */ u32 command_number; - u8 * search_key; + u8 *search_key; int search_mode; /** Position of the insert cursor on the current input line */ @@ -174,10 +179,10 @@ typedef struct { u8 no_pager; /** Pager buffer */ - u8 ** pager_vector; + u8 **pager_vector; /** Index of line fragments in the pager buffer */ - unix_cli_pager_index_t * pager_index; + unix_cli_pager_index_t *pager_index; /** Line number of top of page */ u32 pager_start; @@ -196,9 +201,9 @@ typedef struct { * @param f The CLI session whose pager needs to be reset. */ always_inline void -unix_cli_pager_reset (unix_cli_file_t *f) +unix_cli_pager_reset (unix_cli_file_t * f) { - u8 ** p; + u8 **p; f->pager_start = 0; @@ -206,9 +211,9 @@ unix_cli_pager_reset (unix_cli_file_t *f) f->pager_index = 0; vec_foreach (p, f->pager_vector) - { - vec_free (*p); - } + { + vec_free (*p); + } vec_free (f->pager_vector); f->pager_vector = 0; } @@ -225,14 +230,15 @@ unix_cli_file_free (unix_cli_file_t * f) } /** CLI actions */ -typedef enum { +typedef enum +{ UNIX_CLI_PARSE_ACTION_NOACTION = 0, /**< No action */ - UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */ - UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */ - UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */ + UNIX_CLI_PARSE_ACTION_CRLF, /**< Carriage return, newline or enter */ + UNIX_CLI_PARSE_ACTION_TAB, /**< Tab key */ + UNIX_CLI_PARSE_ACTION_ERASE, /**< Erase cursor left */ UNIX_CLI_PARSE_ACTION_ERASERIGHT, /**< Erase cursor right */ - UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */ - UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */ + UNIX_CLI_PARSE_ACTION_UP, /**< Up arrow */ + UNIX_CLI_PARSE_ACTION_DOWN, /**< Down arrow */ UNIX_CLI_PARSE_ACTION_LEFT, UNIX_CLI_PARSE_ACTION_RIGHT, UNIX_CLI_PARSE_ACTION_HOME, @@ -267,9 +273,10 @@ typedef enum { * @note This won't work as a hash since we need to be able to do * partial matches on the string. */ -typedef struct { - u8 *input; /**< Input string to match. */ - u32 len; /**< Length of input without final NUL. */ +typedef struct +{ + u8 *input; /**< Input string to match. */ + u32 len; /**< Length of input without final NUL. */ unix_cli_parse_action_t action; /**< Action to take when matched. */ } unix_cli_parse_actions_t; @@ -290,61 +297,61 @@ typedef struct { * @showinitializer */ static unix_cli_parse_actions_t unix_cli_parse_strings[] = { - /* Line handling */ - _( "\r\n", UNIX_CLI_PARSE_ACTION_CRLF ), /* Must be before '\r' */ - _( "\n", UNIX_CLI_PARSE_ACTION_CRLF ), - _( "\r\0", UNIX_CLI_PARSE_ACTION_CRLF ), /* Telnet does this */ - _( "\r", UNIX_CLI_PARSE_ACTION_CRLF ), - - /* Unix shell control codes */ - _( CTL('B'), UNIX_CLI_PARSE_ACTION_LEFT ), - _( CTL('F'), UNIX_CLI_PARSE_ACTION_RIGHT ), - _( CTL('P'), UNIX_CLI_PARSE_ACTION_UP ), - _( CTL('N'), UNIX_CLI_PARSE_ACTION_DOWN ), - _( CTL('A'), UNIX_CLI_PARSE_ACTION_HOME ), - _( CTL('E'), UNIX_CLI_PARSE_ACTION_END ), - _( CTL('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT ), - _( CTL('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT ), - _( CTL('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT ), - _( CTL('Y'), UNIX_CLI_PARSE_ACTION_YANK ), - _( CTL('L'), UNIX_CLI_PARSE_ACTION_CLEAR ), - _( ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT ), /* Alt-B */ - _( ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT ), /* Alt-F */ - _( "\b", UNIX_CLI_PARSE_ACTION_ERASE ), /* ^H */ - _( "\x7f", UNIX_CLI_PARSE_ACTION_ERASE ), /* Backspace */ - _( "\t", UNIX_CLI_PARSE_ACTION_TAB ), /* ^I */ - - /* VT100 Normal mode - Broadest support */ - _( CSI "A", UNIX_CLI_PARSE_ACTION_UP ), - _( CSI "B", UNIX_CLI_PARSE_ACTION_DOWN ), - _( CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT ), - _( CSI "D", UNIX_CLI_PARSE_ACTION_LEFT ), - _( CSI "H", UNIX_CLI_PARSE_ACTION_HOME ), - _( CSI "F", UNIX_CLI_PARSE_ACTION_END ), - _( CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT ), /* Delete */ - _( CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT ), /* C-Left */ - _( CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT ),/* C-Right */ + /* Line handling */ + _("\r\n", UNIX_CLI_PARSE_ACTION_CRLF), /* Must be before '\r' */ + _("\n", UNIX_CLI_PARSE_ACTION_CRLF), + _("\r\0", UNIX_CLI_PARSE_ACTION_CRLF), /* Telnet does this */ + _("\r", UNIX_CLI_PARSE_ACTION_CRLF), + + /* Unix shell control codes */ + _(CTL ('B'), UNIX_CLI_PARSE_ACTION_LEFT), + _(CTL ('F'), UNIX_CLI_PARSE_ACTION_RIGHT), + _(CTL ('P'), UNIX_CLI_PARSE_ACTION_UP), + _(CTL ('N'), UNIX_CLI_PARSE_ACTION_DOWN), + _(CTL ('A'), UNIX_CLI_PARSE_ACTION_HOME), + _(CTL ('E'), UNIX_CLI_PARSE_ACTION_END), + _(CTL ('D'), UNIX_CLI_PARSE_ACTION_ERASERIGHT), + _(CTL ('U'), UNIX_CLI_PARSE_ACTION_ERASELINELEFT), + _(CTL ('K'), UNIX_CLI_PARSE_ACTION_ERASELINERIGHT), + _(CTL ('Y'), UNIX_CLI_PARSE_ACTION_YANK), + _(CTL ('L'), UNIX_CLI_PARSE_ACTION_CLEAR), + _(ESC "b", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* Alt-B */ + _(ESC "f", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* Alt-F */ + _("\b", UNIX_CLI_PARSE_ACTION_ERASE), /* ^H */ + _("\x7f", UNIX_CLI_PARSE_ACTION_ERASE), /* Backspace */ + _("\t", UNIX_CLI_PARSE_ACTION_TAB), /* ^I */ + + /* VT100 Normal mode - Broadest support */ + _(CSI "A", UNIX_CLI_PARSE_ACTION_UP), + _(CSI "B", UNIX_CLI_PARSE_ACTION_DOWN), + _(CSI "C", UNIX_CLI_PARSE_ACTION_RIGHT), + _(CSI "D", UNIX_CLI_PARSE_ACTION_LEFT), + _(CSI "H", UNIX_CLI_PARSE_ACTION_HOME), + _(CSI "F", UNIX_CLI_PARSE_ACTION_END), + _(CSI "3~", UNIX_CLI_PARSE_ACTION_ERASERIGHT), /* Delete */ + _(CSI "1;5D", UNIX_CLI_PARSE_ACTION_WORDLEFT), /* C-Left */ + _(CSI "1;5C", UNIX_CLI_PARSE_ACTION_WORDRIGHT), /* C-Right */ /* VT100 Application mode - Some Gnome Terminal functions use these */ - _( ESC "OA", UNIX_CLI_PARSE_ACTION_UP ), - _( ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN ), - _( ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT ), - _( ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT ), - _( ESC "OH", UNIX_CLI_PARSE_ACTION_HOME ), - _( ESC "OF", UNIX_CLI_PARSE_ACTION_END ), - - /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */ - _( CSI "1~", UNIX_CLI_PARSE_ACTION_HOME ), - _( CSI "4~", UNIX_CLI_PARSE_ACTION_END ), - - /* Emacs-ish history search */ - _( CTL('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH ), - _( CTL('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH ), - - /* Other protocol things */ - _( "\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC ), /* IAC */ - _( "\0", UNIX_CLI_PARSE_ACTION_NOACTION ), /* NUL */ - _( NULL, UNIX_CLI_PARSE_ACTION_NOMATCH ) + _(ESC "OA", UNIX_CLI_PARSE_ACTION_UP), + _(ESC "OB", UNIX_CLI_PARSE_ACTION_DOWN), + _(ESC "OC", UNIX_CLI_PARSE_ACTION_RIGHT), + _(ESC "OD", UNIX_CLI_PARSE_ACTION_LEFT), + _(ESC "OH", UNIX_CLI_PARSE_ACTION_HOME), + _(ESC "OF", UNIX_CLI_PARSE_ACTION_END), + + /* ANSI X3.41-1974 - sent by Microsoft Telnet and PuTTY */ + _(CSI "1~", UNIX_CLI_PARSE_ACTION_HOME), + _(CSI "4~", UNIX_CLI_PARSE_ACTION_END), + + /* Emacs-ish history search */ + _(CTL ('S'), UNIX_CLI_PARSE_ACTION_FWDSEARCH), + _(CTL ('R'), UNIX_CLI_PARSE_ACTION_REVSEARCH), + + /* Other protocol things */ + _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */ + _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */ + _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH) }; /** @@ -352,60 +359,63 @@ static unix_cli_parse_actions_t unix_cli_parse_strings[] = { * @showinitializer */ static unix_cli_parse_actions_t unix_cli_parse_pager[] = { - /* Line handling */ - _( "\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), /* Must be before '\r' */ - _( "\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), - _( "\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), /* Telnet does this */ - _( "\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF ), - - /* Pager commands */ - _( " ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT ), - _( "q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT ), - _( CTL('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW ), - _( CTL('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW ), - _( "/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH ), - - /* VT100 */ - _( CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP ), - _( CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN ), - _( CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP ), - _( CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ), - - /* VT100 Application mode */ - _( ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP ), - _( ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN ), - _( ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP ), - _( ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ), - - /* ANSI X3.41-1974 */ - _( CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP ), - _( CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM ), - _( CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP ), - _( CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN ), - - /* Other protocol things */ - _( "\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC ), /* IAC */ - _( "\0", UNIX_CLI_PARSE_ACTION_NOACTION ), /* NUL */ - _( NULL, UNIX_CLI_PARSE_ACTION_NOMATCH ) + /* Line handling */ + _("\r\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Must be before '\r' */ + _("\n", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), + _("\r\0", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), /* Telnet does this */ + _("\r", UNIX_CLI_PARSE_ACTION_PAGER_CRLF), + + /* Pager commands */ + _(" ", UNIX_CLI_PARSE_ACTION_PAGER_NEXT), + _("q", UNIX_CLI_PARSE_ACTION_PAGER_QUIT), + _(CTL ('L'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW), + _(CTL ('R'), UNIX_CLI_PARSE_ACTION_PAGER_REDRAW), + _("/", UNIX_CLI_PARSE_ACTION_PAGER_SEARCH), + + /* VT100 */ + _(CSI "A", UNIX_CLI_PARSE_ACTION_PAGER_UP), + _(CSI "B", UNIX_CLI_PARSE_ACTION_PAGER_DN), + _(CSI "H", UNIX_CLI_PARSE_ACTION_PAGER_TOP), + _(CSI "F", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM), + + /* VT100 Application mode */ + _(ESC "OA", UNIX_CLI_PARSE_ACTION_PAGER_UP), + _(ESC "OB", UNIX_CLI_PARSE_ACTION_PAGER_DN), + _(ESC "OH", UNIX_CLI_PARSE_ACTION_PAGER_TOP), + _(ESC "OF", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM), + + /* ANSI X3.41-1974 */ + _(CSI "1~", UNIX_CLI_PARSE_ACTION_PAGER_TOP), + _(CSI "4~", UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM), + _(CSI "5~", UNIX_CLI_PARSE_ACTION_PAGER_PGUP), + _(CSI "6~", UNIX_CLI_PARSE_ACTION_PAGER_PGDN), + + /* Other protocol things */ + _("\xff", UNIX_CLI_PARSE_ACTION_TELNETIAC), /* IAC */ + _("\0", UNIX_CLI_PARSE_ACTION_NOACTION), /* NUL */ + _(NULL, UNIX_CLI_PARSE_ACTION_NOMATCH) }; + #undef _ /** CLI session events. */ -typedef enum { +typedef enum +{ UNIX_CLI_PROCESS_EVENT_READ_READY, /**< A file descriptor has data to be read. */ - UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */ + UNIX_CLI_PROCESS_EVENT_QUIT, /**< A CLI session wants to close. */ } unix_cli_process_event_type_t; /** CLI global state. */ -typedef struct { +typedef struct +{ /** Prompt string for CLI. */ - u8 * cli_prompt; + u8 *cli_prompt; /** Vec pool of CLI sessions. */ - unix_cli_file_t * cli_file_pool; + unix_cli_file_t *cli_file_pool; /** Vec pool of unused session indices. */ - u32 * unused_cli_process_node_indices; + u32 *unused_cli_process_node_indices; /** The session index of the stdin cli */ u32 stdin_cli_file_index; @@ -441,72 +451,69 @@ static unix_cli_main_t unix_cli_main; * match at all. */ static unix_cli_parse_action_t -unix_cli_match_action(unix_cli_parse_actions_t *a, - u8 *input, u32 ilen, i32 *matched) +unix_cli_match_action (unix_cli_parse_actions_t * a, + u8 * input, u32 ilen, i32 * matched) { u8 partial = 0; while (a->input) { - if (ilen >= a->len) - { - /* see if the start of the input buffer exactly matches the current - * action string. */ - if (memcmp(input, a->input, a->len) == 0) - { - *matched = a->len; - return a->action; - } - } - else - { - /* if the first ilen characters match, flag this as a partial - - * meaning keep collecting bytes in case of a future match */ - if (memcmp(input, a->input, ilen) == 0) - partial = 1; - } - - /* check next action */ - a ++; + if (ilen >= a->len) + { + /* see if the start of the input buffer exactly matches the current + * action string. */ + if (memcmp (input, a->input, a->len) == 0) + { + *matched = a->len; + return a->action; + } + } + else + { + /* if the first ilen characters match, flag this as a partial - + * meaning keep collecting bytes in case of a future match */ + if (memcmp (input, a->input, ilen) == 0) + partial = 1; + } + + /* check next action */ + a++; } return partial ? - UNIX_CLI_PARSE_ACTION_PARTIALMATCH : - UNIX_CLI_PARSE_ACTION_NOMATCH; + UNIX_CLI_PARSE_ACTION_PARTIALMATCH : UNIX_CLI_PARSE_ACTION_NOMATCH; } static void unix_cli_add_pending_output (unix_file_t * uf, unix_cli_file_t * cf, - u8 * buffer, - uword buffer_bytes) + u8 * buffer, uword buffer_bytes) { - unix_main_t * um = &unix_main; + unix_main_t *um = &unix_main; vec_add (cf->output_vector, buffer, buffer_bytes); if (vec_len (cf->output_vector) > 0) { int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; - if (! skip_update) + if (!skip_update) um->file_update (uf, UNIX_FILE_UPDATE_MODIFY); } } static void unix_cli_del_pending_output (unix_file_t * uf, - unix_cli_file_t * cf, - uword n_bytes) + unix_cli_file_t * cf, uword n_bytes) { - unix_main_t * um = &unix_main; + unix_main_t *um = &unix_main; vec_delete (cf->output_vector, n_bytes, 0); if (vec_len (cf->output_vector) <= 0) { int skip_update = 0 == (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; - if (! skip_update) + if (!skip_update) um->file_update (uf, UNIX_FILE_UPDATE_MODIFY); } } @@ -525,15 +532,16 @@ unix_cli_del_pending_output (unix_file_t * uf, * @return The index of the first occurence of \c chr. If \c chr is not * found then \c len instead. */ -always_inline word unix_vlib_findchr(u8 chr, u8 *str, word len) +always_inline word +unix_vlib_findchr (u8 chr, u8 * str, word len) { - word i = 0; - for (i = 0; i < len; i++, str++) - { - if (*str == chr) - return i; - } - return len; + word i = 0; + for (i = 0; i < len; i++, str++) + { + if (*str == chr) + return i; + } + return len; } /** \brief Send a buffer to the CLI stream if possible, enqueue it otherwise. @@ -551,15 +559,14 @@ always_inline word unix_vlib_findchr(u8 chr, u8 *str, word len) * @param buffer Pointer to the buffer that needs to be written. * @param buffer_bytes The number of bytes from \c buffer to write. */ -static void unix_vlib_cli_output_raw(unix_cli_file_t * cf, - unix_file_t * uf, - u8 * buffer, - uword buffer_bytes) +static void +unix_vlib_cli_output_raw (unix_cli_file_t * cf, + unix_file_t * uf, u8 * buffer, uword buffer_bytes) { int n = 0; if (vec_len (cf->output_vector) == 0) - n = write (uf->file_descriptor, buffer, buffer_bytes); + n = write (uf->file_descriptor, buffer, buffer_bytes); if (n < 0 && errno != EAGAIN) { @@ -569,7 +576,8 @@ static void unix_vlib_cli_output_raw(unix_cli_file_t * cf, { /* We got EAGAIN or we already have stuff in the buffer; * queue up whatever didn't get sent for later. */ - if (n < 0) n = 0; + if (n < 0) + n = 0; unix_cli_add_pending_output (uf, cf, buffer + n, buffer_bytes - n); } } @@ -581,130 +589,134 @@ static void unix_vlib_cli_output_raw(unix_cli_file_t * cf, * @param buffer Pointer to the buffer that needs to be written. * @param buffer_bytes The number of bytes from \c buffer to write. */ -static void unix_vlib_cli_output_cooked(unix_cli_file_t * cf, - unix_file_t * uf, - u8 * buffer, - uword buffer_bytes) +static void +unix_vlib_cli_output_cooked (unix_cli_file_t * cf, + unix_file_t * uf, + u8 * buffer, uword buffer_bytes) { word end = 0, start = 0; while (end < buffer_bytes) { if (cf->crlf_mode) - { - /* iterate the line on \n's so we can insert a \r before it */ - end = unix_vlib_findchr('\n', - buffer + start, - buffer_bytes - start) + start; - } + { + /* iterate the line on \n's so we can insert a \r before it */ + end = unix_vlib_findchr ('\n', + buffer + start, + buffer_bytes - start) + start; + } else - { - /* otherwise just send the whole buffer */ - end = buffer_bytes; - } + { + /* otherwise just send the whole buffer */ + end = buffer_bytes; + } - unix_vlib_cli_output_raw(cf, uf, buffer + start, end - start); + unix_vlib_cli_output_raw (cf, uf, buffer + start, end - start); if (cf->crlf_mode) - { - if (end < buffer_bytes) - { - unix_vlib_cli_output_raw(cf, uf, (u8 *)"\r\n", 2); - end ++; /* skip the \n that we already sent */ - } - start = end; - } + { + if (end < buffer_bytes) + { + unix_vlib_cli_output_raw (cf, uf, (u8 *) "\r\n", 2); + end++; /* skip the \n that we already sent */ + } + start = end; + } } } /** \brief Output the CLI prompt */ -static void unix_cli_cli_prompt(unix_cli_file_t * cf, unix_file_t * uf) +static void +unix_cli_cli_prompt (unix_cli_file_t * cf, unix_file_t * uf) { - unix_cli_main_t * cm = &unix_cli_main; + unix_cli_main_t *cm = &unix_cli_main; unix_vlib_cli_output_raw (cf, uf, cm->cli_prompt, vec_len (cm->cli_prompt)); } /** \brief Output a pager prompt and show number of buffered lines */ -static void unix_cli_pager_prompt(unix_cli_file_t * cf, unix_file_t * uf) +static void +unix_cli_pager_prompt (unix_cli_file_t * cf, unix_file_t * uf) { - u8 * prompt; + u8 *prompt; u32 h; h = cf->pager_start + (cf->height - 1); if (h > vec_len (cf->pager_index)) h = vec_len (cf->pager_index); - prompt = format(0, "\r%s-- more -- (%d-%d/%d)%s", - cf->ansi_capable ? ANSI_BOLD : "", - cf->pager_start + 1, - h, - vec_len (cf->pager_index), - cf->ansi_capable ? ANSI_RESET: ""); + prompt = format (0, "\r%s-- more -- (%d-%d/%d)%s", + cf->ansi_capable ? ANSI_BOLD : "", + cf->pager_start + 1, + h, + vec_len (cf->pager_index), + cf->ansi_capable ? ANSI_RESET : ""); - unix_vlib_cli_output_cooked(cf, uf, prompt, vec_len(prompt)); + unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt)); - vec_free(prompt); + vec_free (prompt); } /** \brief Output a pager "skipping" message */ -static void unix_cli_pager_message(unix_cli_file_t * cf, unix_file_t * uf, - char *message, char *postfix) +static void +unix_cli_pager_message (unix_cli_file_t * cf, unix_file_t * uf, + char *message, char *postfix) { - u8 * prompt; + u8 *prompt; - prompt = format(0, "\r%s-- %s --%s%s", - cf->ansi_capable ? ANSI_BOLD : "", - message, - cf->ansi_capable ? ANSI_RESET: "", - postfix); + prompt = format (0, "\r%s-- %s --%s%s", + cf->ansi_capable ? ANSI_BOLD : "", + message, cf->ansi_capable ? ANSI_RESET : "", postfix); - unix_vlib_cli_output_cooked(cf, uf, prompt, vec_len(prompt)); + unix_vlib_cli_output_cooked (cf, uf, prompt, vec_len (prompt)); - vec_free(prompt); + vec_free (prompt); } /** \brief Erase the printed pager prompt */ -static void unix_cli_pager_prompt_erase(unix_cli_file_t * cf, unix_file_t * uf) +static void +unix_cli_pager_prompt_erase (unix_cli_file_t * cf, unix_file_t * uf) { if (cf->ansi_capable) { - unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1); - unix_vlib_cli_output_cooked(cf, uf, - (u8 *)ANSI_CLEARLINE, sizeof(ANSI_CLEARLINE) - 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); + unix_vlib_cli_output_cooked (cf, uf, + (u8 *) ANSI_CLEARLINE, + sizeof (ANSI_CLEARLINE) - 1); } else { int i; - unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1); - for (i = 0; i < cf->width - 1; i ++) - unix_vlib_cli_output_cooked(cf, uf, (u8 *)" ", 1); - unix_vlib_cli_output_cooked(cf, uf, (u8 *)"\r", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); + for (i = 0; i < cf->width - 1; i++) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\r", 1); } } /** \brief Uses an ANSI escape sequence to move the cursor */ -static void unix_cli_ansi_cursor(unix_cli_file_t * cf, unix_file_t * uf, - u16 x, u16 y) +static void +unix_cli_ansi_cursor (unix_cli_file_t * cf, unix_file_t * uf, u16 x, u16 y) { - u8 * str; + u8 *str; - str = format(0, "%s%d;%dH", CSI, y, x); + str = format (0, "%s%d;%dH", CSI, y, x); - unix_vlib_cli_output_cooked(cf, uf, str, vec_len(str)); + unix_vlib_cli_output_cooked (cf, uf, str, vec_len (str)); - vec_free(str); + vec_free (str); } /** Redraw the currently displayed page of text. * @param cf CLI session to redraw the pager buffer of. * @param uf Unix file of the CLI session. */ -static void unix_cli_pager_redraw(unix_cli_file_t * cf, unix_file_t * uf) +static void +unix_cli_pager_redraw (unix_cli_file_t * cf, unix_file_t * uf) { - unix_cli_pager_index_t * pi = NULL; - u8 * line = NULL; + unix_cli_pager_index_t *pi = NULL; + u8 *line = NULL; word i; /* No active pager? Do nothing. */ @@ -715,7 +727,8 @@ static void unix_cli_pager_redraw(unix_cli_file_t * cf, unix_file_t * uf) { /* If we have ANSI, send the clear screen sequence */ unix_vlib_cli_output_cooked (cf, uf, - (u8 *)ANSI_CLEAR, sizeof(ANSI_CLEAR) - 1); + (u8 *) ANSI_CLEAR, + sizeof (ANSI_CLEAR) - 1); } else { @@ -725,8 +738,7 @@ static void unix_cli_pager_redraw(unix_cli_file_t * cf, unix_file_t * uf) /* (Re-)send the current page of content */ for (i = 0; i < cf->height - 1 && - i + cf->pager_start < vec_len (cf->pager_index); - i ++) + i + cf->pager_start < vec_len (cf->pager_index); i++) { pi = &cf->pager_index[cf->pager_start + i]; line = cf->pager_vector[pi->line] + pi->offset; @@ -735,7 +747,7 @@ static void unix_cli_pager_redraw(unix_cli_file_t * cf, unix_file_t * uf) } /* if the last line didn't end in newline, add a newline */ if (pi && line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); unix_cli_pager_prompt (cf, uf); } @@ -761,15 +773,14 @@ static void unix_cli_pager_redraw(unix_cli_file_t * cf, unix_file_t * uf) * value provides the index in the pager buffer of an * existing string to be indexed. */ -static void unix_cli_pager_add_line (unix_cli_file_t * cf, - u8 * line, - word len_or_index) +static void +unix_cli_pager_add_line (unix_cli_file_t * cf, u8 * line, word len_or_index) { - u8 * p; + u8 *p; word i, j, k; word line_index, len; u32 width = cf->width; - unix_cli_pager_index_t * pi; + unix_cli_pager_index_t *pi; if (line == NULL) { @@ -795,10 +806,10 @@ static void unix_cli_pager_add_line (unix_cli_file_t * cf, { /* Find the next line, or run to terminal width, or run to EOL */ int l = len - i; - j = unix_vlib_findchr((u8)'\n', p, l < width ? l : width); + j = unix_vlib_findchr ((u8) '\n', p, l < width ? l : width); - if (j < l && p[j] == '\n') /* incl \n */ - j ++; + if (j < l && p[j] == '\n') /* incl \n */ + j++; /* Add the line to the index */ k = vec_len (cf->pager_index); @@ -823,10 +834,11 @@ static void unix_cli_pager_add_line (unix_cli_file_t * cf, * * @param cf The CLI session whose pager buffer should be reindexed. */ -static void unix_cli_pager_reindex (unix_cli_file_t * cf) +static void +unix_cli_pager_reindex (unix_cli_file_t * cf) { word i, old_line, old_offset; - unix_cli_pager_index_t * pi; + unix_cli_pager_index_t *pi; /* If there is nothing in the pager buffer then make sure the index * is empty and move on. @@ -846,25 +858,24 @@ static void unix_cli_pager_reindex (unix_cli_file_t * cf) /* Re-add the buffered lines to the index */ vec_reset_length (cf->pager_index); - vec_foreach_index(i, cf->pager_vector) - { - unix_cli_pager_add_line(cf, NULL, i); - } + vec_foreach_index (i, cf->pager_vector) + { + unix_cli_pager_add_line (cf, NULL, i); + } /* Attempt to re-locate the previously stored page start line */ - vec_foreach_index(i, cf->pager_index) - { - pi = &cf->pager_index[i]; - - if (pi->line == old_line && - (pi->offset <= old_offset || - pi->offset + pi->length > old_offset)) - { - /* Found it! */ - cf->pager_start = i; - break; - } - } + vec_foreach_index (i, cf->pager_index) + { + pi = &cf->pager_index[i]; + + if (pi->line == old_line && + (pi->offset <= old_offset || pi->offset + pi->length > old_offset)) + { + /* Found it! */ + cf->pager_start = i; + break; + } + } /* In case the start line was not found (rare), ensure the pager start * index is within bounds @@ -872,9 +883,9 @@ static void unix_cli_pager_reindex (unix_cli_file_t * cf) if (cf->pager_start >= vec_len (cf->pager_index)) { if (!cf->height || vec_len (cf->pager_index) < (cf->height - 1)) - cf->pager_start = 0; + cf->pager_start = 0; else - cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); + cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); } } @@ -896,14 +907,13 @@ static void unix_cli_pager_reindex (unix_cli_file_t * cf) * @param buffer String of printabe bytes to be output. * @param buffer_bytes The number of bytes in @c buffer to be output. */ -static void unix_vlib_cli_output (uword cli_file_index, - u8 * buffer, - uword buffer_bytes) +static void +unix_vlib_cli_output (uword cli_file_index, u8 * buffer, uword buffer_bytes) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; - unix_file_t * uf; + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; + unix_file_t *uf; cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); @@ -915,8 +925,8 @@ static void unix_vlib_cli_output (uword cli_file_index, else { word row = vec_len (cf->pager_index); - u8 * line; - unix_cli_pager_index_t * pi; + u8 *line; + unix_cli_pager_index_t *pi; /* Index and add the output lines to the pager buffer. */ unix_cli_pager_add_line (cf, buffer, buffer_bytes); @@ -925,49 +935,49 @@ static void unix_vlib_cli_output (uword cli_file_index, * If we reach the bottom of the page, display a prompt. */ while (row < vec_len (cf->pager_index)) - { - if (row < cf->height - 1) - { - /* output this line */ - pi = &cf->pager_index[row]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - - /* if the last line didn't end in newline, and we're at the - * bottom of the page, add a newline */ - if (line[pi->length - 1] != '\n' && row == cf->height - 2) - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - } - else - { - /* Display the pager prompt every 10 lines */ - if (!(row % 10)) - unix_cli_pager_prompt(cf, uf); - } - row ++; - } + { + if (row < cf->height - 1) + { + /* output this line */ + pi = &cf->pager_index[row]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + + /* if the last line didn't end in newline, and we're at the + * bottom of the page, add a newline */ + if (line[pi->length - 1] != '\n' && row == cf->height - 2) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + } + else + { + /* Display the pager prompt every 10 lines */ + if (!(row % 10)) + unix_cli_pager_prompt (cf, uf); + } + row++; + } /* Check if we went over the pager buffer limit */ if (vec_len (cf->pager_index) > um->cli_pager_buffer_limit) - { - /* Stop using the pager for the remainder of this CLI command */ - cf->no_pager = 2; - - /* If we likely printed the prompt, erase it */ - if (vec_len (cf->pager_index) > cf->height - 1) - unix_cli_pager_prompt_erase (cf, uf); - - /* Dump out the contents of the buffer */ - for (row = cf->pager_start + (cf->height - 1); - row < vec_len (cf->pager_index); row ++) - { - pi = &cf->pager_index[row]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - } - - unix_cli_pager_reset (cf); - } + { + /* Stop using the pager for the remainder of this CLI command */ + cf->no_pager = 2; + + /* If we likely printed the prompt, erase it */ + if (vec_len (cf->pager_index) > cf->height - 1) + unix_cli_pager_prompt_erase (cf, uf); + + /* Dump out the contents of the buffer */ + for (row = cf->pager_start + (cf->height - 1); + row < vec_len (cf->pager_index); row++) + { + pi = &cf->pager_index[row]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + } + + unix_cli_pager_reset (cf); + } } } @@ -984,7 +994,8 @@ static void unix_vlib_cli_output (uword cli_file_index, * @return @c 1 if the terminal type is recognized as supporting ANSI * terminal sequences; @c 0 otherwise. */ -static u8 unix_cli_terminal_type(u8 * term, uword len) +static u8 +unix_cli_terminal_type (u8 * term, uword len) { /* This may later be better done as a hash of some sort. */ #define _(a) do { \ @@ -993,19 +1004,20 @@ static u8 unix_cli_terminal_type(u8 * term, uword len) _("xterm"); _("xterm-color"); - _("xterm-256color"); /* iTerm on Mac */ + _("xterm-256color"); /* iTerm on Mac */ _("screen"); - _("ansi"); /* Microsoft Telnet */ + _("ansi"); /* Microsoft Telnet */ #undef _ return 0; } /** \brief Emit initial welcome banner and prompt on a connection. */ -static void unix_cli_file_welcome(unix_cli_main_t * cm, unix_cli_file_t * cf) +static void +unix_cli_file_welcome (unix_cli_main_t * cm, unix_cli_file_t * cf) { - unix_main_t * um = &unix_main; - unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); + unix_main_t *um = &unix_main; + unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); unix_cli_banner_t *banner; int i, len; @@ -1014,28 +1026,27 @@ static void unix_cli_file_welcome(unix_cli_main_t * cm, unix_cli_file_t * cf) * queued until everything is ready. (oterwise initial prompt can appear * mid way through VPP initialization) */ - unix_cli_add_pending_output (uf, cf, (u8 *)"\r", 1); + unix_cli_add_pending_output (uf, cf, (u8 *) "\r", 1); if (!um->cli_no_banner) { if (cf->ansi_capable) - { - banner = unix_cli_banner_color; - len = ARRAY_LEN(unix_cli_banner_color); - } + { + banner = unix_cli_banner_color; + len = ARRAY_LEN (unix_cli_banner_color); + } else - { - banner = unix_cli_banner; - len = ARRAY_LEN(unix_cli_banner); - } + { + banner = unix_cli_banner; + len = ARRAY_LEN (unix_cli_banner); + } for (i = 0; i < len; i++) - { - unix_vlib_cli_output_cooked(cf, uf, - banner[i].line, - banner[i].length); - } - } + { + unix_vlib_cli_output_cooked (cf, uf, + banner[i].line, banner[i].length); + } + } /* Prompt. */ unix_cli_cli_prompt (cf, uf); @@ -1045,20 +1056,21 @@ static void unix_cli_file_welcome(unix_cli_main_t * cm, unix_cli_file_t * cf) /** \brief A failsafe triggered on a timer to ensure we send the prompt * to telnet sessions that fail to negotiate the terminal type. */ -static void unix_cli_file_welcome_timer(any arg, f64 delay) +static void +unix_cli_file_welcome_timer (any arg, f64 delay) { - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; - (void)delay; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; + (void) delay; /* Check the connection didn't close already */ - if (pool_is_free_index (cm->cli_file_pool, (uword)arg)) + if (pool_is_free_index (cm->cli_file_pool, (uword) arg)) return; - cf = pool_elt_at_index (cm->cli_file_pool, (uword)arg); + cf = pool_elt_at_index (cm->cli_file_pool, (uword) arg); if (!cf->started) - unix_cli_file_welcome(cm, cf); + unix_cli_file_welcome (cm, cf); } /** \brief A mostly no-op Telnet state machine. @@ -1069,11 +1081,10 @@ static void unix_cli_file_welcome_timer(any arg, f64 delay) * bytes to consume from the input_vector, not including the initial * IAC byte. */ -static i32 unix_cli_process_telnet(unix_main_t * um, - unix_cli_file_t * cf, - unix_file_t * uf, - u8 * input_vector, - uword len) +static i32 +unix_cli_process_telnet (unix_main_t * um, + unix_cli_file_t * cf, + unix_file_t * uf, u8 * input_vector, uword len) { /* Input_vector starts at IAC byte. * See if we have a complete message; if not, return -1 so we wait for more. @@ -1082,124 +1093,126 @@ static i32 unix_cli_process_telnet(unix_main_t * um, i32 consume = 0; if (len == 1) - return -1; /* want more bytes */ + return -1; /* want more bytes */ switch (input_vector[1]) { - case IAC: - /* two IAC's in a row means to pass through 0xff. - * since that makes no sense here, just consume it. - */ - consume = 1; - break; - - case WILL: - case WONT: - case DO: - case DONT: - /* Expect 3 bytes */ - if (vec_len(input_vector) < 3) - return -1; /* want more bytes */ - - consume = 2; - break; - - case SB: - { - /* Sub option - search ahead for IAC SE to end it */ - i32 i; - for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++) - { - if (input_vector[i - 1] == IAC && input_vector[i] == SE) - { - /* We have a complete message; see if we care about it */ - switch (input_vector[2]) - { - case TELOPT_TTYPE: - if (input_vector[3] != 0) - break; - /* See if the terminal type is ANSI capable */ - cf->ansi_capable = - unix_cli_terminal_type(input_vector + 4, i - 5); - /* If session not started, we can release the pause */ - if (!cf->started) - /* Send the welcome banner and initial prompt */ - unix_cli_file_welcome(&unix_cli_main, cf); - break; - - case TELOPT_NAWS: - /* Window size */ - if (i != 8) /* check message is correct size */ - break; - cf->width = clib_net_to_host_u16(*((u16 *)(input_vector + 3))); - cf->height = clib_net_to_host_u16(*((u16 *)(input_vector + 5))); - /* reindex pager buffer */ - unix_cli_pager_reindex (cf); - /* redraw page */ - unix_cli_pager_redraw (cf, uf); - break; - - default: - break; - } - /* Consume it all */ - consume = i; - break; - } - } - - if (i == UNIX_CLI_MAX_DEPTH_TELNET) - consume = 1; /* hit max search depth, advance one byte */ - - if (consume == 0) - return -1; /* want more bytes */ - - break; - } - - case GA: - case EL: - case EC: - case AO: - case IP: - case BREAK: - case DM: - case NOP: - case SE: - case EOR: - case ABORT: - case SUSP: - case xEOF: - /* Simple one-byte messages */ - consume = 1; - break; - - case AYT: - /* Are You There - trigger a visible response */ - consume = 1; - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10); - break; - - default: - /* Unknown command! Eat the IAC byte */ - break; + case IAC: + /* two IAC's in a row means to pass through 0xff. + * since that makes no sense here, just consume it. + */ + consume = 1; + break; + + case WILL: + case WONT: + case DO: + case DONT: + /* Expect 3 bytes */ + if (vec_len (input_vector) < 3) + return -1; /* want more bytes */ + + consume = 2; + break; + + case SB: + { + /* Sub option - search ahead for IAC SE to end it */ + i32 i; + for (i = 3; i < len && i < UNIX_CLI_MAX_DEPTH_TELNET; i++) + { + if (input_vector[i - 1] == IAC && input_vector[i] == SE) + { + /* We have a complete message; see if we care about it */ + switch (input_vector[2]) + { + case TELOPT_TTYPE: + if (input_vector[3] != 0) + break; + /* See if the terminal type is ANSI capable */ + cf->ansi_capable = + unix_cli_terminal_type (input_vector + 4, i - 5); + /* If session not started, we can release the pause */ + if (!cf->started) + /* Send the welcome banner and initial prompt */ + unix_cli_file_welcome (&unix_cli_main, cf); + break; + + case TELOPT_NAWS: + /* Window size */ + if (i != 8) /* check message is correct size */ + break; + cf->width = + clib_net_to_host_u16 (*((u16 *) (input_vector + 3))); + cf->height = + clib_net_to_host_u16 (*((u16 *) (input_vector + 5))); + /* reindex pager buffer */ + unix_cli_pager_reindex (cf); + /* redraw page */ + unix_cli_pager_redraw (cf, uf); + break; + + default: + break; + } + /* Consume it all */ + consume = i; + break; + } + } + + if (i == UNIX_CLI_MAX_DEPTH_TELNET) + consume = 1; /* hit max search depth, advance one byte */ + + if (consume == 0) + return -1; /* want more bytes */ + + break; + } + + case GA: + case EL: + case EC: + case AO: + case IP: + case BREAK: + case DM: + case NOP: + case SE: + case EOR: + case ABORT: + case SUSP: + case xEOF: + /* Simple one-byte messages */ + consume = 1; + break; + + case AYT: + /* Are You There - trigger a visible response */ + consume = 1; + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "fd.io VPP\n", 10); + break; + + default: + /* Unknown command! Eat the IAC byte */ + break; } - return consume; + return consume; } /** \brief Process actionable input. * Based on the \c action process the input; this typically involves * searching the command history or editing the current command line. */ -static int unix_cli_line_process_one(unix_cli_main_t * cm, - unix_main_t * um, - unix_cli_file_t * cf, - unix_file_t * uf, - u8 input, - unix_cli_parse_action_t action) +static int +unix_cli_line_process_one (unix_cli_main_t * cm, + unix_main_t * um, + unix_cli_file_t * cf, + unix_file_t * uf, + u8 input, unix_cli_parse_action_t action) { - u8 * prev; + u8 *prev; int j, delta; switch (action) @@ -1210,31 +1223,31 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_REVSEARCH: case UNIX_CLI_PARSE_ACTION_FWDSEARCH: if (!cf->has_history || !cf->history_limit) - break; + break; if (cf->search_mode == 0) - { - /* Erase the current command (if any) */ - for (j = 0; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); - - vec_reset_length (cf->search_key); - vec_reset_length (cf->current_command); - if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH) - cf->search_mode = -1; - else - cf->search_mode = 1; - cf->cursor = 0; - } + { + /* Erase the current command (if any) */ + for (j = 0; j < (vec_len (cf->current_command)); j++) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); + + vec_reset_length (cf->search_key); + vec_reset_length (cf->current_command); + if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH) + cf->search_mode = -1; + else + cf->search_mode = 1; + cf->cursor = 0; + } else - { - if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH) - cf->search_mode = -1; - else - cf->search_mode = 1; - - cf->excursion += cf->search_mode; - goto search_again; - } + { + if (action == UNIX_CLI_PARSE_ACTION_REVSEARCH) + cf->search_mode = -1; + else + cf->search_mode = 1; + + cf->excursion += cf->search_mode; + goto search_again; + } break; case UNIX_CLI_PARSE_ACTION_ERASELINELEFT: @@ -1243,25 +1256,23 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, /* Shimmy forwards to the new end of line position */ delta = vec_len (cf->current_command) - cf->cursor; for (j = cf->cursor; j > delta; j--) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); /* Zap from here to the end of what is currently displayed */ for (; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); /* Get back to the start of the line */ for (j = 0; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - j = vec_len(cf->current_command) - cf->cursor; - memmove(cf->current_command, - cf->current_command + cf->cursor, - j); - _vec_len(cf->current_command) = j; + j = vec_len (cf->current_command) - cf->cursor; + memmove (cf->current_command, cf->current_command + cf->cursor, j); + _vec_len (cf->current_command) = j; /* Print the new contents */ unix_vlib_cli_output_cooked (cf, uf, cf->current_command, j); /* Shimmy back to the start */ for (j = 0; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); cf->cursor = 0; cf->search_mode = 0; @@ -1272,34 +1283,35 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, /* Zap from cursor to end of what is currently displayed */ for (j = cf->cursor; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); /* Get back to where we were */ for (j = cf->cursor; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); /* Truncate the line at the cursor */ - _vec_len(cf->current_command) = cf->cursor; + _vec_len (cf->current_command) = cf->cursor; cf->search_mode = 0; break; case UNIX_CLI_PARSE_ACTION_LEFT: if (cf->cursor > 0) - { - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - cf->cursor --; - } + { + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + cf->cursor--; + } cf->search_mode = 0; break; case UNIX_CLI_PARSE_ACTION_RIGHT: - if (cf->cursor < vec_len(cf->current_command)) - { - /* have to emit the character under the cursor */ - unix_vlib_cli_output_cooked (cf, uf, cf->current_command + cf->cursor, 1); - cf->cursor ++; - } + if (cf->cursor < vec_len (cf->current_command)) + { + /* have to emit the character under the cursor */ + unix_vlib_cli_output_cooked (cf, uf, + cf->current_command + cf->cursor, 1); + cf->cursor++; + } cf->search_mode = 0; break; @@ -1307,123 +1319,124 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_UP: case UNIX_CLI_PARSE_ACTION_DOWN: if (!cf->has_history || !cf->history_limit) - break; + break; cf->search_mode = 0; /* Erase the command */ for (j = cf->cursor; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) " ", 1); for (j = 0; j < (vec_len (cf->current_command)); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); vec_reset_length (cf->current_command); if (vec_len (cf->command_history)) - { - if (action == UNIX_CLI_PARSE_ACTION_UP) - delta = -1; - else - delta = 1; - - cf->excursion += delta; - - if (cf->excursion == vec_len (cf->command_history)) - { - /* down-arrowed to last entry - want a blank line */ - _vec_len (cf->current_command) = 0; - } - else if (cf->excursion < 0) - { - /* up-arrowed over the start to the end, want a blank line */ - cf->excursion = vec_len (cf->command_history); - _vec_len (cf->current_command) = 0; - } - else - { - if (cf->excursion > (i32) vec_len (cf->command_history) -1) - /* down-arrowed past end - wrap to start */ - cf->excursion = 0; - - /* Print the command at the current position */ - prev = cf->command_history [cf->excursion]; - vec_validate (cf->current_command, vec_len(prev)-1); - - clib_memcpy (cf->current_command, prev, vec_len(prev)); - _vec_len (cf->current_command) = vec_len(prev); - unix_vlib_cli_output_cooked (cf, uf, cf->current_command, - vec_len (cf->current_command)); - } - cf->cursor = vec_len(cf->current_command); - - break; - } + { + if (action == UNIX_CLI_PARSE_ACTION_UP) + delta = -1; + else + delta = 1; + + cf->excursion += delta; + + if (cf->excursion == vec_len (cf->command_history)) + { + /* down-arrowed to last entry - want a blank line */ + _vec_len (cf->current_command) = 0; + } + else if (cf->excursion < 0) + { + /* up-arrowed over the start to the end, want a blank line */ + cf->excursion = vec_len (cf->command_history); + _vec_len (cf->current_command) = 0; + } + else + { + if (cf->excursion > (i32) vec_len (cf->command_history) - 1) + /* down-arrowed past end - wrap to start */ + cf->excursion = 0; + + /* Print the command at the current position */ + prev = cf->command_history[cf->excursion]; + vec_validate (cf->current_command, vec_len (prev) - 1); + + clib_memcpy (cf->current_command, prev, vec_len (prev)); + _vec_len (cf->current_command) = vec_len (prev); + unix_vlib_cli_output_cooked (cf, uf, cf->current_command, + vec_len (cf->current_command)); + } + cf->cursor = vec_len (cf->current_command); + + break; + } break; case UNIX_CLI_PARSE_ACTION_HOME: if (vec_len (cf->current_command) && cf->cursor > 0) - { - while (cf->cursor) - { - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - cf->cursor --; - } - } + { + while (cf->cursor) + { + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + cf->cursor--; + } + } cf->search_mode = 0; break; case UNIX_CLI_PARSE_ACTION_END: if (vec_len (cf->current_command) && - cf->cursor < vec_len(cf->current_command)) - { - unix_vlib_cli_output_cooked (cf, uf, - cf->current_command + cf->cursor, - vec_len(cf->current_command) - cf->cursor); - cf->cursor = vec_len(cf->current_command); - } + cf->cursor < vec_len (cf->current_command)) + { + unix_vlib_cli_output_cooked (cf, uf, + cf->current_command + cf->cursor, + vec_len (cf->current_command) - + cf->cursor); + cf->cursor = vec_len (cf->current_command); + } cf->search_mode = 0; break; case UNIX_CLI_PARSE_ACTION_WORDLEFT: if (vec_len (cf->current_command) && cf->cursor > 0) - { - j = cf->cursor; - - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - j --; - - while (j && isspace(cf->current_command[j])) - { - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - j --; - } - while (j && !isspace(cf->current_command[j])) - { - if (isspace(cf->current_command[j - 1])) - break; - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - j --; - } - - cf->cursor = j; - } + { + j = cf->cursor; + + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + j--; + + while (j && isspace (cf->current_command[j])) + { + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + j--; + } + while (j && !isspace (cf->current_command[j])) + { + if (isspace (cf->current_command[j - 1])) + break; + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + j--; + } + + cf->cursor = j; + } cf->search_mode = 0; break; case UNIX_CLI_PARSE_ACTION_WORDRIGHT: if (vec_len (cf->current_command) && - cf->cursor < vec_len(cf->current_command)) - { - int e = vec_len(cf->current_command); - j = cf->cursor; - while (j < e && !isspace(cf->current_command[j])) - j ++; - while (j < e && isspace(cf->current_command[j])) - j ++; - unix_vlib_cli_output_cooked (cf, uf, - cf->current_command + cf->cursor, - j - cf->cursor); - cf->cursor = j; - } + cf->cursor < vec_len (cf->current_command)) + { + int e = vec_len (cf->current_command); + j = cf->cursor; + while (j < e && !isspace (cf->current_command[j])) + j++; + while (j < e && isspace (cf->current_command[j])) + j++; + unix_vlib_cli_output_cooked (cf, uf, + cf->current_command + cf->cursor, + j - cf->cursor); + cf->cursor = j; + } cf->search_mode = 0; break; @@ -1431,32 +1444,32 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_ERASE: if (vec_len (cf->current_command)) - { - if (cf->cursor == vec_len(cf->current_command)) - { - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); - _vec_len (cf->current_command)--; - cf->cursor --; - } - else if (cf->cursor > 0) - { - /* shift everything at & to the right of the cursor left by 1 */ - j = vec_len (cf->current_command) - cf->cursor; - memmove (cf->current_command + cf->cursor - 1, - cf->current_command + cf->cursor, - j); - _vec_len (cf->current_command)--; - cf->cursor --; - /* redraw the rest of the line */ - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - unix_vlib_cli_output_cooked (cf, uf, - cf->current_command + cf->cursor, j); - unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3); - /* and shift the terminal cursor back where it should be */ - while (-- j) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - } - } + { + if (cf->cursor == vec_len (cf->current_command)) + { + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); + _vec_len (cf->current_command)--; + cf->cursor--; + } + else if (cf->cursor > 0) + { + /* shift everything at & to the right of the cursor left by 1 */ + j = vec_len (cf->current_command) - cf->cursor; + memmove (cf->current_command + cf->cursor - 1, + cf->current_command + cf->cursor, j); + _vec_len (cf->current_command)--; + cf->cursor--; + /* redraw the rest of the line */ + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + unix_vlib_cli_output_cooked (cf, uf, + cf->current_command + cf->cursor, + j); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b\b", 3); + /* and shift the terminal cursor back where it should be */ + while (--j) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + } + } cf->search_mode = 0; cf->excursion = 0; vec_reset_length (cf->search_key); @@ -1464,37 +1477,37 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_ERASERIGHT: if (vec_len (cf->current_command)) - { - if (cf->cursor < vec_len(cf->current_command)) - { - /* shift everything to the right of the cursor left by 1 */ - j = vec_len (cf->current_command) - cf->cursor - 1; - memmove (cf->current_command + cf->cursor, - cf->current_command + cf->cursor + 1, - j); - _vec_len (cf->current_command)--; - /* redraw the rest of the line */ - unix_vlib_cli_output_cooked (cf, uf, - cf->current_command + cf->cursor, j); - unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2); - /* and shift the terminal cursor back where it should be */ - if (j) - { - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - while (-- j) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); - } - } - } + { + if (cf->cursor < vec_len (cf->current_command)) + { + /* shift everything to the right of the cursor left by 1 */ + j = vec_len (cf->current_command) - cf->cursor - 1; + memmove (cf->current_command + cf->cursor, + cf->current_command + cf->cursor + 1, j); + _vec_len (cf->current_command)--; + /* redraw the rest of the line */ + unix_vlib_cli_output_cooked (cf, uf, + cf->current_command + cf->cursor, + j); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) " \b", 2); + /* and shift the terminal cursor back where it should be */ + if (j) + { + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + while (--j) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + } + } + } else if (input == 'D' - '@') - { - /* ^D with no command entered = quit */ - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5); - vlib_process_signal_event (um->vlib_main, - vlib_current_process (um->vlib_main), - UNIX_CLI_PROCESS_EVENT_QUIT, - cf - cm->cli_file_pool); - } + { + /* ^D with no command entered = quit */ + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "quit\n", 5); + vlib_process_signal_event (um->vlib_main, + vlib_current_process (um->vlib_main), + UNIX_CLI_PROCESS_EVENT_QUIT, + cf - cm->cli_file_pool); + } cf->search_mode = 0; cf->excursion = 0; vec_reset_length (cf->search_key); @@ -1506,20 +1519,19 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, * the cursor back where it was in that line. */ if (cf->ansi_capable) - unix_vlib_cli_output_cooked (cf, uf, - (u8 *) ANSI_CLEAR, - sizeof(ANSI_CLEAR)-1); + unix_vlib_cli_output_cooked (cf, uf, + (u8 *) ANSI_CLEAR, + sizeof (ANSI_CLEAR) - 1); else - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); unix_vlib_cli_output_raw (cf, uf, - cm->cli_prompt, - vec_len (cm->cli_prompt)); + cm->cli_prompt, vec_len (cm->cli_prompt)); unix_vlib_cli_output_raw (cf, uf, - cf->current_command, - vec_len (cf->current_command)); - for (j = cf->cursor; j < vec_len(cf->current_command); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); + cf->current_command, + vec_len (cf->current_command)); + for (j = cf->cursor; j < vec_len (cf->current_command); j++) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b", 1); break; @@ -1540,171 +1552,178 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_PAGER_PGDN: /* show next page of the buffer */ if (cf->height + cf->pager_start < vec_len (cf->pager_index)) - { - u8 * line = NULL; - unix_cli_pager_index_t * pi = NULL; - - int m = cf->pager_start + (cf->height - 1); - unix_cli_pager_prompt_erase (cf, uf); - for (j = m; - j < vec_len (cf->pager_index) && cf->pager_start < m; - j ++, cf->pager_start ++) - { - pi = &cf->pager_index[j]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - } - /* if the last line didn't end in newline, add a newline */ - if (pi && line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - unix_cli_pager_prompt (cf, uf); - } + { + u8 *line = NULL; + unix_cli_pager_index_t *pi = NULL; + + int m = cf->pager_start + (cf->height - 1); + unix_cli_pager_prompt_erase (cf, uf); + for (j = m; + j < vec_len (cf->pager_index) && cf->pager_start < m; + j++, cf->pager_start++) + { + pi = &cf->pager_index[j]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + } + /* if the last line didn't end in newline, add a newline */ + if (pi && line[pi->length - 1] != '\n') + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_cli_pager_prompt (cf, uf); + } else - { - if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT) - /* no more in buffer, exit, but only if it was <space> */ - goto pager_quit; - } + { + if (action == UNIX_CLI_PARSE_ACTION_PAGER_NEXT) + /* no more in buffer, exit, but only if it was <space> */ + goto pager_quit; + } break; case UNIX_CLI_PARSE_ACTION_PAGER_DN: case UNIX_CLI_PARSE_ACTION_PAGER_CRLF: /* display the next line of the buffer */ if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1)) - { - u8 * line; - unix_cli_pager_index_t * pi; - - unix_cli_pager_prompt_erase (cf, uf); - pi = &cf->pager_index[cf->pager_start + (cf->height - 1)]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - cf->pager_start ++; - /* if the last line didn't end in newline, add a newline */ - if (line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - unix_cli_pager_prompt (cf, uf); - } + { + u8 *line; + unix_cli_pager_index_t *pi; + + unix_cli_pager_prompt_erase (cf, uf); + pi = &cf->pager_index[cf->pager_start + (cf->height - 1)]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + cf->pager_start++; + /* if the last line didn't end in newline, add a newline */ + if (line[pi->length - 1] != '\n') + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_cli_pager_prompt (cf, uf); + } else - { - if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF) - /* no more in buffer, exit, but only if it was <enter> */ - goto pager_quit; - } + { + if (action == UNIX_CLI_PARSE_ACTION_PAGER_CRLF) + /* no more in buffer, exit, but only if it was <enter> */ + goto pager_quit; + } break; case UNIX_CLI_PARSE_ACTION_PAGER_UP: /* scroll the page back one line */ if (cf->pager_start > 0) - { - u8 * line = NULL; - unix_cli_pager_index_t * pi = NULL; - - cf->pager_start --; - if (cf->ansi_capable) - { - pi = &cf->pager_index[cf->pager_start]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_cli_pager_prompt_erase (cf, uf); - unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_SCROLLDN, sizeof(ANSI_SCROLLDN) - 1); - unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_SAVECURSOR, sizeof(ANSI_SAVECURSOR) - 1); - unix_cli_ansi_cursor(cf, uf, 1, 1); - unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_CLEARLINE, sizeof(ANSI_CLEARLINE) - 1); - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - unix_vlib_cli_output_cooked (cf, uf, (u8 *)ANSI_RESTCURSOR, sizeof(ANSI_RESTCURSOR) - 1); - unix_cli_pager_prompt_erase (cf, uf); - unix_cli_pager_prompt (cf, uf); - } - else - { - int m = cf->pager_start + (cf->height - 1); - unix_cli_pager_prompt_erase (cf, uf); - for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; j ++) - { - pi = &cf->pager_index[j]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - } - /* if the last line didn't end in newline, add a newline */ - if (pi && line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - unix_cli_pager_prompt (cf, uf); - } - } + { + u8 *line = NULL; + unix_cli_pager_index_t *pi = NULL; + + cf->pager_start--; + if (cf->ansi_capable) + { + pi = &cf->pager_index[cf->pager_start]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_cli_pager_prompt_erase (cf, uf); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SCROLLDN, + sizeof (ANSI_SCROLLDN) - 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_SAVECURSOR, + sizeof (ANSI_SAVECURSOR) - 1); + unix_cli_ansi_cursor (cf, uf, 1, 1); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_CLEARLINE, + sizeof (ANSI_CLEARLINE) - 1); + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + unix_vlib_cli_output_cooked (cf, uf, (u8 *) ANSI_RESTCURSOR, + sizeof (ANSI_RESTCURSOR) - 1); + unix_cli_pager_prompt_erase (cf, uf); + unix_cli_pager_prompt (cf, uf); + } + else + { + int m = cf->pager_start + (cf->height - 1); + unix_cli_pager_prompt_erase (cf, uf); + for (j = cf->pager_start; + j < vec_len (cf->pager_index) && j < m; j++) + { + pi = &cf->pager_index[j]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + } + /* if the last line didn't end in newline, add a newline */ + if (pi && line[pi->length - 1] != '\n') + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_cli_pager_prompt (cf, uf); + } + } break; case UNIX_CLI_PARSE_ACTION_PAGER_TOP: /* back to the first page of the buffer */ if (cf->pager_start > 0) - { - u8 * line = NULL; - unix_cli_pager_index_t * pi = NULL; - - cf->pager_start = 0; - int m = cf->pager_start + (cf->height - 1); - unix_cli_pager_prompt_erase (cf, uf); - for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; j ++) - { - pi = &cf->pager_index[j]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - } - /* if the last line didn't end in newline, add a newline */ - if (pi && line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - unix_cli_pager_prompt (cf, uf); - } + { + u8 *line = NULL; + unix_cli_pager_index_t *pi = NULL; + + cf->pager_start = 0; + int m = cf->pager_start + (cf->height - 1); + unix_cli_pager_prompt_erase (cf, uf); + for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; + j++) + { + pi = &cf->pager_index[j]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + } + /* if the last line didn't end in newline, add a newline */ + if (pi && line[pi->length - 1] != '\n') + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_cli_pager_prompt (cf, uf); + } break; case UNIX_CLI_PARSE_ACTION_PAGER_BOTTOM: /* skip to the last page of the buffer */ if (cf->pager_start < vec_len (cf->pager_index) - (cf->height - 1)) - { - u8 * line = NULL; - unix_cli_pager_index_t * pi = NULL; - - cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); - unix_cli_pager_prompt_erase (cf, uf); - unix_cli_pager_message (cf, uf, "skipping", "\n"); - for (j = cf->pager_start; j < vec_len (cf->pager_index); j ++) - { - pi = &cf->pager_index[j]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - } - /* if the last line didn't end in newline, add a newline */ - if (pi && line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - unix_cli_pager_prompt (cf, uf); - } + { + u8 *line = NULL; + unix_cli_pager_index_t *pi = NULL; + + cf->pager_start = vec_len (cf->pager_index) - (cf->height - 1); + unix_cli_pager_prompt_erase (cf, uf); + unix_cli_pager_message (cf, uf, "skipping", "\n"); + for (j = cf->pager_start; j < vec_len (cf->pager_index); j++) + { + pi = &cf->pager_index[j]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + } + /* if the last line didn't end in newline, add a newline */ + if (pi && line[pi->length - 1] != '\n') + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_cli_pager_prompt (cf, uf); + } break; case UNIX_CLI_PARSE_ACTION_PAGER_PGUP: /* wander back one page in the buffer */ if (cf->pager_start > 0) - { - u8 * line = NULL; - unix_cli_pager_index_t * pi = NULL; - int m; - - if (cf->pager_start >= cf->height) - cf->pager_start -= cf->height - 1; - else - cf->pager_start = 0; - m = cf->pager_start + cf->height - 1; - unix_cli_pager_prompt_erase (cf, uf); - for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; j ++) - { - pi = &cf->pager_index[j]; - line = cf->pager_vector[pi->line] + pi->offset; - unix_vlib_cli_output_cooked (cf, uf, line, pi->length); - } - /* if the last line didn't end in newline, add a newline */ - if (pi && line[pi->length - 1] != '\n') - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\n", 1); - unix_cli_pager_prompt (cf, uf); - } + { + u8 *line = NULL; + unix_cli_pager_index_t *pi = NULL; + int m; + + if (cf->pager_start >= cf->height) + cf->pager_start -= cf->height - 1; + else + cf->pager_start = 0; + m = cf->pager_start + cf->height - 1; + unix_cli_pager_prompt_erase (cf, uf); + for (j = cf->pager_start; j < vec_len (cf->pager_index) && j < m; + j++) + { + pi = &cf->pager_index[j]; + line = cf->pager_vector[pi->line] + pi->offset; + unix_vlib_cli_output_cooked (cf, uf, line, pi->length); + } + /* if the last line didn't end in newline, add a newline */ + if (pi && line[pi->length - 1] != '\n') + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); + unix_cli_pager_prompt (cf, uf); + } break; case UNIX_CLI_PARSE_ACTION_PAGER_REDRAW: @@ -1722,31 +1741,33 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\n", 1); if (cf->has_history && cf->history_limit) - { - if (cf->command_history && vec_len(cf->command_history) >= cf->history_limit) - { - vec_free (cf->command_history[0]); - vec_delete (cf->command_history, 1, 0); - } - /* Don't add blank lines to the cmd history */ - if (vec_len (cf->current_command)) - { - /* Don't duplicate the previous command */ - j = vec_len(cf->command_history); - if (j == 0 || - (vec_len (cf->current_command) != vec_len (cf->command_history[j - 1]) || - memcmp(cf->current_command, cf->command_history[j - 1], - vec_len (cf->current_command)) != 0)) - { - /* copy the command to the history */ - u8 * c = 0; - vec_append(c, cf->current_command); - vec_add1 (cf->command_history, c); - cf->command_number ++; - } - } - cf->excursion = vec_len (cf->command_history); - } + { + if (cf->command_history + && vec_len (cf->command_history) >= cf->history_limit) + { + vec_free (cf->command_history[0]); + vec_delete (cf->command_history, 1, 0); + } + /* Don't add blank lines to the cmd history */ + if (vec_len (cf->current_command)) + { + /* Don't duplicate the previous command */ + j = vec_len (cf->command_history); + if (j == 0 || + (vec_len (cf->current_command) != + vec_len (cf->command_history[j - 1]) + || memcmp (cf->current_command, cf->command_history[j - 1], + vec_len (cf->current_command)) != 0)) + { + /* copy the command to the history */ + u8 *c = 0; + vec_append (c, cf->current_command); + vec_add1 (cf->command_history, c); + cf->command_number++; + } + } + cf->excursion = vec_len (cf->command_history); + } cf->search_mode = 0; vec_reset_length (cf->search_key); @@ -1757,101 +1778,100 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_PARTIALMATCH: case UNIX_CLI_PARSE_ACTION_NOMATCH: if (vec_len (cf->pager_index)) - { - /* no-op for now */ - } - else if (cf->has_history && cf->search_mode && isprint(input)) - { - int k, limit, offset; - u8 * item; - - vec_add1 (cf->search_key, input); - - search_again: - for (j = 0; j < vec_len(cf->command_history); j++) - { - if (cf->excursion > (i32) vec_len (cf->command_history) -1) - cf->excursion = 0; - else if (cf->excursion < 0) - cf->excursion = vec_len (cf->command_history) -1; - - item = cf->command_history[cf->excursion]; - - limit = (vec_len(cf->search_key) > vec_len (item)) ? - vec_len(item) : vec_len (cf->search_key); - - for (offset = 0; offset <= vec_len(item) - limit; offset++) - { - for (k = 0; k < limit; k++) - { - if (item[k+offset] != cf->search_key[k]) - goto next_offset; - } - goto found_at_offset; - - next_offset: - ; - } - goto next; - - found_at_offset: - for (j = 0; j < vec_len (cf->current_command); j++) - unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); - - vec_validate (cf->current_command, vec_len(item)-1); - clib_memcpy (cf->current_command, item, vec_len(item)); - _vec_len (cf->current_command) = vec_len(item); - - unix_vlib_cli_output_cooked (cf, uf, cf->current_command, - vec_len (cf->current_command)); - cf->cursor = vec_len (cf->current_command); - goto found; - - next: - cf->excursion += cf->search_mode; - } - - unix_vlib_cli_output_cooked (cf, uf, (u8 *)"\nNo match...", 12); - vec_reset_length (cf->search_key); - vec_reset_length (cf->current_command); - cf->search_mode = 0; - cf->cursor = 0; - goto crlf; - } - else if (isprint(input)) /* skip any errant control codes */ - { - if (cf->cursor == vec_len(cf->current_command)) - { - /* Append to end */ - vec_add1 (cf->current_command, input); - cf->cursor ++; - - /* Echo the character back to the client */ - unix_vlib_cli_output_raw (cf, uf, &input, 1); - } - else - { - /* Insert at cursor: resize +1 byte, move everything over */ - j = vec_len (cf->current_command) - cf->cursor; - vec_add1 (cf->current_command, (u8)'A'); - memmove (cf->current_command + cf->cursor + 1, - cf->current_command + cf->cursor, - j); - cf->current_command[cf->cursor] = input; - /* Redraw the line */ - j ++; - unix_vlib_cli_output_raw (cf, uf, - cf->current_command + cf->cursor, j); - /* Put terminal cursor back */ - while (-- j) - unix_vlib_cli_output_raw (cf, uf, (u8 *)"\b", 1); - cf->cursor ++; - } - } + { + /* no-op for now */ + } + else if (cf->has_history && cf->search_mode && isprint (input)) + { + int k, limit, offset; + u8 *item; + + vec_add1 (cf->search_key, input); + + search_again: + for (j = 0; j < vec_len (cf->command_history); j++) + { + if (cf->excursion > (i32) vec_len (cf->command_history) - 1) + cf->excursion = 0; + else if (cf->excursion < 0) + cf->excursion = vec_len (cf->command_history) - 1; + + item = cf->command_history[cf->excursion]; + + limit = (vec_len (cf->search_key) > vec_len (item)) ? + vec_len (item) : vec_len (cf->search_key); + + for (offset = 0; offset <= vec_len (item) - limit; offset++) + { + for (k = 0; k < limit; k++) + { + if (item[k + offset] != cf->search_key[k]) + goto next_offset; + } + goto found_at_offset; + + next_offset: + ; + } + goto next; + + found_at_offset: + for (j = 0; j < vec_len (cf->current_command); j++) + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\b \b", 3); + + vec_validate (cf->current_command, vec_len (item) - 1); + clib_memcpy (cf->current_command, item, vec_len (item)); + _vec_len (cf->current_command) = vec_len (item); + + unix_vlib_cli_output_cooked (cf, uf, cf->current_command, + vec_len (cf->current_command)); + cf->cursor = vec_len (cf->current_command); + goto found; + + next: + cf->excursion += cf->search_mode; + } + + unix_vlib_cli_output_cooked (cf, uf, (u8 *) "\nNo match...", 12); + vec_reset_length (cf->search_key); + vec_reset_length (cf->current_command); + cf->search_mode = 0; + cf->cursor = 0; + goto crlf; + } + else if (isprint (input)) /* skip any errant control codes */ + { + if (cf->cursor == vec_len (cf->current_command)) + { + /* Append to end */ + vec_add1 (cf->current_command, input); + cf->cursor++; + + /* Echo the character back to the client */ + unix_vlib_cli_output_raw (cf, uf, &input, 1); + } + else + { + /* Insert at cursor: resize +1 byte, move everything over */ + j = vec_len (cf->current_command) - cf->cursor; + vec_add1 (cf->current_command, (u8) 'A'); + memmove (cf->current_command + cf->cursor + 1, + cf->current_command + cf->cursor, j); + cf->current_command[cf->cursor] = input; + /* Redraw the line */ + j++; + unix_vlib_cli_output_raw (cf, uf, + cf->current_command + cf->cursor, j); + /* Put terminal cursor back */ + while (--j) + unix_vlib_cli_output_raw (cf, uf, (u8 *) "\b", 1); + cf->cursor++; + } + } else - { - /* no-op - not printable or otherwise not actionable */ - } + { + /* no-op - not printable or otherwise not actionable */ + } found: @@ -1860,16 +1880,16 @@ static int unix_cli_line_process_one(unix_cli_main_t * cm, case UNIX_CLI_PARSE_ACTION_TELNETIAC: break; } - return 1; + return 1; } /** \brief Process input bytes on a stream to provide line editing and * command history in the CLI. */ -static int unix_cli_line_edit (unix_cli_main_t * cm, - unix_main_t * um, - unix_cli_file_t * cf) +static int +unix_cli_line_edit (unix_cli_main_t * cm, + unix_main_t * um, unix_cli_file_t * cf) { - unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); + unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); int i; for (i = 0; i < vec_len (cf->input_vector); i++) @@ -1879,78 +1899,82 @@ static int unix_cli_line_edit (unix_cli_main_t * cm, unix_cli_parse_actions_t *a; /* If we're in the pager mode, search the pager actions */ - a = vec_len (cf->pager_index) ? unix_cli_parse_pager : unix_cli_parse_strings; + a = + vec_len (cf->pager_index) ? unix_cli_parse_pager : + unix_cli_parse_strings; /* See if the input buffer is some sort of control code */ - action = unix_cli_match_action(a, &cf->input_vector[i], - vec_len (cf->input_vector) - i, &matched); + action = unix_cli_match_action (a, &cf->input_vector[i], + vec_len (cf->input_vector) - i, + &matched); switch (action) - { - case UNIX_CLI_PARSE_ACTION_PARTIALMATCH: - if (i) - { - /* There was a partial match which means we need more bytes - * than the input buffer currently has. - * Since the bytes before here have been processed, shift - * the remaining contents to the start of the input buffer. - */ - vec_delete (cf->input_vector, i, 0); - } - return 1; /* wait for more */ - - case UNIX_CLI_PARSE_ACTION_TELNETIAC: - /* process telnet options */ - matched = unix_cli_process_telnet(um, cf, uf, - cf->input_vector + i, vec_len(cf->input_vector) - i); - if (matched < 0) - { - if (i) - { - /* There was a partial match which means we need more bytes - * than the input buffer currently has. - * Since the bytes before here have been processed, shift - * the remaining contents to the start of the input buffer. - */ - vec_delete (cf->input_vector, i, 0); - } - return 1; /* wait for more */ - } - break; - - default: - /* process the action */ - if (!unix_cli_line_process_one(cm, um, cf, uf, - cf->input_vector[i], action)) - { - /* CRLF found. Consume the bytes from the input_vector */ - vec_delete (cf->input_vector, i + matched, 0); - /* And tell our caller to execute cf->input_command */ - return 0; - } - } + { + case UNIX_CLI_PARSE_ACTION_PARTIALMATCH: + if (i) + { + /* There was a partial match which means we need more bytes + * than the input buffer currently has. + * Since the bytes before here have been processed, shift + * the remaining contents to the start of the input buffer. + */ + vec_delete (cf->input_vector, i, 0); + } + return 1; /* wait for more */ + + case UNIX_CLI_PARSE_ACTION_TELNETIAC: + /* process telnet options */ + matched = unix_cli_process_telnet (um, cf, uf, + cf->input_vector + i, + vec_len (cf->input_vector) - i); + if (matched < 0) + { + if (i) + { + /* There was a partial match which means we need more bytes + * than the input buffer currently has. + * Since the bytes before here have been processed, shift + * the remaining contents to the start of the input buffer. + */ + vec_delete (cf->input_vector, i, 0); + } + return 1; /* wait for more */ + } + break; + + default: + /* process the action */ + if (!unix_cli_line_process_one (cm, um, cf, uf, + cf->input_vector[i], action)) + { + /* CRLF found. Consume the bytes from the input_vector */ + vec_delete (cf->input_vector, i + matched, 0); + /* And tell our caller to execute cf->input_command */ + return 0; + } + } i += matched; } - vec_reset_length(cf->input_vector); + vec_reset_length (cf->input_vector); return 1; } /** \brief Process input to a CLI session. */ -static void unix_cli_process_input (unix_cli_main_t * cm, - uword cli_file_index) +static void +unix_cli_process_input (unix_cli_main_t * cm, uword cli_file_index) { - unix_main_t * um = &unix_main; - unix_file_t * uf; - unix_cli_file_t * cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); + unix_main_t *um = &unix_main; + unix_file_t *uf; + unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); unformat_input_t input; int vlib_parse_eval (u8 *); more: /* Try vlibplex first. Someday... */ if (0 && vlib_parse_eval (cf->input_vector) == 0) - goto done; + goto done; if (cf->line_mode) { @@ -1961,23 +1985,21 @@ more: { /* Line edit, echo, etc. */ if (unix_cli_line_edit (cm, um, cf)) - /* want more input */ - return; + /* want more input */ + return; } if (um->log_fd) { - static u8 * lv; + static u8 *lv; vec_reset_length (lv); - lv = format (lv, "%U[%d]: %v", - format_timeval, - 0 /* current bat-time */, - 0 /* current bat-format */, - cli_file_index, - cf->input_vector); + lv = format (lv, "%U[%d]: %v", + format_timeval, 0 /* current bat-time */ , + 0 /* current bat-format */ , + cli_file_index, cf->input_vector); { - int rv __attribute__((unused)) = - write (um->log_fd, lv, vec_len(lv)); + int rv __attribute__ ((unused)) = + write (um->log_fd, lv, vec_len (lv)); } } @@ -1988,10 +2010,11 @@ more: (void) unformat (&input, ""); cm->current_input_file_index = cli_file_index; - cf->pager_start = 0; /* start a new pager session */ + cf->pager_start = 0; /* start a new pager session */ if (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) - vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output, cli_file_index); + vlib_cli_input (um->vlib_main, &input, unix_vlib_cli_output, + cli_file_index); /* Zero buffer since otherwise unformat_free will call vec_free on it. */ input.buffer = 0; @@ -2016,7 +2039,8 @@ done: cf->no_pager = um->cli_no_pager; } - if (vec_len (cf->pager_index) == 0 || vec_len (cf->pager_index) < cf->height) + if (vec_len (cf->pager_index) == 0 + || vec_len (cf->pager_index) < cf->height) { /* There was no need for the pager */ unix_cli_pager_reset (cf); @@ -2027,19 +2051,20 @@ done: else { /* Display the pager prompt */ - unix_cli_pager_prompt(cf, uf); + unix_cli_pager_prompt (cf, uf); } - /* Any residual data in the input vector? */ - if (vec_len (cf->input_vector)) - goto more; + /* Any residual data in the input vector? */ + if (vec_len (cf->input_vector)) + goto more; } -static void unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index) +static void +unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index) { - unix_main_t * um = &unix_main; - unix_cli_file_t * cf; - unix_file_t * uf; + unix_main_t *um = &unix_main; + unix_cli_file_t *cf; + unix_file_t *uf; int i; cf = pool_elt_at_index (cm->cli_file_pool, cli_file_index); @@ -2053,7 +2078,7 @@ static void unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index) vec_free (cf->search_key); for (i = 0; i < vec_len (cf->command_history); i++) - vec_free (cf->command_history[i]); + vec_free (cf->command_history[i]); vec_free (cf->command_history); @@ -2065,11 +2090,10 @@ static void unix_cli_kill (unix_cli_main_t * cm, uword cli_file_index) static uword unix_cli_process (vlib_main_t * vm, - vlib_node_runtime_t * rt, - vlib_frame_t * f) + vlib_node_runtime_t * rt, vlib_frame_t * f) { - unix_cli_main_t * cm = &unix_cli_main; - uword i, * data = 0; + unix_cli_main_t *cm = &unix_cli_main; + uword i, *data = 0; while (1) { @@ -2095,7 +2119,7 @@ unix_cli_process (vlib_main_t * vm, _vec_len (data) = 0; } - done: +done: vec_free (data); vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED); @@ -2106,10 +2130,11 @@ unix_cli_process (vlib_main_t * vm, return 0; } -static clib_error_t * unix_cli_write_ready (unix_file_t * uf) +static clib_error_t * +unix_cli_write_ready (unix_file_t * uf) { - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; int n; cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data); @@ -2127,18 +2152,20 @@ static clib_error_t * unix_cli_write_ready (unix_file_t * uf) return /* no error */ 0; } -static clib_error_t * unix_cli_read_ready (unix_file_t * uf) +static clib_error_t * +unix_cli_read_ready (unix_file_t * uf) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; uword l; int n, n_read, n_try; cf = pool_elt_at_index (cm->cli_file_pool, uf->private_data); n = n_try = 4096; - while (n == n_try) { + while (n == n_try) + { l = vec_len (cf->input_vector); vec_resize (cf->input_vector, l + n_try); @@ -2146,13 +2173,13 @@ static clib_error_t * unix_cli_read_ready (unix_file_t * uf) /* Error? */ if (n < 0 && errno != EAGAIN) - return clib_error_return_unix (0, "read"); - + return clib_error_return_unix (0, "read"); + n_read = n < 0 ? 0 : n; _vec_len (cf->input_vector) = l + n_read; - } + } - if (! (n < 0)) + if (!(n < 0)) vlib_process_signal_event (um->vlib_main, cf->process_node_index, (n_read == 0 @@ -2168,13 +2195,14 @@ static clib_error_t * unix_cli_read_ready (unix_file_t * uf) * @param fd The file descriptor for the session I/O. * @return The session ID. */ -static u32 unix_cli_file_add (unix_cli_main_t * cm, char * name, int fd) +static u32 +unix_cli_file_add (unix_cli_main_t * cm, char *name, int fd) { - unix_main_t * um = &unix_main; - unix_cli_file_t * cf; - unix_file_t template = {0}; - vlib_main_t * vm = um->vlib_main; - vlib_node_t * n; + unix_main_t *um = &unix_main; + unix_cli_file_t *cf; + unix_file_t template = { 0 }; + vlib_main_t *vm = um->vlib_main; + vlib_node_t *n; name = (char *) format (0, "unix-cli-%s", name); @@ -2221,7 +2249,7 @@ static u32 unix_cli_file_add (unix_cli_main_t * cm, char * name, int fd) vlib_start_process (vm, n->runtime_index); - vlib_process_t * p = vlib_get_process_from_node(vm, n); + vlib_process_t *p = vlib_get_process_from_node (vm, n); p->output_function = unix_vlib_cli_output; p->output_function_arg = cf - cm->cli_file_pool; @@ -2229,15 +2257,16 @@ static u32 unix_cli_file_add (unix_cli_main_t * cm, char * name, int fd) } /** Telnet listening socket has a new connection. */ -static clib_error_t * unix_cli_listen_read_ready (unix_file_t * uf) +static clib_error_t * +unix_cli_listen_read_ready (unix_file_t * uf) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; - clib_socket_t * s = &um->cli_listen_socket; + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; + clib_socket_t *s = &um->cli_listen_socket; clib_socket_t client; - char * client_name; - clib_error_t * error; - unix_cli_file_t * cf; + char *client_name; + clib_error_t *error; + unix_cli_file_t *cf; u32 cf_index; error = clib_socket_accept (s, &client); @@ -2262,16 +2291,16 @@ static clib_error_t * unix_cli_listen_read_ready (unix_file_t * uf) * Technically these should be negotiated, but this works. */ u8 charmode_option[] = { - IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */ - IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */ - IAC, WILL, TELOPT_SGA, /* server willl supress GA */ - IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */ - IAC, WILL, TELOPT_ECHO, /* server will do echo */ - IAC, DONT, TELOPT_ECHO, /* client should not echo */ - IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */ - IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */ - IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */ - IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */ + IAC, WONT, TELOPT_LINEMODE, /* server will do char-by-char */ + IAC, DONT, TELOPT_LINEMODE, /* client should do char-by-char */ + IAC, WILL, TELOPT_SGA, /* server willl supress GA */ + IAC, DO, TELOPT_SGA, /* client should supress Go Ahead */ + IAC, WILL, TELOPT_ECHO, /* server will do echo */ + IAC, DONT, TELOPT_ECHO, /* client should not echo */ + IAC, DO, TELOPT_TTYPE, /* client should tell us its term type */ + IAC, SB, TELOPT_TTYPE, 1, IAC, SE, /* now tell me ttype */ + IAC, DO, TELOPT_NAWS, /* client should tell us its window sz */ + IAC, SB, TELOPT_NAWS, 1, IAC, SE, /* now tell me window size */ }; /* Enable history on this CLI */ @@ -2291,7 +2320,7 @@ static clib_error_t * unix_cli_listen_read_ready (unix_file_t * uf) /* Send the telnet options */ unix_vlib_cli_output_raw (cf, uf, charmode_option, - ARRAY_LEN(charmode_option)); + ARRAY_LEN (charmode_option)); /* In case the client doesn't negotiate terminal type, use * a timer to kick off the initial prompt. */ @@ -2307,16 +2336,16 @@ static clib_error_t * unix_cli_listen_read_ready (unix_file_t * uf) static void unix_cli_resize_interrupt (int signum) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf = pool_elt_at_index (cm->cli_file_pool, - cm->stdin_cli_file_index); - unix_file_t * uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf = pool_elt_at_index (cm->cli_file_pool, + cm->stdin_cli_file_index); + unix_file_t *uf = pool_elt_at_index (um->file_pool, cf->unix_file_index); struct winsize ws; - (void)signum; + (void) signum; /* Terminal resized, fetch the new size */ - ioctl(UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws); + ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws); cf->width = ws.ws_col; cf->height = ws.ws_row; @@ -2331,16 +2360,16 @@ unix_cli_resize_interrupt (int signum) static clib_error_t * unix_cli_config (vlib_main_t * vm, unformat_input_t * input) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; int flags; - clib_error_t * error = 0; - unix_cli_file_t * cf; + clib_error_t *error = 0; + unix_cli_file_t *cf; u32 cf_index; struct termios tio; struct sigaction sa; struct winsize ws; - u8 * term; + u8 *term; /* We depend on unix flags being set. */ if ((error = vlib_call_config_function (vm, unix_config))) @@ -2350,7 +2379,7 @@ unix_cli_config (vlib_main_t * vm, unformat_input_t * input) { /* Set stdin to be non-blocking. */ if ((flags = fcntl (UNIX_CLI_STDIN_FD, F_GETFL, 0)) < 0) - flags = 0; + flags = 0; fcntl (UNIX_CLI_STDIN_FD, F_SETFL, flags | O_NONBLOCK); cf_index = unix_cli_file_add (cm, "stdin", UNIX_CLI_STDIN_FD); @@ -2359,85 +2388,87 @@ unix_cli_config (vlib_main_t * vm, unformat_input_t * input) /* If stdin is a tty and we are using chacracter mode, enable * history on the CLI and set the tty line discipline accordingly. */ - if (isatty(UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0) - { - /* Capture terminal resize events */ - sa.sa_handler = unix_cli_resize_interrupt; - sa.sa_flags = 0; - if (sigaction (SIGWINCH, &sa, 0) < 0) - clib_panic ("sigaction"); - - /* Retrieve the current terminal size */ - ioctl(UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws); - cf->width = ws.ws_col; - cf->height = ws.ws_row; - - if (cf->width == 0 || cf->height == 0) - /* We have a tty, but no size. Stick to line mode. */ - goto notty; - - /* Setup the history */ - cf->history_limit = um->cli_history_limit; - cf->has_history = cf->history_limit != 0; - - /* Setup the pager */ - cf->no_pager = um->cli_no_pager; - - /* We're going to be in char by char mode */ - cf->line_mode = 0; - - /* Save the original tty state so we can restore it later */ - tcgetattr(UNIX_CLI_STDIN_FD, &um->tio_stdin); - um->tio_isset = 1; - - /* Tweak the tty settings */ - tio = um->tio_stdin; - /* echo off, canonical mode off, ext'd input processing off */ - tio.c_lflag &= ~(ECHO | ICANON | IEXTEN); - tio.c_cc[VMIN] = 1; /* 1 byte at a time */ - tio.c_cc[VTIME] = 0; /* no timer */ - tcsetattr(UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio); - - /* See if we can do ANSI/VT100 output */ - term = (u8 *)getenv("TERM"); - if (term != NULL) - cf->ansi_capable = unix_cli_terminal_type(term, - strlen((char *)term)); - } + if (isatty (UNIX_CLI_STDIN_FD) && um->cli_line_mode == 0) + { + /* Capture terminal resize events */ + sa.sa_handler = unix_cli_resize_interrupt; + sa.sa_flags = 0; + if (sigaction (SIGWINCH, &sa, 0) < 0) + clib_panic ("sigaction"); + + /* Retrieve the current terminal size */ + ioctl (UNIX_CLI_STDIN_FD, TIOCGWINSZ, &ws); + cf->width = ws.ws_col; + cf->height = ws.ws_row; + + if (cf->width == 0 || cf->height == 0) + /* We have a tty, but no size. Stick to line mode. */ + goto notty; + + /* Setup the history */ + cf->history_limit = um->cli_history_limit; + cf->has_history = cf->history_limit != 0; + + /* Setup the pager */ + cf->no_pager = um->cli_no_pager; + + /* We're going to be in char by char mode */ + cf->line_mode = 0; + + /* Save the original tty state so we can restore it later */ + tcgetattr (UNIX_CLI_STDIN_FD, &um->tio_stdin); + um->tio_isset = 1; + + /* Tweak the tty settings */ + tio = um->tio_stdin; + /* echo off, canonical mode off, ext'd input processing off */ + tio.c_lflag &= ~(ECHO | ICANON | IEXTEN); + tio.c_cc[VMIN] = 1; /* 1 byte at a time */ + tio.c_cc[VTIME] = 0; /* no timer */ + tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &tio); + + /* See if we can do ANSI/VT100 output */ + term = (u8 *) getenv ("TERM"); + if (term != NULL) + cf->ansi_capable = unix_cli_terminal_type (term, + strlen ((char *) + term)); + } else - { - notty: - /* No tty, so make sure these things are off */ - cf->no_pager = 1; - cf->history_limit = 0; - cf->has_history = 0; - cf->line_mode = 1; - } + { + notty: + /* No tty, so make sure these things are off */ + cf->no_pager = 1; + cf->history_limit = 0; + cf->has_history = 0; + cf->line_mode = 1; + } /* Send banner and initial prompt */ - unix_cli_file_welcome(cm, cf); + unix_cli_file_welcome (cm, cf); } /* If we have socket config, LISTEN, otherwise, don't */ - clib_socket_t * s = &um->cli_listen_socket; - if(s->config && s->config[0] != 0) { - /* CLI listen. */ - unix_file_t template = {0}; + clib_socket_t *s = &um->cli_listen_socket; + if (s->config && s->config[0] != 0) + { + /* CLI listen. */ + unix_file_t template = { 0 }; - s->flags = SOCKET_IS_SERVER; /* listen, don't connect */ - error = clib_socket_init (s); + s->flags = SOCKET_IS_SERVER; /* listen, don't connect */ + error = clib_socket_init (s); - if (error) - return error; + if (error) + return error; - template.read_function = unix_cli_listen_read_ready; - template.file_descriptor = s->fd; + template.read_function = unix_cli_listen_read_ready; + template.file_descriptor = s->fd; - unix_file_add (um, &template); - } + unix_file_add (um, &template); + } /* Set CLI prompt. */ - if (! cm->cli_prompt) + if (!cm->cli_prompt) cm->cli_prompt = format (0, "VLIB: "); return 0; @@ -2451,11 +2482,11 @@ VLIB_CONFIG_FUNCTION (unix_cli_config, "unix-cli"); static clib_error_t * unix_cli_exit (vlib_main_t * vm) { - unix_main_t * um = &unix_main; + unix_main_t *um = &unix_main; /* If stdin is a tty and we saved the tty state, reset the tty state */ - if (isatty(UNIX_CLI_STDIN_FD) && um->tio_isset) - tcsetattr(UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin); + if (isatty (UNIX_CLI_STDIN_FD) && um->tio_isset) + tcsetattr (UNIX_CLI_STDIN_FD, TCSAFLUSH, &um->tio_stdin); return 0; } @@ -2467,10 +2498,11 @@ VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_cli_exit); * @note This setting is global; it impacts all current * and future CLI sessions. */ -void vlib_unix_cli_set_prompt (char * prompt) +void +vlib_unix_cli_set_prompt (char *prompt) { - char * fmt = (prompt[strlen(prompt)-1] == ' ') ? "%s" : "%s "; - unix_cli_main_t * cm = &unix_cli_main; + char *fmt = (prompt[strlen (prompt) - 1] == ' ') ? "%s" : "%s "; + unix_cli_main_t *cm = &unix_cli_main; if (cm->cli_prompt) vec_free (cm->cli_prompt); cm->cli_prompt = format (0, fmt, prompt); @@ -2482,10 +2514,9 @@ void vlib_unix_cli_set_prompt (char * prompt) */ static clib_error_t * unix_cli_quit (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - unix_cli_main_t * cm = &unix_cli_main; + unix_cli_main_t *cm = &unix_cli_main; vlib_process_signal_event (vm, vlib_current_process (vm), @@ -2494,28 +2525,29 @@ unix_cli_quit (vlib_main_t * vm, return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (unix_cli_quit_command, static) = { .path = "quit", .short_help = "Exit CLI", .function = unix_cli_quit, }; +/* *INDENT-ON* */ /** CLI command to execute a VPP command script. */ static clib_error_t * unix_cli_exec (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - char * file_name; + char *file_name; int fd; unformat_input_t sub_input; - clib_error_t * error; + clib_error_t *error; file_name = 0; fd = -1; error = 0; - if (! unformat (input, "%s", &file_name)) + if (!unformat (input, "%s", &file_name)) { error = clib_error_return (0, "expecting file name, got `%U'", format_unformat_error, input); @@ -2538,8 +2570,8 @@ unix_cli_exec (vlib_main_t * vm, error = clib_error_return_unix (0, "failed to stat `%s'", file_name); goto done; } - - if (! (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) + + if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) { error = clib_error_return (0, "not a regular file `%s'", file_name); goto done; @@ -2551,7 +2583,7 @@ unix_cli_exec (vlib_main_t * vm, vlib_cli_input (vm, &sub_input, 0, 0); unformat_free (&sub_input); - done: +done: if (fd > 0) close (fd); vec_free (file_name); @@ -2559,45 +2591,51 @@ unix_cli_exec (vlib_main_t * vm, return error; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_exec, static) = { .path = "exec", .short_help = "Execute commands from file", .function = unix_cli_exec, .is_mp_safe = 1, }; +/* *INDENT-ON* */ /** CLI command to show various unix error statistics. */ static clib_error_t * unix_show_errors (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - unix_main_t * um = &unix_main; - clib_error_t * error = 0; + unix_main_t *um = &unix_main; + clib_error_t *error = 0; int i, n_errors_to_show; - unix_error_history_t * unix_errors = 0; + unix_error_history_t *unix_errors = 0; n_errors_to_show = 1 << 30; if (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { - if (! unformat (input, "%d", &n_errors_to_show)) + if (!unformat (input, "%d", &n_errors_to_show)) { - error = clib_error_return (0, "expecting integer number of errors to show, got `%U'", - format_unformat_error, input); + error = + clib_error_return (0, + "expecting integer number of errors to show, got `%U'", + format_unformat_error, input); goto done; } } - n_errors_to_show = clib_min (ARRAY_LEN (um->error_history), n_errors_to_show); + n_errors_to_show = + clib_min (ARRAY_LEN (um->error_history), n_errors_to_show); - i = um->error_history_index > 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1; + i = + um->error_history_index > + 0 ? um->error_history_index - 1 : ARRAY_LEN (um->error_history) - 1; while (n_errors_to_show > 0) { - unix_error_history_t * eh = um->error_history + i; + unix_error_history_t *eh = um->error_history + i; - if (! eh->error) + if (!eh->error) break; vec_add1 (unix_errors, eh[0]); @@ -2615,7 +2653,7 @@ unix_show_errors (vlib_main_t * vm, vlib_cli_output (vm, "%Ld total errors seen", um->n_total_errors); for (i = vec_len (unix_errors) - 1; i >= 0; i--) { - unix_error_history_t * eh = vec_elt_at_index (unix_errors, i); + unix_error_history_t *eh = vec_elt_at_index (unix_errors, i); vlib_cli_output (vm, "%U: %U", format_time_interval, "h:m:s:u", eh->time, format_clib_error, eh->error); @@ -2624,34 +2662,35 @@ unix_show_errors (vlib_main_t * vm, format_time_interval, "h:m:s:u", vlib_time_now (vm)); } - done: +done: vec_free (unix_errors); return error; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_unix_show_errors, static) = { .path = "show unix-errors", .short_help = "Show Unix system call error history", .function = unix_show_errors, }; +/* *INDENT-ON* */ /** CLI command to show session command history. */ static clib_error_t * unix_cli_show_history (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; int i, j; cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index); if (cf->has_history && cf->history_limit) { - i = 1 + cf->command_number - vec_len(cf->command_history); + i = 1 + cf->command_number - vec_len (cf->command_history); for (j = 0; j < vec_len (cf->command_history); j++) - vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]); + vlib_cli_output (vm, "%d %v\n", i + j, cf->command_history[j]); } else { @@ -2661,153 +2700,166 @@ unix_cli_show_history (vlib_main_t * vm, return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_unix_cli_show_history, static) = { .path = "history", .short_help = "Show current session command history", .function = unix_cli_show_history, }; +/* *INDENT-ON* */ /** CLI command to show terminal status. */ static clib_error_t * unix_cli_show_terminal (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; - vlib_node_t * n; + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; + vlib_node_t *n; cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index); n = vlib_get_node (vm, cf->process_node_index); vlib_cli_output (vm, "Terminal name: %v\n", n->name); - vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ? - "line-by-line" : - "char-by-char"); + vlib_cli_output (vm, "Terminal mode: %s\n", cf->line_mode ? + "line-by-line" : "char-by-char"); vlib_cli_output (vm, "Terminal width: %d\n", cf->width); vlib_cli_output (vm, "Terminal height: %d\n", cf->height); - vlib_cli_output (vm, "ANSI capable: %s\n", cf->ansi_capable ? "yes" : "no"); + vlib_cli_output (vm, "ANSI capable: %s\n", + cf->ansi_capable ? "yes" : "no"); vlib_cli_output (vm, "History enabled: %s%s\n", - cf->has_history ? "yes" : "no", - !cf->has_history || cf->history_limit ? "" : " (disabled by history limit)"); + cf->has_history ? "yes" : "no", !cf->has_history + || cf->history_limit ? "" : + " (disabled by history limit)"); if (cf->has_history) vlib_cli_output (vm, "History limit: %d\n", cf->history_limit); vlib_cli_output (vm, "Pager enabled: %s%s%s\n", - cf->no_pager ? "no" : "yes", - cf->no_pager || cf->height ? "" : " (disabled by terminal height)", - cf->no_pager || um->cli_pager_buffer_limit ? "" : " (disabled by buffer limit)"); + cf->no_pager ? "no" : "yes", + cf->no_pager + || cf->height ? "" : " (disabled by terminal height)", + cf->no_pager + || um->cli_pager_buffer_limit ? "" : + " (disabled by buffer limit)"); if (!cf->no_pager) vlib_cli_output (vm, "Pager limit: %d\n", um->cli_pager_buffer_limit); - vlib_cli_output (vm, "CRLF mode: %s\n", cf->crlf_mode ? "CR+LF" : "LF"); + vlib_cli_output (vm, "CRLF mode: %s\n", + cf->crlf_mode ? "CR+LF" : "LF"); return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_unix_cli_show_terminal, static) = { .path = "show terminal", .short_help = "Show current session terminal settings", .function = unix_cli_show_terminal, }; +/* *INDENT-ON* */ /** CLI command to set terminal pager settings. */ static clib_error_t * unix_cli_set_terminal_pager (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, + vlib_cli_command_t * cmd) { - unix_main_t * um = &unix_main; - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; - unformat_input_t _line_input, * line_input = &_line_input; + unix_main_t *um = &unix_main; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; + unformat_input_t _line_input, *line_input = &_line_input; - if (! unformat_user (input, unformat_line_input, line_input)) + if (!unformat_user (input, unformat_line_input, line_input)) return 0; cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index); while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { - if (unformat (line_input, "on")) - cf->no_pager = 0; - else if (unformat (line_input, "off")) - cf->no_pager = 1; - else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit)) - vlib_cli_output (vm, "Pager limit set to %u lines; note, this is global.\n", - um->cli_pager_buffer_limit); - else - return clib_error_return (0, "unknown parameter: `%U`", - format_unformat_error, line_input); - } + if (unformat (line_input, "on")) + cf->no_pager = 0; + else if (unformat (line_input, "off")) + cf->no_pager = 1; + else if (unformat (line_input, "limit %u", &um->cli_pager_buffer_limit)) + vlib_cli_output (vm, + "Pager limit set to %u lines; note, this is global.\n", + um->cli_pager_buffer_limit); + else + return clib_error_return (0, "unknown parameter: `%U`", + format_unformat_error, line_input); + } - unformat_free(line_input); + unformat_free (line_input); return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_pager, static) = { .path = "set terminal pager", .short_help = "set terminal pager [on|off] [limit <lines>]", .function = unix_cli_set_terminal_pager, }; +/* *INDENT-ON* */ /** CLI command to set terminal history settings. */ static clib_error_t * unix_cli_set_terminal_history (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, + vlib_cli_command_t * cmd) { - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; - unformat_input_t _line_input, * line_input = &_line_input; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; + unformat_input_t _line_input, *line_input = &_line_input; u32 limit; - if (! unformat_user (input, unformat_line_input, line_input)) + if (!unformat_user (input, unformat_line_input, line_input)) return 0; cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index); while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { - if (unformat (line_input, "on")) - cf->has_history = 1; - else if (unformat (line_input, "off")) - cf->has_history = 0; - else if (unformat (line_input, "limit %u", &cf->history_limit)) - ; - else - return clib_error_return (0, "unknown parameter: `%U`", - format_unformat_error, line_input); - - /* If we reduced history size, or turned it off, purge the history */ - limit = cf->has_history ? cf->history_limit : 0; - - while (cf->command_history && vec_len(cf->command_history) >= limit) - { - vec_free (cf->command_history[0]); - vec_delete (cf->command_history, 1, 0); - } - } + if (unformat (line_input, "on")) + cf->has_history = 1; + else if (unformat (line_input, "off")) + cf->has_history = 0; + else if (unformat (line_input, "limit %u", &cf->history_limit)) + ; + else + return clib_error_return (0, "unknown parameter: `%U`", + format_unformat_error, line_input); + + /* If we reduced history size, or turned it off, purge the history */ + limit = cf->has_history ? cf->history_limit : 0; - unformat_free(line_input); + while (cf->command_history && vec_len (cf->command_history) >= limit) + { + vec_free (cf->command_history[0]); + vec_delete (cf->command_history, 1, 0); + } + } + + unformat_free (line_input); return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_history, static) = { .path = "set terminal history", .short_help = "set terminal history [on|off] [limit <lines>]", .function = unix_cli_set_terminal_history, }; +/* *INDENT-ON* */ /** CLI command to set terminal ANSI settings. */ static clib_error_t * unix_cli_set_terminal_ansi (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, + vlib_cli_command_t * cmd) { - unix_cli_main_t * cm = &unix_cli_main; - unix_cli_file_t * cf; + unix_cli_main_t *cm = &unix_cli_main; + unix_cli_file_t *cf; cf = pool_elt_at_index (cm->cli_file_pool, cm->current_input_file_index); @@ -2817,16 +2869,18 @@ unix_cli_set_terminal_ansi (vlib_main_t * vm, cf->ansi_capable = 0; else return clib_error_return (0, "unknown parameter: `%U`", - format_unformat_error, input); + format_unformat_error, input); return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (cli_unix_cli_set_terminal_ansi, static) = { .path = "set terminal ansi", .short_help = "set terminal ansi [on|off]", .function = unix_cli_set_terminal_ansi, }; +/* *INDENT-ON* */ static clib_error_t * unix_cli_init (vlib_main_t * vm) @@ -2835,3 +2889,11 @@ unix_cli_init (vlib_main_t * vm) } VLIB_INIT_FUNCTION (unix_cli_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/input.c b/vlib/vlib/unix/input.c index 543e2c000a7..b4f2ba537a4 100644 --- a/vlib/vlib/unix/input.c +++ b/vlib/vlib/unix/input.c @@ -48,9 +48,10 @@ #include <sys/epoll.h> -typedef struct { +typedef struct +{ int epoll_fd; - struct epoll_event * epoll_events; + struct epoll_event *epoll_events; /* Statistics. */ u64 epoll_files_ready; @@ -60,11 +61,10 @@ typedef struct { static linux_epoll_main_t linux_epoll_main; static void -linux_epoll_file_update (unix_file_t * f, - unix_file_update_type_t update_type) +linux_epoll_file_update (unix_file_t * f, unix_file_update_type_t update_type) { - unix_main_t * um = &unix_main; - linux_epoll_main_t * em = &linux_epoll_main; + unix_main_t *um = &unix_main; + linux_epoll_main_t *em = &linux_epoll_main; struct epoll_event e; memset (&e, 0, sizeof (e)); @@ -72,8 +72,8 @@ linux_epoll_file_update (unix_file_t * f, e.events = EPOLLIN; if (f->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE) e.events |= EPOLLOUT; - if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED) - e.events |= EPOLLET; + if (f->flags & UNIX_FILE_EVENT_EDGE_TRIGGERED) + e.events |= EPOLLET; e.data.u32 = f - um->file_pool; if (epoll_ctl (em->epoll_fd, @@ -81,24 +81,21 @@ linux_epoll_file_update (unix_file_t * f, ? EPOLL_CTL_ADD : (update_type == UNIX_FILE_UPDATE_MODIFY ? EPOLL_CTL_MOD - : EPOLL_CTL_DEL)), - f->file_descriptor, - &e) < 0) + : EPOLL_CTL_DEL)), f->file_descriptor, &e) < 0) clib_warning ("epoll_ctl"); } static uword linux_epoll_input (vlib_main_t * vm, - vlib_node_runtime_t * node, - vlib_frame_t * frame) + vlib_node_runtime_t * node, vlib_frame_t * frame) { - unix_main_t * um = &unix_main; - linux_epoll_main_t * em = &linux_epoll_main; - struct epoll_event * e; + unix_main_t *um = &unix_main; + linux_epoll_main_t *em = &linux_epoll_main; + struct epoll_event *e; int n_fds_ready; { - vlib_node_main_t * nm = &vm->node_main; + vlib_node_main_t *nm = &vm->node_main; u64 t = nm->cpu_time_next_process_ready; f64 timeout; int timeout_ms, max_timeout_ms = 10; @@ -114,9 +111,9 @@ linux_epoll_input (vlib_main_t * vm, timeout = (((i64) t - (i64) clib_cpu_time_now ()) * vm->clib_time.seconds_per_clock) - /* subtract off some slop time */ - 50e-6; + /* subtract off some slop time */ - 50e-6; timeout_ms = timeout * 1e3; - + /* Must be between 1 and 10 ms. */ timeout_ms = clib_max (1, timeout_ms); timeout_ms = clib_min (max_timeout_ms, timeout_ms); @@ -127,9 +124,9 @@ linux_epoll_input (vlib_main_t * vm, if (nm->input_node_counts_by_state[VLIB_NODE_STATE_POLLING] > 0) timeout_ms = 0; - /* - * When busy: don't wait & only epoll for input - * every 1024 times through main loop. + /* + * When busy: don't wait & only epoll for input + * every 1024 times through main loop. */ if (vector_rate > 1 || vm->api_queue_nonempty) { @@ -144,26 +141,24 @@ linux_epoll_input (vlib_main_t * vm, { static sigset_t unblock_all_signals; n_fds_ready = epoll_pwait (em->epoll_fd, - em->epoll_events, - vec_len (em->epoll_events), - timeout_ms, - &unblock_all_signals); - + em->epoll_events, + vec_len (em->epoll_events), + timeout_ms, &unblock_all_signals); + /* This kludge is necessary to run over absurdly old kernels */ if (n_fds_ready < 0 && errno == ENOSYS) - { - n_fds_ready = epoll_wait (em->epoll_fd, - em->epoll_events, - vec_len (em->epoll_events), - timeout_ms); - } + { + n_fds_ready = epoll_wait (em->epoll_fd, + em->epoll_events, + vec_len (em->epoll_events), timeout_ms); + } } } if (n_fds_ready < 0) { if (unix_error_is_fatal (errno)) - vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait")); + vlib_panic_with_error (vm, clib_error_return_unix (0, "epoll_wait")); /* non fatal error (e.g. EINTR). */ return 0; @@ -175,11 +170,11 @@ linux_epoll_input (vlib_main_t * vm, for (e = em->epoll_events; e < em->epoll_events + n_fds_ready; e++) { u32 i = e->data.u32; - unix_file_t * f = pool_elt_at_index (um->file_pool, i); - clib_error_t * errors[4]; + unix_file_t *f = pool_elt_at_index (um->file_pool, i); + clib_error_t *errors[4]; int n_errors = 0; - if (PREDICT_TRUE (! (e->events & EPOLLERR))) + if (PREDICT_TRUE (!(e->events & EPOLLERR))) { if (e->events & EPOLLIN) { @@ -194,13 +189,13 @@ linux_epoll_input (vlib_main_t * vm, } else { - if (f->error_function) - { - errors[n_errors] = f->error_function (f); - n_errors += errors[n_errors] != 0; - } + if (f->error_function) + { + errors[n_errors] = f->error_function (f); + n_errors += errors[n_errors] != 0; + } else - close(f->file_descriptor); + close (f->file_descriptor); } ASSERT (n_errors < ARRAY_LEN (errors)); @@ -213,18 +208,20 @@ linux_epoll_input (vlib_main_t * vm, return 0; } +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (linux_epoll_input_node,static) = { .function = linux_epoll_input, .type = VLIB_NODE_TYPE_PRE_INPUT, .name = "unix-epoll-input", }; +/* *INDENT-ON* */ clib_error_t * linux_epoll_input_init (vlib_main_t * vm) { - linux_epoll_main_t * em = &linux_epoll_main; - unix_main_t * um = &unix_main; - + linux_epoll_main_t *em = &linux_epoll_main; + unix_main_t *um = &unix_main; + /* Allocate some events. */ vec_resize (em->epoll_events, VLIB_FRAME_SIZE); @@ -248,3 +245,11 @@ unix_input_init (vlib_main_t * vm) } VLIB_INIT_FUNCTION (unix_input_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/main.c b/vlib/vlib/unix/main.c index 9049644f5b3..b8753f4df93 100644 --- a/vlib/vlib/unix/main.c +++ b/vlib/vlib/unix/main.c @@ -59,21 +59,21 @@ unix_main_t unix_main; static clib_error_t * unix_main_init (vlib_main_t * vm) { - unix_main_t * um = &unix_main; + unix_main_t *um = &unix_main; um->vlib_main = vm; return vlib_call_init_function (vm, unix_input_init); } VLIB_INIT_FUNCTION (unix_main_init); -static void unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc) +static void +unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc) { uword fatal; - u8 * msg = 0; + u8 *msg = 0; msg = format (msg, "received signal %U, PC %U", - format_signal, signum, - format_ucontext_pc, uc); + format_signal, signum, format_ucontext_pc, uc); if (signum == SIGSEGV) msg = format (msg, ", faulting address %p", si->si_addr); @@ -82,13 +82,13 @@ static void unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc) { /* these (caught) signals cause the application to exit */ case SIGTERM: - if (unix_main.vlib_main->main_loop_exit_set) - { - syslog (LOG_ERR | LOG_DAEMON, "received SIGTERM, exiting..."); + if (unix_main.vlib_main->main_loop_exit_set) + { + syslog (LOG_ERR | LOG_DAEMON, "received SIGTERM, exiting..."); - clib_longjmp (&unix_main.vlib_main->main_loop_exit, - VLIB_MAIN_LOOP_EXIT_CLI); - } + clib_longjmp (&unix_main.vlib_main->main_loop_exit, + VLIB_MAIN_LOOP_EXIT_CLI); + } case SIGQUIT: case SIGINT: case SIGILL: @@ -133,21 +133,21 @@ setup_signal_handlers (unix_main_t * um) switch (i) { - /* these signals take the default action */ - case SIGABRT: + /* these signals take the default action */ + case SIGABRT: case SIGKILL: case SIGSTOP: - case SIGUSR1: - case SIGUSR2: + case SIGUSR1: + case SIGUSR2: continue; - /* ignore SIGPIPE, SIGCHLD */ + /* ignore SIGPIPE, SIGCHLD */ case SIGPIPE: case SIGCHLD: sa.sa_sigaction = (void *) SIG_IGN; break; - /* catch and handle all other signals */ + /* catch and handle all other signals */ default: break; } @@ -159,9 +159,10 @@ setup_signal_handlers (unix_main_t * um) return 0; } -static void unix_error_handler (void * arg, u8 * msg, int msg_len) +static void +unix_error_handler (void *arg, u8 * msg, int msg_len) { - unix_main_t * um = arg; + unix_main_t *um = arg; /* Echo to stderr when interactive. */ if (um->flags & UNIX_FLAG_INTERACTIVE) @@ -173,46 +174,46 @@ static void unix_error_handler (void * arg, u8 * msg, int msg_len) char save = msg[msg_len - 1]; /* Null Terminate. */ - msg[msg_len-1] = 0; + msg[msg_len - 1] = 0; syslog (LOG_ERR | LOG_DAEMON, "%s", msg); - msg[msg_len-1] = save; + msg[msg_len - 1] = save; } } -void vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error) +void +vlib_unix_error_report (vlib_main_t * vm, clib_error_t * error) { - unix_main_t * um = &unix_main; + unix_main_t *um = &unix_main; - if (um->flags & UNIX_FLAG_INTERACTIVE || error == 0) - return; + if (um->flags & UNIX_FLAG_INTERACTIVE || error == 0) + return; - { - char save; - u8 * msg; - u32 msg_len; + { + char save; + u8 *msg; + u32 msg_len; - msg = error->what; - msg_len = vec_len(msg); + msg = error->what; + msg_len = vec_len (msg); - /* Null Terminate. */ - save = msg[msg_len-1]; - msg[msg_len-1] = 0; + /* Null Terminate. */ + save = msg[msg_len - 1]; + msg[msg_len - 1] = 0; - syslog (LOG_ERR | LOG_DAEMON, "%s", msg); + syslog (LOG_ERR | LOG_DAEMON, "%s", msg); - msg[msg_len-1] = save; - } + msg[msg_len - 1] = save; + } } static uword startup_config_process (vlib_main_t * vm, - vlib_node_runtime_t * rt, - vlib_frame_t * f) + vlib_node_runtime_t * rt, vlib_frame_t * f) { - unix_main_t * um = &unix_main; - u8 * buf = 0; + unix_main_t *um = &unix_main; + u8 *buf = 0; uword l, n = 1; vlib_process_suspend (vm, 2.0); @@ -220,91 +221,95 @@ startup_config_process (vlib_main_t * vm, while (um->unix_config_complete == 0) vlib_process_suspend (vm, 0.1); - if (um->startup_config_filename) { - unformat_input_t sub_input; - int fd; - struct stat s; - char *fn = (char *)um->startup_config_filename; - - fd = open (fn, O_RDONLY); - if (fd < 0) { - clib_warning ("failed to open `%s'", fn); - return 0; - } + if (um->startup_config_filename) + { + unformat_input_t sub_input; + int fd; + struct stat s; + char *fn = (char *) um->startup_config_filename; - if (fstat (fd, &s) < 0) { - clib_warning ("failed to stat `%s'", fn); - bail: - close(fd); - return 0; - } - - if (! (S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) { - clib_warning ("not a regular file: `%s'", fn); - goto bail; - } + fd = open (fn, O_RDONLY); + if (fd < 0) + { + clib_warning ("failed to open `%s'", fn); + return 0; + } - while (n > 0) - { - l = vec_len (buf); - vec_resize (buf, 4096); - n = read (fd, buf + l, 4096); - if (n > 0) - { - _vec_len (buf) = l + n; - if (n < 4096) - break; - } - else - break; - } - if (um->log_fd && vec_len (buf)) - { - u8 * lv = 0; - lv = format (lv, "%U: ***** Startup Config *****\n%v", - format_timeval, - 0 /* current bat-time */, - 0 /* current bat-format */, - buf); + if (fstat (fd, &s) < 0) { - int rv __attribute__((unused)) = - write (um->log_fd, lv, vec_len(lv)); + clib_warning ("failed to stat `%s'", fn); + bail: + close (fd); + return 0; } - vec_reset_length (lv); - lv = format (lv, "%U: ***** End Startup Config *****\n", - format_timeval, - 0 /* current bat-time */, - 0 /* current bat-format */); + + if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode))) { - int rv __attribute__((unused)) = - write (um->log_fd, lv, vec_len(lv)); + clib_warning ("not a regular file: `%s'", fn); + goto bail; } - vec_free (lv); - } - - if (vec_len(buf)) - { - unformat_init_vector (&sub_input, buf); - vlib_cli_input (vm, &sub_input, 0, 0); - /* frees buf for us */ - unformat_free (&sub_input); - } - close(fd); - } + + while (n > 0) + { + l = vec_len (buf); + vec_resize (buf, 4096); + n = read (fd, buf + l, 4096); + if (n > 0) + { + _vec_len (buf) = l + n; + if (n < 4096) + break; + } + else + break; + } + if (um->log_fd && vec_len (buf)) + { + u8 *lv = 0; + lv = format (lv, "%U: ***** Startup Config *****\n%v", + format_timeval, 0 /* current bat-time */ , + 0 /* current bat-format */ , + buf); + { + int rv __attribute__ ((unused)) = + write (um->log_fd, lv, vec_len (lv)); + } + vec_reset_length (lv); + lv = format (lv, "%U: ***** End Startup Config *****\n", + format_timeval, 0 /* current bat-time */ , + 0 /* current bat-format */ ); + { + int rv __attribute__ ((unused)) = + write (um->log_fd, lv, vec_len (lv)); + } + vec_free (lv); + } + + if (vec_len (buf)) + { + unformat_init_vector (&sub_input, buf); + vlib_cli_input (vm, &sub_input, 0, 0); + /* frees buf for us */ + unformat_free (&sub_input); + } + close (fd); + } return 0; } +/* *INDENT-OFF* */ VLIB_REGISTER_NODE (startup_config_node,static) = { .function = startup_config_process, .type = VLIB_NODE_TYPE_PROCESS, .name = "startup-config-process", }; +/* *INDENT-ON* */ static clib_error_t * unix_config (vlib_main_t * vm, unformat_input_t * input) { - unix_main_t * um = &unix_main; - clib_error_t * error = 0; + unix_main_t *um = &unix_main; + clib_error_t *error = 0; /* Defaults */ um->cli_pager_buffer_limit = UNIX_CLI_DEFAULT_PAGER_LIMIT; @@ -312,76 +317,76 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { - char * cli_prompt; + char *cli_prompt; if (unformat (input, "interactive")) um->flags |= UNIX_FLAG_INTERACTIVE; else if (unformat (input, "nodaemon")) - um->flags |= UNIX_FLAG_NODAEMON; + um->flags |= UNIX_FLAG_NODAEMON; else if (unformat (input, "cli-prompt %s", &cli_prompt)) vlib_unix_cli_set_prompt (cli_prompt); - else if (unformat (input, "cli-listen %s", &um->cli_listen_socket.config)) + else + if (unformat (input, "cli-listen %s", &um->cli_listen_socket.config)) ; else if (unformat (input, "cli-line-mode")) - um->cli_line_mode = 1; + um->cli_line_mode = 1; else if (unformat (input, "cli-no-banner")) - um->cli_no_banner = 1; + um->cli_no_banner = 1; else if (unformat (input, "cli-no-pager")) - um->cli_no_pager = 1; + um->cli_no_pager = 1; else if (unformat (input, "cli-pager-buffer-limit %d", - &um->cli_pager_buffer_limit)) - ; - else if (unformat (input, "cli-history-limit %d", &um->cli_history_limit)) - ; + &um->cli_pager_buffer_limit)) + ; + else + if (unformat (input, "cli-history-limit %d", &um->cli_history_limit)) + ; else if (unformat (input, "full-coredump")) - { - int fd; - - fd = open ("/proc/self/coredump_filter", O_WRONLY); - if (fd > 0) - { - if (write (fd, "0x6f\n", 5) != 5) - clib_unix_warning ("coredump filter write failed!"); - close(fd); - } - else - clib_unix_warning ("couldn't open /proc/self/coredump_filter"); - } - else if (unformat (input, "startup-config %s", - &um->startup_config_filename)) - ; - else if (unformat (input, "exec %s", - &um->startup_config_filename)) - ; + { + int fd; + + fd = open ("/proc/self/coredump_filter", O_WRONLY); + if (fd > 0) + { + if (write (fd, "0x6f\n", 5) != 5) + clib_unix_warning ("coredump filter write failed!"); + close (fd); + } + else + clib_unix_warning ("couldn't open /proc/self/coredump_filter"); + } + else if (unformat (input, "startup-config %s", + &um->startup_config_filename)) + ; + else if (unformat (input, "exec %s", &um->startup_config_filename)) + ; else if (unformat (input, "log %s", &um->log_filename)) - { - um->log_fd = open ((char *) um->log_filename, - O_CREAT | O_WRONLY | O_APPEND, 0644); - if (um->log_fd < 0) - { - clib_warning ("couldn't open log '%s'\n", um->log_filename); - um->log_fd = 0; - } - else - { - u8 * lv = 0; - lv = format (0, "%U: ***** Start: PID %d *****\n", - format_timeval, - 0 /* current bat-time */, - 0 /* current bat-format */, - getpid()); + { + um->log_fd = open ((char *) um->log_filename, + O_CREAT | O_WRONLY | O_APPEND, 0644); + if (um->log_fd < 0) + { + clib_warning ("couldn't open log '%s'\n", um->log_filename); + um->log_fd = 0; + } + else + { + u8 *lv = 0; + lv = format (0, "%U: ***** Start: PID %d *****\n", + format_timeval, 0 /* current bat-time */ , + 0 /* current bat-format */ , + getpid ()); { - int rv __attribute__((unused)) = - write (um->log_fd, lv, vec_len(lv)); + int rv __attribute__ ((unused)) = + write (um->log_fd, lv, vec_len (lv)); } - vec_free (lv); - } - } + vec_free (lv); + } + } else return clib_error_return (0, "unknown input `%U'", format_unformat_error, input); } - if (! (um->flags & UNIX_FLAG_INTERACTIVE)) + if (!(um->flags & UNIX_FLAG_INTERACTIVE)) { error = setup_signal_handlers (um); if (error) @@ -390,9 +395,9 @@ unix_config (vlib_main_t * vm, unformat_input_t * input) openlog (vm->name, LOG_CONS | LOG_PERROR | LOG_PID, LOG_DAEMON); clib_error_register_handler (unix_error_handler, um); - if (! (um->flags & UNIX_FLAG_NODAEMON) - && daemon (/* chdir to / */ 0, - /* stdin/stdout/stderr -> /dev/null */ 0) < 0) + if (!(um->flags & UNIX_FLAG_NODAEMON) && daemon ( /* chdir to / */ 0, + /* stdin/stdout/stderr -> /dev/null */ + 0) < 0) clib_error_return (0, "daemon () fails"); } um->unix_config_complete = 1; @@ -415,54 +420,56 @@ VLIB_MAIN_LOOP_EXIT_FUNCTION (unix_exit); u8 **vlib_thread_stacks; -static uword thread0 (uword arg) +static uword +thread0 (uword arg) { - vlib_main_t * vm = (vlib_main_t *)arg; + vlib_main_t *vm = (vlib_main_t *) arg; unformat_input_t input; int i; - - unformat_init_command_line (&input, (char **)vm->argv); + + unformat_init_command_line (&input, (char **) vm->argv); i = vlib_main (vm, &input); unformat_free (&input); - + return i; - } +} -int vlib_unix_main (int argc, char * argv[]) +int +vlib_unix_main (int argc, char *argv[]) { - vlib_main_t * vm = &vlib_global_main; /* one and only time for this! */ - - clib_smp_main_t * sm = &clib_smp_main; - vlib_thread_main_t * tm = &vlib_thread_main; + vlib_main_t *vm = &vlib_global_main; /* one and only time for this! */ + + clib_smp_main_t *sm = &clib_smp_main; + vlib_thread_main_t *tm = &vlib_thread_main; unformat_input_t input; - u8 * thread_stacks; - clib_error_t * e; + u8 *thread_stacks; + clib_error_t *e; int i; - vm->argv = (u8 **)argv; + vm->argv = (u8 **) argv; vm->name = argv[0]; vm->heap_base = clib_mem_get_heap (); - ASSERT(vm->heap_base); + ASSERT (vm->heap_base); i = vlib_plugin_early_init (vm); if (i) return i; - - unformat_init_command_line (&input, (char **)vm->argv); + + unformat_init_command_line (&input, (char **) vm->argv); if (vm->init_functions_called == 0) - vm->init_functions_called = hash_create (0, /* value bytes */ 0); - e = vlib_call_all_config_functions (vm, &input, 1 /* early */); + vm->init_functions_called = hash_create (0, /* value bytes */ 0); + e = vlib_call_all_config_functions (vm, &input, 1 /* early */ ); if (e != 0) { - clib_error_report(e); + clib_error_report (e); return 1; } unformat_free (&input); /* allocate N x 1mb stacks, aligned e.g. to a 16mb boundary */ - thread_stacks = clib_mem_alloc_aligned - (tm->n_thread_stacks * VLIB_THREAD_STACK_SIZE, - (VLIB_MAX_CPUS << VLIB_LOG2_THREAD_STACK_SIZE)); + thread_stacks = clib_mem_alloc_aligned + (tm->n_thread_stacks * VLIB_THREAD_STACK_SIZE, + (VLIB_MAX_CPUS << VLIB_LOG2_THREAD_STACK_SIZE)); sm->vm_base = thread_stacks; sm->log2_n_per_cpu_vm_bytes = VLIB_LOG2_THREAD_STACK_SIZE; @@ -472,17 +479,26 @@ int vlib_unix_main (int argc, char * argv[]) { vlib_thread_stacks[i] = thread_stacks; - /* + /* * Disallow writes to the bottom page of the stack, to * catch stack overflows. */ - if (mprotect (thread_stacks, clib_mem_get_page_size(), PROT_READ) < 0) - clib_unix_warning ("thread stack"); + if (mprotect (thread_stacks, clib_mem_get_page_size (), PROT_READ) < 0) + clib_unix_warning ("thread stack"); thread_stacks += VLIB_THREAD_STACK_SIZE; } - - i = clib_calljmp (thread0, (uword) vm, - (void *)(vlib_thread_stacks[0] + VLIB_THREAD_STACK_SIZE)); + + i = clib_calljmp (thread0, (uword) vm, + (void *) (vlib_thread_stacks[0] + + VLIB_THREAD_STACK_SIZE)); return i; } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/mc_socket.c b/vlib/vlib/unix/mc_socket.c index be68a13c1e2..2077fd7c519 100644 --- a/vlib/vlib/unix/mc_socket.c +++ b/vlib/vlib/unix/mc_socket.c @@ -19,10 +19,11 @@ #include <vlib/unix/mc_socket.h> #include <sys/ioctl.h> /* for FIONBIO */ -#include <netinet/tcp.h> /* for TCP_NODELAY */ +#include <netinet/tcp.h> /* for TCP_NODELAY */ #include <net/if.h> /* for struct ifreq */ -static u8 * format_socket_peer_id (u8 * s, va_list * args) +static u8 * +format_socket_peer_id (u8 * s, va_list * args) { u64 peer_id_as_u64 = va_arg (*args, u64); mc_peer_id_t peer_id; @@ -30,36 +31,34 @@ static u8 * format_socket_peer_id (u8 * s, va_list * args) u32 a = mc_socket_peer_id_get_address (peer_id); u32 p = mc_socket_peer_id_get_port (peer_id); - s = format (s, "%U:%04x", format_network_address, AF_INET, &a, - ntohs (p)); + s = format (s, "%U:%04x", format_network_address, AF_INET, &a, ntohs (p)); return s; } -typedef void (mc_msg_handler_t) (mc_main_t * mcm, void * msg, u32 buffer_index); +typedef void (mc_msg_handler_t) (mc_main_t * mcm, void *msg, + u32 buffer_index); -always_inline void msg_handler (mc_main_t * mcm, - u32 buffer_index, - u32 handler_frees_buffer, - void * _h) +always_inline void +msg_handler (mc_main_t * mcm, + u32 buffer_index, u32 handler_frees_buffer, void *_h) { - vlib_main_t * vm = mcm->vlib_main; - mc_msg_handler_t * h = _h; - vlib_buffer_t * b = vlib_get_buffer (vm, buffer_index); - void * the_msg = vlib_buffer_get_current (b); + vlib_main_t *vm = mcm->vlib_main; + mc_msg_handler_t *h = _h; + vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); + void *the_msg = vlib_buffer_get_current (b); h (mcm, the_msg, buffer_index); - if (! handler_frees_buffer) + if (!handler_frees_buffer) vlib_buffer_free_one (vm, buffer_index); } static uword append_buffer_index_to_iovec (vlib_main_t * vm, - u32 buffer_index, - struct iovec ** iovs_return) + u32 buffer_index, struct iovec **iovs_return) { - struct iovec * i; - vlib_buffer_t * b; + struct iovec *i; + vlib_buffer_t *b; u32 bi = buffer_index; u32 l = 0; @@ -70,7 +69,7 @@ append_buffer_index_to_iovec (vlib_main_t * vm, i->iov_base = vlib_buffer_get_current (b); i->iov_len = b->current_length; l += i->iov_len; - if (! (b->flags & VLIB_BUFFER_NEXT_PRESENT)) + if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) break; bi = b->next_buffer; } @@ -80,11 +79,9 @@ append_buffer_index_to_iovec (vlib_main_t * vm, static clib_error_t * sendmsg_helper (mc_socket_main_t * msm, - int socket, - struct sockaddr_in * tx_addr, - u32 buffer_index) + int socket, struct sockaddr_in *tx_addr, u32 buffer_index) { - vlib_main_t * vm = msm->mc_main.vlib_main; + vlib_main_t *vm = msm->mc_main.vlib_main; struct msghdr h; word n_bytes, n_bytes_tx, n_retries; @@ -105,7 +102,7 @@ sendmsg_helper (mc_socket_main_t * msm, n_retries = 0; while ((n_bytes_tx = sendmsg (socket, &h, /* flags */ 0)) != n_bytes - && errno == EAGAIN) + && errno == EAGAIN) n_retries++; if (n_bytes_tx != n_bytes) { @@ -114,11 +111,13 @@ sendmsg_helper (mc_socket_main_t * msm, } if (n_retries) { - ELOG_TYPE_DECLARE (e) = { - .format = "sendmsg-helper: %d retries", - .format_args = "i4", - }; - struct { u32 retries; } * ed = 0; + ELOG_TYPE_DECLARE (e) = + { + .format = "sendmsg-helper: %d retries",.format_args = "i4",}; + struct + { + u32 retries; + } *ed = 0; ed = ELOG_DATA (&vm->elog_main, e); ed->retries = n_retries; @@ -127,12 +126,12 @@ sendmsg_helper (mc_socket_main_t * msm, } static clib_error_t * -tx_buffer (void * transport, mc_transport_type_t type, u32 buffer_index) +tx_buffer (void *transport, mc_transport_type_t type, u32 buffer_index) { - mc_socket_main_t *msm = (mc_socket_main_t *)transport; - vlib_main_t * vm = msm->mc_main.vlib_main; - mc_multicast_socket_t * ms = &msm->multicast_sockets[type]; - clib_error_t * error; + mc_socket_main_t *msm = (mc_socket_main_t *) transport; + vlib_main_t *vm = msm->mc_main.vlib_main; + mc_multicast_socket_t *ms = &msm->multicast_sockets[type]; + clib_error_t *error; error = sendmsg_helper (msm, ms->socket, &ms->tx_addr, buffer_index); if (type != MC_TRANSPORT_USER_REQUEST_TO_RELAY) vlib_buffer_free_one (vm, buffer_index); @@ -143,9 +142,9 @@ static clib_error_t * tx_ack (void *transport, mc_peer_id_t dest_peer_id, u32 buffer_index) { struct sockaddr_in tx_addr; - mc_socket_main_t *msm = (mc_socket_main_t *)transport; - vlib_main_t * vm = msm->mc_main.vlib_main; - clib_error_t * error; + mc_socket_main_t *msm = (mc_socket_main_t *) transport; + vlib_main_t *vm = msm->mc_main.vlib_main; + clib_error_t *error; memset (&tx_addr, 0, sizeof (tx_addr)); tx_addr.sin_family = AF_INET; @@ -160,12 +159,11 @@ tx_ack (void *transport, mc_peer_id_t dest_peer_id, u32 buffer_index) static clib_error_t * recvmsg_helper (mc_socket_main_t * msm, int socket, - struct sockaddr_in * rx_addr, - u32 * buffer_index, - u32 drop_message) + struct sockaddr_in *rx_addr, + u32 * buffer_index, u32 drop_message) { - vlib_main_t * vm = msm->mc_main.vlib_main; - vlib_buffer_t * b; + vlib_main_t *vm = msm->mc_main.vlib_main; + vlib_buffer_t *b; uword n_left, n_alloc, n_mtu, i, i_rx; const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES; word n_bytes_left; @@ -177,7 +175,8 @@ recvmsg_helper (mc_socket_main_t * msm, { uword max_alloc = 8 * n_mtu; vec_validate (msm->rx_buffers, max_alloc - 1); - n_alloc = vlib_buffer_alloc (vm, msm->rx_buffers + n_left, max_alloc - n_left); + n_alloc = + vlib_buffer_alloc (vm, msm->rx_buffers + n_left, max_alloc - n_left); _vec_len (msm->rx_buffers) = n_left + n_alloc; } @@ -225,7 +224,8 @@ recvmsg_helper (mc_socket_main_t * msm, 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; @@ -239,19 +239,22 @@ recvmsg_helper (mc_socket_main_t * msm, _vec_len (msm->rx_buffers) = i_rx; - return 0 /* no error */; + return 0 /* no error */ ; } -static clib_error_t * mastership_socket_read_ready (unix_file_t * uf) +static clib_error_t * +mastership_socket_read_ready (unix_file_t * uf) { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - mc_main_t * mcm = &msm->mc_main; - mc_multicast_socket_t * ms = &msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP]; - clib_error_t * error; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + mc_main_t *mcm = &msm->mc_main; + mc_multicast_socket_t *ms = + &msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP]; + clib_error_t *error; u32 bi; - error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ 0); - if (! error) + error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ + 0); + if (!error) msg_handler (mcm, bi, /* handler_frees_buffer */ 0, mc_msg_master_assert_handler); @@ -259,45 +262,53 @@ static clib_error_t * mastership_socket_read_ready (unix_file_t * uf) return error; } -static clib_error_t * to_relay_socket_read_ready (unix_file_t * uf) +static clib_error_t * +to_relay_socket_read_ready (unix_file_t * uf) { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; mc_main_t *mcm = &msm->mc_main; - vlib_main_t * vm = msm->mc_main.vlib_main; - mc_multicast_socket_t * ms_to_relay = &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY]; - mc_multicast_socket_t * ms_from_relay = &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY]; - clib_error_t * error; + vlib_main_t *vm = msm->mc_main.vlib_main; + mc_multicast_socket_t *ms_to_relay = + &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY]; + mc_multicast_socket_t *ms_from_relay = + &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY]; + clib_error_t *error; u32 bi; u32 is_master = mcm->relay_state == MC_RELAY_STATE_MASTER; /* Not the ordering master? Turf the msg */ error = recvmsg_helper (msm, ms_to_relay->socket, /* rx_addr */ 0, &bi, - /* drop_message */ ! is_master); + /* drop_message */ !is_master); /* If we are the master, number and rebroadcast the msg. */ - if (! error && is_master) + if (!error && is_master) { - vlib_buffer_t * b = vlib_get_buffer (vm, bi); - mc_msg_user_request_t * mp = vlib_buffer_get_current (b); + vlib_buffer_t *b = vlib_get_buffer (vm, bi); + mc_msg_user_request_t *mp = vlib_buffer_get_current (b); mp->global_sequence = clib_host_to_net_u32 (mcm->relay_global_sequence); mcm->relay_global_sequence++; - error = sendmsg_helper (msm, ms_from_relay->socket, &ms_from_relay->tx_addr, bi); + error = + sendmsg_helper (msm, ms_from_relay->socket, &ms_from_relay->tx_addr, + bi); vlib_buffer_free_one (vm, bi); } return error; } -static clib_error_t * from_relay_socket_read_ready (unix_file_t * uf) +static clib_error_t * +from_relay_socket_read_ready (unix_file_t * uf) { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - mc_main_t * mcm = &msm->mc_main; - mc_multicast_socket_t * ms = &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY]; - clib_error_t * error; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + mc_main_t *mcm = &msm->mc_main; + mc_multicast_socket_t *ms = + &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY]; + clib_error_t *error; u32 bi; - error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ 0); - if (! error) + error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ + 0); + if (!error) { msg_handler (mcm, bi, /* handler_frees_buffer */ 1, mc_msg_user_request_handler); @@ -305,20 +316,22 @@ static clib_error_t * from_relay_socket_read_ready (unix_file_t * uf) return error; } -static clib_error_t * join_socket_read_ready (unix_file_t * uf) +static clib_error_t * +join_socket_read_ready (unix_file_t * uf) { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - mc_main_t * mcm = &msm->mc_main; - vlib_main_t * vm = mcm->vlib_main; - mc_multicast_socket_t * ms = &msm->multicast_sockets[MC_TRANSPORT_JOIN]; - clib_error_t * error; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + mc_main_t *mcm = &msm->mc_main; + vlib_main_t *vm = mcm->vlib_main; + mc_multicast_socket_t *ms = &msm->multicast_sockets[MC_TRANSPORT_JOIN]; + clib_error_t *error; u32 bi; - error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ 0); - if (! error) + error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ + 0); + if (!error) { - vlib_buffer_t * b = vlib_get_buffer (vm, bi); - mc_msg_join_or_leave_request_t * mp = vlib_buffer_get_current (b); + vlib_buffer_t *b = vlib_get_buffer (vm, bi); + mc_msg_join_or_leave_request_t *mp = vlib_buffer_get_current (b); switch (clib_host_to_net_u32 (mp->type)) { @@ -340,23 +353,25 @@ static clib_error_t * join_socket_read_ready (unix_file_t * uf) return error; } -static clib_error_t * ack_socket_read_ready (unix_file_t * uf) +static clib_error_t * +ack_socket_read_ready (unix_file_t * uf) { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - mc_main_t * mcm = &msm->mc_main; - clib_error_t * error; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + mc_main_t *mcm = &msm->mc_main; + clib_error_t *error; u32 bi; - error = recvmsg_helper (msm, msm->ack_socket, /* rx_addr */ 0, &bi, /* drop_message */ 0); - if (! error) + error = recvmsg_helper (msm, msm->ack_socket, /* rx_addr */ 0, &bi, + /* drop_message */ 0); + if (!error) msg_handler (mcm, bi, /* handler_frees_buffer */ 0, mc_msg_user_ack_handler); return error; } -static void catchup_cleanup (mc_socket_main_t *msm, - mc_socket_catchup_t *c, - unix_main_t *um, unix_file_t *uf) +static void +catchup_cleanup (mc_socket_main_t * msm, + mc_socket_catchup_t * c, unix_main_t * um, unix_file_t * uf) { hash_unset (msm->catchup_index_by_file_descriptor, uf->file_descriptor); unix_file_del (um, uf); @@ -366,23 +381,29 @@ static void catchup_cleanup (mc_socket_main_t *msm, } static mc_socket_catchup_t * -find_catchup_from_file_descriptor (mc_socket_main_t * msm, int file_descriptor) +find_catchup_from_file_descriptor (mc_socket_main_t * msm, + int file_descriptor) { - uword * p = hash_get (msm->catchup_index_by_file_descriptor, file_descriptor); + uword *p = + hash_get (msm->catchup_index_by_file_descriptor, file_descriptor); return p ? pool_elt_at_index (msm->catchups, p[0]) : 0; } -static clib_error_t * catchup_socket_read_ready (unix_file_t * uf, int is_server) +static clib_error_t * +catchup_socket_read_ready (unix_file_t * uf, int is_server) { - unix_main_t * um = &unix_main; - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; + unix_main_t *um = &unix_main; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; mc_main_t *mcm = &msm->mc_main; - mc_socket_catchup_t * c = find_catchup_from_file_descriptor (msm, uf->file_descriptor); + mc_socket_catchup_t *c = + find_catchup_from_file_descriptor (msm, uf->file_descriptor); word l, n, is_eof; l = vec_len (c->input_vector); vec_resize (c->input_vector, 4096); - n = read (uf->file_descriptor, c->input_vector + l, vec_len (c->input_vector) - l); + n = + read (uf->file_descriptor, c->input_vector + l, + vec_len (c->input_vector) - l); is_eof = n == 0; if (n < 0) @@ -402,43 +423,50 @@ static clib_error_t * catchup_socket_read_ready (unix_file_t * uf, int is_server { if (is_server) { - mc_msg_catchup_request_handler (mcm, (void *) c->input_vector, c - msm->catchups); + mc_msg_catchup_request_handler (mcm, (void *) c->input_vector, + c - msm->catchups); _vec_len (c->input_vector) = 0; } else { - mc_msg_catchup_reply_handler (mcm, (void *) c->input_vector, c - msm->catchups); + mc_msg_catchup_reply_handler (mcm, (void *) c->input_vector, + c - msm->catchups); c->input_vector = 0; /* reply handler is responsible for freeing vector */ catchup_cleanup (msm, c, um, uf); } } - return 0 /* no error */; + return 0 /* no error */ ; } -static clib_error_t * catchup_server_read_ready (unix_file_t * uf) -{ return catchup_socket_read_ready (uf, /* is_server */ 1); } +static clib_error_t * +catchup_server_read_ready (unix_file_t * uf) +{ + return catchup_socket_read_ready (uf, /* is_server */ 1); +} -static clib_error_t * catchup_client_read_ready (unix_file_t * uf) -{ - if (MC_EVENT_LOGGING) - { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - vlib_main_t * vm = msm->mc_main.vlib_main; - - ELOG_TYPE (e, "catchup_client_read_ready"); - ELOG (&vm->elog_main, e, 0); - } - return catchup_socket_read_ready (uf, /* is_server */ 0); +static clib_error_t * +catchup_client_read_ready (unix_file_t * uf) +{ + if (MC_EVENT_LOGGING) + { + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + vlib_main_t *vm = msm->mc_main.vlib_main; + + ELOG_TYPE (e, "catchup_client_read_ready"); + ELOG (&vm->elog_main, e, 0); + } + return catchup_socket_read_ready (uf, /* is_server */ 0); } -static clib_error_t * +static clib_error_t * catchup_socket_write_ready (unix_file_t * uf, int is_server) { - unix_main_t * um = &unix_main; - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - mc_socket_catchup_t *c = find_catchup_from_file_descriptor (msm, uf->file_descriptor); - clib_error_t * error = 0; + unix_main_t *um = &unix_main; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + mc_socket_catchup_t *c = + find_catchup_from_file_descriptor (msm, uf->file_descriptor); + clib_error_t *error = 0; int n; if (c->connect_in_progress) @@ -447,50 +475,54 @@ catchup_socket_write_ready (unix_file_t * uf, int is_server) c->connect_in_progress = 0; len = sizeof (value); - if (getsockopt (c->socket, SOL_SOCKET, - SO_ERROR, &value, &len) < 0) + if (getsockopt (c->socket, SOL_SOCKET, SO_ERROR, &value, &len) < 0) { error = clib_error_return_unix (0, "getsockopt SO_ERROR"); goto error_quit; } if (value != 0) { - error = clib_error_return_code (0, value, CLIB_ERROR_ERRNO_VALID, "connect fails"); + error = + clib_error_return_code (0, value, CLIB_ERROR_ERRNO_VALID, + "connect fails"); goto error_quit; } } - while (1) + while (1) { u32 n_this_write; - - n_this_write = - clib_min (vec_len (c->output_vector) - c->output_vector_n_written, - msm->rx_mtu_n_bytes - 64 /* ip + tcp + option allowance */); + + n_this_write = + clib_min (vec_len (c->output_vector) - c->output_vector_n_written, + msm->rx_mtu_n_bytes - + 64 /* ip + tcp + option allowance */ ); if (n_this_write <= 0) - break; - - do { - n = write (uf->file_descriptor, - c->output_vector + c->output_vector_n_written, - n_this_write); - } while (n < 0 && errno == EAGAIN); - + break; + + do + { + n = write (uf->file_descriptor, + c->output_vector + c->output_vector_n_written, + n_this_write); + } + while (n < 0 && errno == EAGAIN); + if (n < 0) - { - error = clib_error_return_unix (0, "write"); - goto error_quit; - } + { + error = clib_error_return_unix (0, "write"); + goto error_quit; + } c->output_vector_n_written += n; - } + } if (c->output_vector_n_written >= vec_len (c->output_vector)) { - if (! is_server) + if (!is_server) { - uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; - unix_main.file_update (uf, UNIX_FILE_UPDATE_MODIFY); + uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; + unix_main.file_update (uf, UNIX_FILE_UPDATE_MODIFY); /* Send EOF to other side. */ shutdown (uf->file_descriptor, SHUT_WR); return error; @@ -504,41 +536,48 @@ catchup_socket_write_ready (unix_file_t * uf, int is_server) return error; } -static clib_error_t * +static clib_error_t * catchup_server_write_ready (unix_file_t * uf) -{ return catchup_socket_write_ready (uf, /* is_server */ 1); } +{ + return catchup_socket_write_ready (uf, /* is_server */ 1); +} -static clib_error_t * +static clib_error_t * catchup_client_write_ready (unix_file_t * uf) -{ return catchup_socket_write_ready (uf, /* is_server */ 0); } +{ + return catchup_socket_write_ready (uf, /* is_server */ 0); +} -static clib_error_t *catchup_socket_error_ready (unix_file_t *uf) +static clib_error_t * +catchup_socket_error_ready (unix_file_t * uf) { unix_main_t *um = &unix_main; - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; - mc_socket_catchup_t *c = find_catchup_from_file_descriptor (msm, uf->file_descriptor); + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; + mc_socket_catchup_t *c = + find_catchup_from_file_descriptor (msm, uf->file_descriptor); catchup_cleanup (msm, c, um, uf); return clib_error_return (0, "error"); } -static clib_error_t *catchup_listen_read_ready (unix_file_t * uf) +static clib_error_t * +catchup_listen_read_ready (unix_file_t * uf) { - mc_socket_main_t *msm = (mc_socket_main_t *)uf->private_data; + mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; struct sockaddr_in client_addr; int client_len; mc_socket_catchup_t *c; - unix_file_t template = {0}; + unix_file_t template = { 0 }; pool_get (msm->catchups, c); - memset(c, 0, sizeof (c[0])); + memset (c, 0, sizeof (c[0])); + + client_len = sizeof (client_addr); - client_len = sizeof(client_addr); - /* Acquires the non-blocking attrib from the server socket. */ - c->socket = accept (uf->file_descriptor, - (struct sockaddr *)&client_addr, - (socklen_t *)&client_len); - + c->socket = accept (uf->file_descriptor, + (struct sockaddr *) &client_addr, + (socklen_t *) & client_len); + if (c->socket < 0) { pool_put (msm->catchups, c); @@ -547,26 +586,29 @@ static clib_error_t *catchup_listen_read_ready (unix_file_t * uf) if (MC_EVENT_LOGGING) { - mc_main_t * mcm = &msm->mc_main; - vlib_main_t * vm = mcm->vlib_main; + mc_main_t *mcm = &msm->mc_main; + vlib_main_t *vm = mcm->vlib_main; - ELOG_TYPE_DECLARE (e) = { - .format = "catchup accepted from 0x%lx", - .format_args = "i4", - }; - struct { u32 addr; } * ed = 0; + ELOG_TYPE_DECLARE (e) = + { + .format = "catchup accepted from 0x%lx",.format_args = "i4",}; + struct + { + u32 addr; + } *ed = 0; ed = ELOG_DATA (&vm->elog_main, e); - ed->addr = ntohl(client_addr.sin_addr.s_addr); + ed->addr = ntohl (client_addr.sin_addr.s_addr); } /* Disable the Nagle algorithm, ship catchup pkts immediately */ { int one = 1; - if ((setsockopt(c->socket, IPPROTO_TCP, - TCP_NODELAY, (void *)&one, sizeof(one))) < 0) { - clib_unix_warning("catchup socket: set TCP_NODELAY"); - } + if ((setsockopt (c->socket, IPPROTO_TCP, + TCP_NODELAY, (void *) &one, sizeof (one))) < 0) + { + clib_unix_warning ("catchup socket: set TCP_NODELAY"); + } } template.read_function = catchup_server_read_ready; @@ -575,19 +617,21 @@ static clib_error_t *catchup_listen_read_ready (unix_file_t * uf) template.file_descriptor = c->socket; template.private_data = pointer_to_uword (msm); c->unix_file_index = unix_file_add (&unix_main, &template); - hash_set (msm->catchup_index_by_file_descriptor, c->socket, c - msm->catchups); + hash_set (msm->catchup_index_by_file_descriptor, c->socket, + c - msm->catchups); return 0; } /* Return and bind to an unused port. */ -static word find_and_bind_to_free_port (word sock, word port) +static word +find_and_bind_to_free_port (word sock, word port) { for (; port < 1 << 16; port++) { struct sockaddr_in a; - memset (&a, 0, sizeof(a)); /* Warnings be gone */ + memset (&a, 0, sizeof (a)); /* Warnings be gone */ a.sin_family = PF_INET; a.sin_addr.s_addr = INADDR_ANY; @@ -596,54 +640,57 @@ static word find_and_bind_to_free_port (word sock, word port) if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0) break; } - + return port < 1 << 16 ? port : -1; } static clib_error_t * setup_mutlicast_socket (mc_socket_main_t * msm, mc_multicast_socket_t * ms, - char * type, - uword udp_port) + char *type, uword udp_port) { int one = 1; struct ip_mreq mcast_req; - if (! msm->multicast_ttl) + if (!msm->multicast_ttl) msm->multicast_ttl = 1; /* mastership (multicast) TX socket */ if ((ms->socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) - return clib_error_return_unix(0, "%s socket", type); + return clib_error_return_unix (0, "%s socket", type); { u8 ttl = msm->multicast_ttl; - if ((setsockopt(ms->socket, IPPROTO_IP, - IP_MULTICAST_TTL, (void *)&ttl, sizeof(ttl))) < 0) - return clib_error_return_unix(0, "%s set multicast ttl", type); + if ((setsockopt (ms->socket, IPPROTO_IP, + IP_MULTICAST_TTL, (void *) &ttl, sizeof (ttl))) < 0) + return clib_error_return_unix (0, "%s set multicast ttl", type); } - if (setsockopt(ms->socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) + if (setsockopt (ms->socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)) < + 0) return clib_error_return_unix (0, "%s setsockopt SO_REUSEADDR", type); memset (&ms->tx_addr, 0, sizeof (ms->tx_addr)); ms->tx_addr.sin_family = AF_INET; - ms->tx_addr.sin_addr.s_addr = htonl (msm->multicast_tx_ip4_address_host_byte_order); + ms->tx_addr.sin_addr.s_addr = + htonl (msm->multicast_tx_ip4_address_host_byte_order); ms->tx_addr.sin_port = htons (udp_port); - - if (bind(ms->socket, (struct sockaddr *)&ms->tx_addr, - sizeof (ms->tx_addr)) < 0) - return clib_error_return_unix(0, "%s bind", type); + + if (bind (ms->socket, (struct sockaddr *) &ms->tx_addr, + sizeof (ms->tx_addr)) < 0) + return clib_error_return_unix (0, "%s bind", type); memset (&mcast_req, 0, sizeof (mcast_req)); - mcast_req.imr_multiaddr.s_addr = htonl (msm->multicast_tx_ip4_address_host_byte_order); + mcast_req.imr_multiaddr.s_addr = + htonl (msm->multicast_tx_ip4_address_host_byte_order); mcast_req.imr_interface.s_addr = msm->if_ip4_address_net_byte_order; - if ((setsockopt(ms->socket, IPPROTO_IP, - IP_ADD_MEMBERSHIP, (void *)&mcast_req, - sizeof (mcast_req))) < 0) - return clib_error_return_unix(0, "%s IP_ADD_MEMBERSHIP setsockopt", type); + if ((setsockopt (ms->socket, IPPROTO_IP, + IP_ADD_MEMBERSHIP, (void *) &mcast_req, + sizeof (mcast_req))) < 0) + return clib_error_return_unix (0, "%s IP_ADD_MEMBERSHIP setsockopt", + type); if (ioctl (ms->socket, FIONBIO, &one) < 0) return clib_error_return_unix (0, "%s set FIONBIO", type); @@ -652,7 +699,7 @@ setup_mutlicast_socket (mc_socket_main_t * msm, { u32 len = 1 << 20; socklen_t sl = sizeof (len); - if (setsockopt(ms->socket, SOL_SOCKET, SO_SNDBUF, &len, sl) < 0) + if (setsockopt (ms->socket, SOL_SOCKET, SO_SNDBUF, &len, sl) < 0) clib_unix_error ("setsockopt"); } @@ -660,92 +707,96 @@ setup_mutlicast_socket (mc_socket_main_t * msm, } static clib_error_t * -socket_setup (mc_socket_main_t *msm) +socket_setup (mc_socket_main_t * msm) { int one = 1; - clib_error_t * error; + clib_error_t *error; u32 port; - if (! msm->base_multicast_udp_port_host_byte_order) - msm->base_multicast_udp_port_host_byte_order = - 0xffff - ((MC_N_TRANSPORT_TYPE + 2 /* ack socket, catchup socket */) - - 1); + if (!msm->base_multicast_udp_port_host_byte_order) + msm->base_multicast_udp_port_host_byte_order = + 0xffff - ((MC_N_TRANSPORT_TYPE + 2 /* ack socket, catchup socket */ ) + - 1); port = msm->base_multicast_udp_port_host_byte_order; error = setup_mutlicast_socket (msm, - &msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP], - "mastership", + &msm->multicast_sockets + [MC_TRANSPORT_MASTERSHIP], "mastership", port++); if (error) return error; error = setup_mutlicast_socket (msm, &msm->multicast_sockets[MC_TRANSPORT_JOIN], - "join", - port++); + "join", port++); if (error) return error; error = setup_mutlicast_socket (msm, - &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY], - "to relay", - port++); + &msm->multicast_sockets + [MC_TRANSPORT_USER_REQUEST_TO_RELAY], + "to relay", port++); if (error) return error; error = setup_mutlicast_socket (msm, - &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY], - "from relay", - port++); + &msm->multicast_sockets + [MC_TRANSPORT_USER_REQUEST_FROM_RELAY], + "from relay", port++); if (error) return error; /* ACK rx socket */ msm->ack_socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (msm->ack_socket < 0) - return clib_error_return_unix(0, "ack socket"); + return clib_error_return_unix (0, "ack socket"); msm->ack_udp_port = find_and_bind_to_free_port (msm->ack_socket, port++); if (ioctl (msm->ack_socket, FIONBIO, &one) < 0) return clib_error_return_unix (0, "ack socket FIONBIO"); - msm->catchup_server_socket = socket(AF_INET, SOCK_STREAM, 0); + msm->catchup_server_socket = socket (AF_INET, SOCK_STREAM, 0); if (msm->catchup_server_socket < 0) return clib_error_return_unix (0, "catchup server socket"); - - msm->catchup_tcp_port = find_and_bind_to_free_port (msm->catchup_server_socket, port++); + + msm->catchup_tcp_port = + find_and_bind_to_free_port (msm->catchup_server_socket, port++); if (ioctl (msm->catchup_server_socket, FIONBIO, &one) < 0) return clib_error_return_unix (0, "catchup server socket FIONBIO"); - if (listen(msm->catchup_server_socket, 5) < 0) + if (listen (msm->catchup_server_socket, 5) < 0) return clib_error_return_unix (0, "catchup server socket listen"); - + /* epoll setup for multicast mastership socket */ { - unix_file_t template = {0}; + unix_file_t template = { 0 }; template.read_function = mastership_socket_read_ready; - template.file_descriptor = msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP].socket; + template.file_descriptor = + msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP].socket; template.private_data = (uword) msm; unix_file_add (&unix_main, &template); /* epoll setup for multicast to_relay socket */ template.read_function = to_relay_socket_read_ready; - template.file_descriptor = msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY].socket; + template.file_descriptor = + msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY].socket; template.private_data = (uword) msm; unix_file_add (&unix_main, &template); /* epoll setup for multicast from_relay socket */ template.read_function = from_relay_socket_read_ready; - template.file_descriptor = msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY].socket; + template.file_descriptor = + msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY].socket; template.private_data = (uword) msm; unix_file_add (&unix_main, &template); template.read_function = join_socket_read_ready; - template.file_descriptor = msm->multicast_sockets[MC_TRANSPORT_JOIN].socket; + template.file_descriptor = + msm->multicast_sockets[MC_TRANSPORT_JOIN].socket; template.private_data = (uword) msm; unix_file_add (&unix_main, &template); @@ -766,11 +817,12 @@ socket_setup (mc_socket_main_t *msm) } static void * -catchup_add_pending_output (mc_socket_catchup_t * c, uword n_bytes, u8 * set_output_vector) +catchup_add_pending_output (mc_socket_catchup_t * c, uword n_bytes, + u8 * set_output_vector) { - unix_file_t * uf = pool_elt_at_index (unix_main.file_pool, - c->unix_file_index); - u8 * result=0; + unix_file_t *uf = pool_elt_at_index (unix_main.file_pool, + c->unix_file_index); + u8 *result = 0; if (set_output_vector) c->output_vector = set_output_vector; @@ -780,19 +832,19 @@ catchup_add_pending_output (mc_socket_catchup_t * c, uword n_bytes, u8 * set_out { int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; - if (! skip_update) + if (!skip_update) unix_main.file_update (uf, UNIX_FILE_UPDATE_MODIFY); } return result; } -static uword catchup_request_fun (void *transport_main, - u32 stream_index, - mc_peer_id_t catchup_peer_id) +static uword +catchup_request_fun (void *transport_main, + u32 stream_index, mc_peer_id_t catchup_peer_id) { - mc_socket_main_t *msm = (mc_socket_main_t *)transport_main; - mc_main_t * mcm = &msm->mc_main; - vlib_main_t * vm = mcm->vlib_main; + mc_socket_main_t *msm = (mc_socket_main_t *) transport_main; + mc_main_t *mcm = &msm->mc_main; + vlib_main_t *vm = mcm->vlib_main; mc_socket_catchup_t *c; struct sockaddr_in addr; unix_main_t *um = &unix_main; @@ -801,7 +853,7 @@ static uword catchup_request_fun (void *transport_main, pool_get (msm->catchups, c); memset (c, 0, sizeof (*c)); - c->socket = socket(AF_INET, SOCK_STREAM, 0); + c->socket = socket (AF_INET, SOCK_STREAM, 0); if (c->socket < 0) { clib_unix_warning ("socket"); @@ -814,35 +866,37 @@ static uword catchup_request_fun (void *transport_main, return 0; } - memset(&addr, 0, sizeof(addr)); + memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = mc_socket_peer_id_get_address (catchup_peer_id); addr.sin_port = mc_socket_peer_id_get_port (catchup_peer_id); - + c->connect_in_progress = 1; if (MC_EVENT_LOGGING) { - ELOG_TYPE_DECLARE (e) = { - .format = "connecting to peer 0x%Lx", - .format_args = "i8", - }; - struct { u64 peer; } * ed; + ELOG_TYPE_DECLARE (e) = + { + .format = "connecting to peer 0x%Lx",.format_args = "i8",}; + struct + { + u64 peer; + } *ed; ed = ELOG_DATA (&vm->elog_main, e); ed->peer = catchup_peer_id.as_u64; } - - if (connect(c->socket, (const void *)&addr,sizeof(addr)) + + if (connect (c->socket, (const void *) &addr, sizeof (addr)) < 0 && errno != EINPROGRESS) { clib_unix_warning ("connect to %U fails", format_socket_peer_id, catchup_peer_id); return 0; } - + { - unix_file_t template = {0}; - + unix_file_t template = { 0 }; + template.read_function = catchup_client_read_ready; template.write_function = catchup_client_write_ready; template.error_function = catchup_socket_error_ready; @@ -850,13 +904,15 @@ static uword catchup_request_fun (void *transport_main, template.private_data = (uword) msm; c->unix_file_index = unix_file_add (um, &template); - hash_set (msm->catchup_index_by_file_descriptor, c->socket, c - msm->catchups); + hash_set (msm->catchup_index_by_file_descriptor, c->socket, + c - msm->catchups); } { - mc_msg_catchup_request_t * mp; - mp = catchup_add_pending_output (c, sizeof (mp[0]), /* set_output_vector */ 0); - mp->peer_id = msm->mc_main.transport.our_catchup_peer_id; + mc_msg_catchup_request_t *mp; + mp = catchup_add_pending_output (c, sizeof (mp[0]), /* set_output_vector */ + 0); + mp->peer_id = msm->mc_main.transport.our_catchup_peer_id; mp->stream_index = stream_index; mc_byte_swap_msg_catchup_request (mp); } @@ -864,33 +920,36 @@ static uword catchup_request_fun (void *transport_main, return c - msm->catchups; } -static void catchup_send_fun (void *transport_main, uword opaque, u8 * data) +static void +catchup_send_fun (void *transport_main, uword opaque, u8 * data) { - mc_socket_main_t *msm = (mc_socket_main_t *)transport_main; + mc_socket_main_t *msm = (mc_socket_main_t *) transport_main; mc_socket_catchup_t *c = pool_elt_at_index (msm->catchups, opaque); catchup_add_pending_output (c, 0, data); } static int -find_interface_ip4_address (char * if_name, u32 * ip4_address, u32 * mtu) +find_interface_ip4_address (char *if_name, u32 * ip4_address, u32 * mtu) { int fd; struct ifreq ifr; - struct sockaddr_in * sa; + struct sockaddr_in *sa; /* Dig up our IP address */ fd = socket (PF_INET, AF_INET, 0); - if (fd < 0) { - clib_unix_error ("socket"); - return -1; - } + if (fd < 0) + { + clib_unix_error ("socket"); + return -1; + } ifr.ifr_addr.sa_family = AF_INET; - strncpy (ifr.ifr_name, if_name, sizeof(ifr.ifr_name)-1); - if (ioctl (fd, SIOCGIFADDR, &ifr) < 0) { - clib_unix_error ("ioctl(SIOCFIGADDR)"); - return -1; - } + strncpy (ifr.ifr_name, if_name, sizeof (ifr.ifr_name) - 1); + if (ioctl (fd, SIOCGIFADDR, &ifr) < 0) + { + clib_unix_error ("ioctl(SIOCFIGADDR)"); + return -1; + } sa = (void *) &ifr.ifr_addr; clib_memcpy (ip4_address, &sa->sin_addr.s_addr, sizeof (ip4_address[0])); @@ -898,7 +957,7 @@ find_interface_ip4_address (char * if_name, u32 * ip4_address, u32 * mtu) if (ioctl (fd, SIOCGIFMTU, &ifr) < 0) return -1; if (mtu) - *mtu = ifr.ifr_mtu - (/* IP4 header */ 20 + /* UDP header */ 8); + *mtu = ifr.ifr_mtu - ( /* IP4 header */ 20 + /* UDP header */ 8); close (fd); @@ -907,16 +966,16 @@ find_interface_ip4_address (char * if_name, u32 * ip4_address, u32 * mtu) clib_error_t * mc_socket_main_init (mc_socket_main_t * msm, char **intfc_probe_list, - int n_intfcs_to_probe) + int n_intfcs_to_probe) { - clib_error_t * error; - mc_main_t * mcm; + clib_error_t *error; + mc_main_t *mcm; u32 mtu; mcm = &msm->mc_main; /* 239.255.0.7 */ - if (! msm->multicast_tx_ip4_address_host_byte_order) + if (!msm->multicast_tx_ip4_address_host_byte_order) msm->multicast_tx_ip4_address_host_byte_order = 0xefff0007; { @@ -925,12 +984,14 @@ mc_socket_main_init (mc_socket_main_t * msm, char **intfc_probe_list, win = 0; if (msm->multicast_interface_name) { - win = ! find_interface_ip4_address (msm->multicast_interface_name, &a, &mtu); + win = + !find_interface_ip4_address (msm->multicast_interface_name, &a, + &mtu); } else { for (i = 0; i < n_intfcs_to_probe; i++) - if (! find_interface_ip4_address (intfc_probe_list[i], &a, &mtu)) + if (!find_interface_ip4_address (intfc_probe_list[i], &a, &mtu)) { win = 1; msm->multicast_interface_name = intfc_probe_list[i]; @@ -938,25 +999,29 @@ mc_socket_main_init (mc_socket_main_t * msm, char **intfc_probe_list, } } - if (! win) + if (!win) return clib_error_return (0, "can't find interface ip4 address"); msm->if_ip4_address_net_byte_order = a; } msm->rx_mtu_n_bytes = mtu; - msm->rx_mtu_n_buffers = msm->rx_mtu_n_bytes / VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES; - msm->rx_mtu_n_buffers += (msm->rx_mtu_n_bytes % VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES) != 0; + msm->rx_mtu_n_buffers = + msm->rx_mtu_n_bytes / VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES; + msm->rx_mtu_n_buffers += + (msm->rx_mtu_n_bytes % VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES) != 0; error = socket_setup (msm); if (error) return error; mcm->transport.our_ack_peer_id = - mc_socket_set_peer_id (msm->if_ip4_address_net_byte_order, msm->ack_udp_port); + mc_socket_set_peer_id (msm->if_ip4_address_net_byte_order, + msm->ack_udp_port); mcm->transport.our_catchup_peer_id = - mc_socket_set_peer_id (msm->if_ip4_address_net_byte_order, msm->catchup_tcp_port); + mc_socket_set_peer_id (msm->if_ip4_address_net_byte_order, + msm->catchup_tcp_port); mcm->transport.tx_buffer = tx_buffer; mcm->transport.tx_ack = tx_ack; @@ -970,3 +1035,11 @@ mc_socket_main_init (mc_socket_main_t * msm, char **intfc_probe_list, return error; } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/mc_socket.h b/vlib/vlib/unix/mc_socket.h index 7dd6b5e27b1..273c9ad430b 100644 --- a/vlib/vlib/unix/mc_socket.h +++ b/vlib/vlib/unix/mc_socket.h @@ -21,24 +21,27 @@ #include <vlib/unix/unix.h> #include <netinet/in.h> -typedef struct { +typedef struct +{ int socket; struct sockaddr_in tx_addr; } mc_multicast_socket_t; /* TCP catchup socket */ -typedef struct { +typedef struct +{ int socket; u32 unix_file_index; - u8 * input_vector; - u8 * output_vector; + u8 *input_vector; + u8 *output_vector; u32 output_vector_n_written; u32 connect_in_progress; } mc_socket_catchup_t; -typedef struct mc_socket_main_t { +typedef struct mc_socket_main_t +{ mc_main_t mc_main; /* Multicast mastership/to-relay/from-relay sockets. */ @@ -53,7 +56,7 @@ typedef struct mc_socket_main_t { /* Pool of stream-private catchup sockets */ mc_socket_catchup_t *catchups; - uword * catchup_index_by_file_descriptor; + uword *catchup_index_by_file_descriptor; u32 rx_mtu_n_bytes; @@ -61,19 +64,19 @@ typedef struct mc_socket_main_t { u32 rx_mtu_n_buffers; /* Vector of RX VLIB buffers. */ - u32 * rx_buffers; + u32 *rx_buffers; /* Vector of scatter/gather descriptors for sending/receiving VLIB buffers via kernel. */ - struct iovec * iovecs; + struct iovec *iovecs; /* IP address of interface to use for multicast. */ u32 if_ip4_address_net_byte_order; - + u32 ack_udp_port; u32 catchup_tcp_port; /* Interface on which to listen for multicasts. */ - char * multicast_interface_name; + char *multicast_interface_name; /* Multicast address to use (e.g. 0xefff0000). Host byte order. */ @@ -92,15 +95,15 @@ always_inline u32 mc_socket_peer_id_get_address (mc_peer_id_t i) { u32 a = ((i.as_u8[0] << 24) - | (i.as_u8[1] << 16) - | (i.as_u8[2] << 8) - | (i.as_u8[3] << 0)); + | (i.as_u8[1] << 16) | (i.as_u8[2] << 8) | (i.as_u8[3] << 0)); return clib_host_to_net_u32 (a); } always_inline u32 mc_socket_peer_id_get_port (mc_peer_id_t i) -{ return clib_host_to_net_u16 ((i.as_u8[4] << 8) | i.as_u8[5]); } +{ + return clib_host_to_net_u16 ((i.as_u8[4] << 8) | i.as_u8[5]); +} static_always_inline mc_peer_id_t mc_socket_set_peer_id (u32 address_net_byte_order, u32 port_host_byte_order) @@ -110,17 +113,25 @@ mc_socket_set_peer_id (u32 address_net_byte_order, u32 port_host_byte_order) u32 p = port_host_byte_order; i.as_u8[0] = (a >> 24) & 0xff; i.as_u8[1] = (a >> 16) & 0xff; - i.as_u8[2] = (a >> 8) & 0xff; - i.as_u8[3] = (a >> 0) & 0xff; - i.as_u8[4] = (p >> 8) & 0xff; - i.as_u8[5] = (p >> 0) & 0xff; + i.as_u8[2] = (a >> 8) & 0xff; + i.as_u8[3] = (a >> 0) & 0xff; + i.as_u8[4] = (p >> 8) & 0xff; + i.as_u8[5] = (p >> 0) & 0xff; i.as_u8[6] = 0; i.as_u8[7] = 0; return i; } -clib_error_t * -mc_socket_main_init (mc_socket_main_t * msm, char **intfc_probe_list, - int n_intfcs_to_probe); +clib_error_t *mc_socket_main_init (mc_socket_main_t * msm, + char **intfc_probe_list, + int n_intfcs_to_probe); #endif /* __included_mc_socket_h__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/physmem.c b/vlib/vlib/unix/physmem.c index d3155e0c7c9..80ab7b9d6f8 100644 --- a/vlib/vlib/unix/physmem.c +++ b/vlib/vlib/unix/physmem.c @@ -42,11 +42,12 @@ static physmem_main_t physmem_main; static void * -unix_physmem_alloc_aligned (vlib_physmem_main_t * vpm, uword n_bytes, uword alignment) +unix_physmem_alloc_aligned (vlib_physmem_main_t * vpm, uword n_bytes, + uword alignment) { - physmem_main_t * pm = &physmem_main; + physmem_main_t *pm = &physmem_main; uword lo_offset, hi_offset; - uword * to_free = 0; + uword *to_free = 0; #if DPDK > 0 clib_warning ("unsafe alloc!"); @@ -74,7 +75,7 @@ unix_physmem_alloc_aligned (vlib_physmem_main_t * vpm, uword n_bytes, uword alig break; /* Allocation would span chunk boundary, queue it to be freed as soon as - we find suitable chunk. */ + we find suitable chunk. */ vec_add1 (to_free, lo_offset); } @@ -89,43 +90,46 @@ unix_physmem_alloc_aligned (vlib_physmem_main_t * vpm, uword n_bytes, uword alig return lo_offset != ~0 ? pm->heap + lo_offset : 0; } -static void unix_physmem_free (void * x) +static void +unix_physmem_free (void *x) { - physmem_main_t * pm = &physmem_main; + physmem_main_t *pm = &physmem_main; /* Return object to region's heap. */ mheap_put (pm->heap, x - pm->heap); } -static void htlb_shutdown(void) +static void +htlb_shutdown (void) { - physmem_main_t * pm = &physmem_main; - - if (! pm->shmid) + physmem_main_t *pm = &physmem_main; + + if (!pm->shmid) return; shmctl (pm->shmid, IPC_RMID, 0); pm->shmid = 0; } /* try to use huge TLB pgs if possible */ -static int htlb_init (vlib_main_t * vm) +static int +htlb_init (vlib_main_t * vm) { - vlib_physmem_main_t * vpm = &vm->physmem_main; - physmem_main_t * pm = &physmem_main; + vlib_physmem_main_t *vpm = &vm->physmem_main; + physmem_main_t *pm = &physmem_main; u64 hugepagesize, pagesize; u64 pfn, seek_loc; u64 cur, physaddr, ptbits; int fd, i; - pm->shmid = shmget (11 /* key, my amp goes to 11 */, pm->mem_size, - IPC_CREAT | SHM_HUGETLB | SHM_R | SHM_W); + pm->shmid = shmget (11 /* key, my amp goes to 11 */ , pm->mem_size, + IPC_CREAT | SHM_HUGETLB | SHM_R | SHM_W); if (pm->shmid < 0) { clib_unix_warning ("shmget"); return 0; } - pm->mem = shmat (pm->shmid, NULL, 0 /* flags */); + pm->mem = shmat (pm->shmid, NULL, 0 /* flags */ ); if (pm->mem == 0) { shmctl (pm->shmid, IPC_RMID, 0); @@ -135,8 +139,8 @@ static int htlb_init (vlib_main_t * vm) memset (pm->mem, 0, pm->mem_size); /* $$$ get page size info from /proc/meminfo */ - hugepagesize = 2<<20; - pagesize = 4<<10; + hugepagesize = 2 << 20; + pagesize = 4 << 10; vpm->log2_n_bytes_per_page = min_log2 (hugepagesize); vec_resize (vpm->page_table, pm->mem_size / hugepagesize); @@ -145,71 +149,72 @@ static int htlb_init (vlib_main_t * vm) vpm->virtual.size = pm->mem_size; vpm->virtual.end = vpm->virtual.start + vpm->virtual.size; - fd = open("/proc/self/pagemap", O_RDONLY); + fd = open ("/proc/self/pagemap", O_RDONLY); - if (fd < 0) + if (fd < 0) { (void) shmdt (pm->mem); return 0; } - - pm->heap = mheap_alloc_with_flags - (pm->mem, pm->mem_size, - /* Don't want mheap mmap/munmap with IO memory. */ - MHEAP_FLAG_DISABLE_VM); - - cur = pointer_to_uword(pm->mem); + + pm->heap = mheap_alloc_with_flags (pm->mem, pm->mem_size, + /* Don't want mheap mmap/munmap with IO memory. */ + MHEAP_FLAG_DISABLE_VM); + + cur = pointer_to_uword (pm->mem); i = 0; - while (cur < pointer_to_uword(pm->mem) + pm->mem_size) + while (cur < pointer_to_uword (pm->mem) + pm->mem_size) { pfn = (u64) cur / pagesize; seek_loc = pfn * sizeof (u64); if (lseek (fd, seek_loc, SEEK_SET) != seek_loc) - { - clib_unix_warning ("lseek to 0x%llx", seek_loc); - shmctl (pm->shmid, IPC_RMID, 0); - close(fd); - return 0; - } - if (read (fd, &ptbits, sizeof (ptbits)) != (sizeof(ptbits))) - { - clib_unix_warning ("read ptbits"); - shmctl (pm->shmid, IPC_RMID, 0); - close(fd); - return 0; - } - + { + clib_unix_warning ("lseek to 0x%llx", seek_loc); + shmctl (pm->shmid, IPC_RMID, 0); + close (fd); + return 0; + } + if (read (fd, &ptbits, sizeof (ptbits)) != (sizeof (ptbits))) + { + clib_unix_warning ("read ptbits"); + shmctl (pm->shmid, IPC_RMID, 0); + close (fd); + return 0; + } + /* bits 0-54 are the physical page number */ physaddr = (ptbits & 0x7fffffffffffffULL) * pagesize; if (CLIB_DEBUG > 1) - fformat(stderr, "pm: virtual 0x%llx physical 0x%llx\n", - cur, physaddr); + fformat (stderr, "pm: virtual 0x%llx physical 0x%llx\n", + cur, physaddr); vpm->page_table[i++] = physaddr; cur += hugepagesize; } - close(fd); + close (fd); atexit (htlb_shutdown); return 1; } -int vlib_app_physmem_init (vlib_main_t * vm, - physmem_main_t * pm, int) __attribute__ ((weak)); -int vlib_app_physmem_init (vlib_main_t * vm, physmem_main_t * pm, int x) +int vlib_app_physmem_init (vlib_main_t * vm, + physmem_main_t * pm, int) __attribute__ ((weak)); +int +vlib_app_physmem_init (vlib_main_t * vm, physmem_main_t * pm, int x) { return 0; } -clib_error_t * unix_physmem_init (vlib_main_t * vm, int physical_memory_required) +clib_error_t * +unix_physmem_init (vlib_main_t * vm, int physical_memory_required) { - vlib_physmem_main_t * vpm = &vm->physmem_main; - physmem_main_t * pm = &physmem_main; - clib_error_t * error = 0; + vlib_physmem_main_t *vpm = &vm->physmem_main; + physmem_main_t *pm = &physmem_main; + clib_error_t *error = 0; /* Avoid multiple calls. */ if (vm->os_physmem_alloc_aligned) - return error; + return error; vm->os_physmem_alloc_aligned = unix_physmem_alloc_aligned; vm->os_physmem_free = unix_physmem_free; @@ -220,15 +225,17 @@ clib_error_t * unix_physmem_init (vlib_main_t * vm, int physical_memory_required /* OK, Mr. App, you tell us */ if (vlib_app_physmem_init (vm, pm, physical_memory_required)) - return 0; + return 0; - if (!pm->no_hugepages && htlb_init(vm)) + if (!pm->no_hugepages && htlb_init (vm)) { - fformat(stderr, "%s: use huge pages\n", __FUNCTION__); + fformat (stderr, "%s: use huge pages\n", __FUNCTION__); return 0; } - pm->mem = mmap (0, pm->mem_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + pm->mem = + mmap (0, pm->mem_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (pm->mem == MAP_FAILED) { error = clib_error_return_unix (0, "mmap"); @@ -247,9 +254,9 @@ clib_error_t * unix_physmem_init (vlib_main_t * vm, int physical_memory_required vpm->virtual.end = vpm->virtual.start + vpm->virtual.size; vpm->is_fake = 1; - fformat(stderr, "%s: use fake dma pages\n", __FUNCTION__); + fformat (stderr, "%s: use fake dma pages\n", __FUNCTION__); - done: +done: if (error) { if (pm->mem != MAP_FAILED) @@ -260,32 +267,32 @@ clib_error_t * unix_physmem_init (vlib_main_t * vm, int physical_memory_required static clib_error_t * show_physmem (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { #if DPDK > 0 - vlib_cli_output (vm, "Not supported with DPDK drivers."); + vlib_cli_output (vm, "Not supported with DPDK drivers."); #else - physmem_main_t * pm = &physmem_main; + physmem_main_t *pm = &physmem_main; if (pm->heap) - vlib_cli_output (vm, "%U", format_mheap, pm->heap, /* verbose */ 1); + vlib_cli_output (vm, "%U", format_mheap, pm->heap, /* verbose */ 1); else - vlib_cli_output (vm, "No physmem allocated."); + vlib_cli_output (vm, "No physmem allocated."); #endif return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_physmem_command, static) = { .path = "show physmem", .short_help = "Show physical memory allocation", .function = show_physmem, }; +/* *INDENT-ON* */ static clib_error_t * show_affinity (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { cpu_set_t set; cpu_set_t *setp = &set; @@ -295,66 +302,67 @@ show_affinity (vlib_main_t * vm, int last_set_bit_in_run = -1; int output_done = 0; - rv = sched_getaffinity (0 /* pid, 0 = this proc */, - sizeof (*setp), setp); + rv = sched_getaffinity (0 /* pid, 0 = this proc */ , + sizeof (*setp), setp); if (rv < 0) { vlib_cli_output (vm, "Couldn't get affinity mask: %s\n", - strerror(errno)); + strerror (errno)); return 0; } - + for (i = 0; i < 64; i++) { - if (CPU_ISSET(i, setp)) - { - if (first_set_bit_in_run == -1) - { - first_set_bit_in_run = i; - last_set_bit_in_run = i; - if (output_done) - s = format (s, ","); - s = format (s, "%d-", i); - output_done = 1; - } - else - { - if (i == (last_set_bit_in_run+1)) - last_set_bit_in_run = i; - } - } + if (CPU_ISSET (i, setp)) + { + if (first_set_bit_in_run == -1) + { + first_set_bit_in_run = i; + last_set_bit_in_run = i; + if (output_done) + s = format (s, ","); + s = format (s, "%d-", i); + output_done = 1; + } + else + { + if (i == (last_set_bit_in_run + 1)) + last_set_bit_in_run = i; + } + } else - { - if (first_set_bit_in_run != -1) - { - if (first_set_bit_in_run == (i-1)) - { - _vec_len (s) -= 2 + ((first_set_bit_in_run/10)); - } - s = format (s, "%d", last_set_bit_in_run); - first_set_bit_in_run = -1; - last_set_bit_in_run = -1; - } - } + { + if (first_set_bit_in_run != -1) + { + if (first_set_bit_in_run == (i - 1)) + { + _vec_len (s) -= 2 + ((first_set_bit_in_run / 10)); + } + s = format (s, "%d", last_set_bit_in_run); + first_set_bit_in_run = -1; + last_set_bit_in_run = -1; + } + } } - - if (first_set_bit_in_run != -1) + + if (first_set_bit_in_run != -1) s = format (s, "%d", first_set_bit_in_run); vlib_cli_output (vm, "Process runs on: %v", s); return 0; } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_affinity_command, static) = { .path = "show affinity", .short_help = "Show process cpu affinity", .function = show_affinity, }; +/* *INDENT-ON* */ static clib_error_t * set_affinity (vlib_main_t * vm, - unformat_input_t * input, - vlib_cli_command_t * cmd) + unformat_input_t * input, vlib_cli_command_t * cmd) { cpu_set_t set; cpu_set_t *setp = &set; @@ -364,81 +372,85 @@ set_affinity (vlib_main_t * vm, memset (setp, 0, sizeof (*setp)); - do { - another_round = 0; - if (unformat (input, "%d-%d,", &first, &last)) - { - if (first > 64 || last > 64) - { - barf1: - vlib_cli_output (vm, "range %d-%d invalid", first, last); - return 0; - } - - for (i = first; i <= last; i++) - CPU_SET(i, setp); - another_round = 1; - } - else if (unformat (input, "%d-%d", &first, &last)) - { - if (first > 64 || last > 64) - goto barf1; - - for (i = first; i <= last; i++) - CPU_SET(i, setp); - } - else if (unformat (input, "%d,", &first)) - { - if (first > 64) - { - barf2: - vlib_cli_output (vm, "cpu %d invalid", first); - return 0; - } - CPU_SET(first, setp); - another_round = 1; - } - else if (unformat (input, "%d", &first)) - { - if (first > 64) - goto barf2; - - CPU_SET(first, setp); - } - } while (another_round); - - rv = sched_setaffinity (0 /* pid, 0 = this proc */, - sizeof (*setp), setp); + do + { + another_round = 0; + if (unformat (input, "%d-%d,", &first, &last)) + { + if (first > 64 || last > 64) + { + barf1: + vlib_cli_output (vm, "range %d-%d invalid", first, last); + return 0; + } + + for (i = first; i <= last; i++) + CPU_SET (i, setp); + another_round = 1; + } + else if (unformat (input, "%d-%d", &first, &last)) + { + if (first > 64 || last > 64) + goto barf1; + + for (i = first; i <= last; i++) + CPU_SET (i, setp); + } + else if (unformat (input, "%d,", &first)) + { + if (first > 64) + { + barf2: + vlib_cli_output (vm, "cpu %d invalid", first); + return 0; + } + CPU_SET (first, setp); + another_round = 1; + } + else if (unformat (input, "%d", &first)) + { + if (first > 64) + goto barf2; + + CPU_SET (first, setp); + } + } + while (another_round); + + rv = sched_setaffinity (0 /* pid, 0 = this proc */ , + sizeof (*setp), setp); if (rv < 0) { vlib_cli_output (vm, "Couldn't get affinity mask: %s\n", - strerror(errno)); + strerror (errno)); return 0; } return show_affinity (vm, input, cmd); } +/* *INDENT-OFF* */ VLIB_CLI_COMMAND (set_affinity_command, static) = { .path = "set affinity", .short_help = "Set process cpu affinity", .function = set_affinity, }; +/* *INDENT-ON* */ static clib_error_t * vlib_physmem_configure (vlib_main_t * vm, unformat_input_t * input) { - physmem_main_t * pm = &physmem_main; + physmem_main_t *pm = &physmem_main; u32 size_in_mb; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "no-huge") || unformat (input, "no-huge-pages")) - pm->no_hugepages = 1; + pm->no_hugepages = 1; - else if (unformat(input, "size-in-mb %d", &size_in_mb) || - unformat(input, "size %d", &size_in_mb)) - pm->mem_size = size_in_mb << 20; + else if (unformat (input, "size-in-mb %d", &size_in_mb) || + unformat (input, "size %d", &size_in_mb)) + pm->mem_size = size_in_mb << 20; else return unformat_parse_error (input); } @@ -448,3 +460,11 @@ vlib_physmem_configure (vlib_main_t * vm, unformat_input_t * input) } VLIB_EARLY_CONFIG_FUNCTION (vlib_physmem_configure, "physmem"); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/physmem.h b/vlib/vlib/unix/physmem.h index adbd3478edb..5519a7d6f3e 100644 --- a/vlib/vlib/unix/physmem.h +++ b/vlib/vlib/unix/physmem.h @@ -16,7 +16,7 @@ #define __included_physmem_h__ /* Manage I/O physical memory. */ -#define _GNU_SOURCE +#define _GNU_SOURCE #include <sched.h> #include <vppinfra/cache.h> #include <vppinfra/error.h> @@ -35,15 +35,16 @@ #include <sys/ipc.h> #include <sys/shm.h> -typedef struct { +typedef struct +{ /* Virtual memory via mmaped. */ - void * mem; + void *mem; /* Size in bytes. */ uword mem_size; /* Heap allocated out of virtual memory. */ - void * heap; + void *heap; /* huge TLB segment id */ int shmid; @@ -54,3 +55,11 @@ typedef struct { } physmem_main_t; #endif /* __included_physmem_h__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/plugin.c b/vlib/vlib/unix/plugin.c index 93bfbb71b7c..1869953d723 100644 --- a/vlib/vlib/unix/plugin.c +++ b/vlib/vlib/unix/plugin.c @@ -21,44 +21,46 @@ plugin_main_t vlib_plugin_main; -void vlib_set_get_handoff_structure_cb (void *cb) +void +vlib_set_get_handoff_structure_cb (void *cb) { - plugin_main_t * pm = &vlib_plugin_main; + plugin_main_t *pm = &vlib_plugin_main; pm->handoff_structure_get_cb = cb; } -static void * vnet_get_handoff_structure (void) +static void * +vnet_get_handoff_structure (void) { - void * (*fp)(void); + void *(*fp) (void); fp = vlib_plugin_main.handoff_structure_get_cb; if (fp == 0) return 0; else - return (*fp)(); + return (*fp) (); } -static int -load_one_plugin (plugin_main_t *pm, plugin_info_t *pi, int from_early_init) +static int +load_one_plugin (plugin_main_t * pm, plugin_info_t * pi, int from_early_init) { void *handle, *register_handle; - clib_error_t * (*fp)(vlib_main_t *, void *, int); - clib_error_t * error; + clib_error_t *(*fp) (vlib_main_t *, void *, int); + clib_error_t *error; void *handoff_structure; - - handle = dlopen ((char *)pi->name, RTLD_LAZY); - /* + handle = dlopen ((char *) pi->name, RTLD_LAZY); + + /* * Note: this can happen if the plugin has an undefined symbol reference, * so print a warning. Otherwise, the poor slob won't know what happened. * Ask me how I know that... */ if (handle == 0) { - clib_warning ("%s", dlerror()); + clib_warning ("%s", dlerror ()); return -1; } - + pi->handle = handle; @@ -66,18 +68,19 @@ load_one_plugin (plugin_main_t *pm, plugin_info_t *pi, int from_early_init) if (register_handle == 0) { dlclose (handle); - clib_warning("Plugin missing vlib_plugin_register: %s\n", (char *)pi->name); + clib_warning ("Plugin missing vlib_plugin_register: %s\n", + (char *) pi->name); return 1; } fp = register_handle; - handoff_structure = vnet_get_handoff_structure(); + handoff_structure = vnet_get_handoff_structure (); if (handoff_structure == 0) error = clib_error_return (0, "handoff structure callback returned 0"); else - error = (*fp)(pm->vlib_main, handoff_structure, from_early_init); + error = (*fp) (pm->vlib_main, handoff_structure, from_early_init); if (error) { @@ -91,7 +94,8 @@ load_one_plugin (plugin_main_t *pm, plugin_info_t *pi, int from_early_init) return 0; } -static u8 **split_plugin_path (plugin_main_t *pm) +static u8 ** +split_plugin_path (plugin_main_t * pm) { int i; u8 **rv = 0; @@ -101,11 +105,11 @@ static u8 **split_plugin_path (plugin_main_t *pm) for (i = 0; i < vec_len (pm->plugin_path); i++) { if (path[i] != ':') - { - vec_add1(this, path[i]); - continue; - } - vec_add1(this, 0); + { + vec_add1 (this, path[i]); + continue; + } + vec_add1 (this, 0); vec_add1 (rv, this); this = 0; } @@ -117,7 +121,8 @@ static u8 **split_plugin_path (plugin_main_t *pm) return rv; } -int vlib_load_new_plugins (plugin_main_t *pm, int from_early_init) +int +vlib_load_new_plugins (plugin_main_t * pm, int from_early_init) { DIR *dp; struct dirent *entry; @@ -128,76 +133,78 @@ int vlib_load_new_plugins (plugin_main_t *pm, int from_early_init) int i; plugin_path = split_plugin_path (pm); - + for (i = 0; i < vec_len (plugin_path); i++) { - dp = opendir ((char *)plugin_path[i]); - + dp = opendir ((char *) plugin_path[i]); + if (dp == 0) - continue; - + continue; + while ((entry = readdir (dp))) - { - u8 *plugin_name; - - if (pm->plugin_name_filter) - { - int j; - for (j = 0; j < vec_len (pm->plugin_name_filter); j++) - if (entry->d_name[j] != pm->plugin_name_filter[j]) - goto next; - } - - plugin_name = format (0, "%s/%s%c", plugin_path[i], - entry->d_name, 0); + { + u8 *plugin_name; + + if (pm->plugin_name_filter) + { + int j; + for (j = 0; j < vec_len (pm->plugin_name_filter); j++) + if (entry->d_name[j] != pm->plugin_name_filter[j]) + goto next; + } + + plugin_name = format (0, "%s/%s%c", plugin_path[i], + entry->d_name, 0); /* Only accept .so */ - char * ext = strrchr((const char *)plugin_name, '.'); - /* unreadable */ - if(!ext || (strcmp(ext, ".so") != 0) || - stat ((char *)plugin_name, &statb) < 0) - { - ignore: - vec_free (plugin_name); - continue; - } - - /* a dir or other things which aren't plugins */ - if (!S_ISREG(statb.st_mode)) - goto ignore; - - p = hash_get_mem (pm->plugin_by_name_hash, plugin_name); - if (p == 0) - { - vec_add2 (pm->plugin_info, pi, 1); - pi->name = plugin_name; - pi->file_info = statb; - - if (load_one_plugin (pm, pi, from_early_init)) - { - vec_free (plugin_name); - _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1; - continue; - } - memset (pi, 0, sizeof (*pi)); - hash_set_mem (pm->plugin_by_name_hash, plugin_name, - pi - pm->plugin_info); - } - next: - ; - } + char *ext = strrchr ((const char *) plugin_name, '.'); + /* unreadable */ + if (!ext || (strcmp (ext, ".so") != 0) || + stat ((char *) plugin_name, &statb) < 0) + { + ignore: + vec_free (plugin_name); + continue; + } + + /* a dir or other things which aren't plugins */ + if (!S_ISREG (statb.st_mode)) + goto ignore; + + p = hash_get_mem (pm->plugin_by_name_hash, plugin_name); + if (p == 0) + { + vec_add2 (pm->plugin_info, pi, 1); + pi->name = plugin_name; + pi->file_info = statb; + + if (load_one_plugin (pm, pi, from_early_init)) + { + vec_free (plugin_name); + _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1; + continue; + } + memset (pi, 0, sizeof (*pi)); + hash_set_mem (pm->plugin_by_name_hash, plugin_name, + pi - pm->plugin_info); + } + next: + ; + } closedir (dp); vec_free (plugin_path[i]); } vec_free (plugin_path); return 0; } -char *vlib_plugin_path __attribute__((weak)); + +char *vlib_plugin_path __attribute__ ((weak)); char *vlib_plugin_path = ""; -char *vlib_plugin_name_filter __attribute__((weak)); +char *vlib_plugin_name_filter __attribute__ ((weak)); char *vlib_plugin_name_filter = 0; -int vlib_plugin_early_init (vlib_main_t *vm) +int +vlib_plugin_early_init (vlib_main_t * vm) { plugin_main_t *pm = &vlib_plugin_main; @@ -210,6 +217,14 @@ int vlib_plugin_early_init (vlib_main_t *vm) pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword)); pm->vlib_main = vm; - - return vlib_load_new_plugins (pm, 1 /* from_early_init */); + + return vlib_load_new_plugins (pm, 1 /* from_early_init */ ); } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/plugin.h b/vlib/vlib/unix/plugin.h index e7d75099ed9..e905263f232 100644 --- a/vlib/vlib/unix/plugin.h +++ b/vlib/vlib/unix/plugin.h @@ -28,12 +28,12 @@ * vlib plugin scheme * * Almost anything which can be made to work in a vlib unix - * application will also work in a vlib plugin. - * - * The elf-section magic which registers static objects + * application will also work in a vlib plugin. + * + * The elf-section magic which registers static objects * works so long as plugins are preset when the vlib unix process - * starts. But wait: there's more... - * + * starts. But wait: there's more... + * * If an application calls vlib_load_new_plugins() -- possibly after * changing vlib_plugin_main.plugin_path / vlib_plugin_main.plugin_name_filter, * -- new plugins will be loaded. That, in turn, allows considerable @@ -55,16 +55,18 @@ * An application can change these parameters at any time and call * vlib_load_new_plugins(). */ - -typedef struct { + +typedef struct +{ u8 *name; struct stat file_info; void *handle; } plugin_info_t; -typedef struct { +typedef struct +{ /* loaded plugin info */ plugin_info_t *plugin_info; uword *plugin_by_name_hash; @@ -82,7 +84,15 @@ typedef struct { plugin_main_t vlib_plugin_main; -int vlib_plugin_early_init (vlib_main_t *vm); -int vlib_load_new_plugins (plugin_main_t *pm, int from_early_init); +int vlib_plugin_early_init (vlib_main_t * vm); +int vlib_load_new_plugins (plugin_main_t * pm, int from_early_init); #endif /* __included_plugin_h__ */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/unix.h b/vlib/vlib/unix/unix.h index 6c1f985aaba..510e3f196c5 100644 --- a/vlib/vlib/unix/unix.h +++ b/vlib/vlib/unix/unix.h @@ -44,9 +44,10 @@ #include <termios.h> struct unix_file; -typedef clib_error_t * (unix_file_function_t) (struct unix_file * f); +typedef clib_error_t *(unix_file_function_t) (struct unix_file * f); -typedef struct unix_file { +typedef struct unix_file +{ /* Unix file descriptor from open/socket. */ u32 file_descriptor; @@ -58,23 +59,26 @@ typedef struct unix_file { uword private_data; /* Functions to be called when read/write data becomes ready. */ - unix_file_function_t * read_function, * write_function, * error_function; + unix_file_function_t *read_function, *write_function, *error_function; } unix_file_t; -typedef struct { +typedef struct +{ f64 time; - clib_error_t * error; + clib_error_t *error; } unix_error_history_t; -typedef enum { +typedef enum +{ UNIX_FILE_UPDATE_ADD, UNIX_FILE_UPDATE_MODIFY, UNIX_FILE_UPDATE_DELETE, } unix_file_update_type_t; -typedef struct { +typedef struct +{ /* Back pointer to main structure. */ - vlib_main_t * vlib_main; + vlib_main_t *vlib_main; u32 flags; /* Run interactively or as daemon (background process). */ @@ -82,12 +86,13 @@ typedef struct { #define UNIX_FLAG_NODAEMON (1 << 1) /* Pool of files to poll for input/output. */ - unix_file_t * file_pool; + unix_file_t *file_pool; /* CLI listen socket. */ clib_socket_t cli_listen_socket; - void (* file_update) (unix_file_t * file, unix_file_update_type_t update_type); + void (*file_update) (unix_file_t * file, + unix_file_update_type_t update_type); /* Circular buffer of last unix errors. */ unix_error_history_t error_history[128]; @@ -130,7 +135,7 @@ extern unix_main_t unix_main; always_inline uword unix_file_add (unix_main_t * um, unix_file_t * template) { - unix_file_t * f; + unix_file_t *f; pool_get (um->file_pool, f); f[0] = template[0]; um->file_update (f, UNIX_FILE_UPDATE_ADD); @@ -147,9 +152,10 @@ unix_file_del (unix_main_t * um, unix_file_t * f) } always_inline uword -unix_file_set_data_available_to_write (u32 unix_file_index, uword is_available) +unix_file_set_data_available_to_write (u32 unix_file_index, + uword is_available) { - unix_file_t * uf = pool_elt_at_index (unix_main.file_pool, unix_file_index); + unix_file_t *uf = pool_elt_at_index (unix_main.file_pool, unix_file_index); uword was_available = (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); if ((was_available != 0) != (is_available != 0)) { @@ -162,7 +168,7 @@ unix_file_set_data_available_to_write (u32 unix_file_index, uword is_available) always_inline void unix_save_error (unix_main_t * um, clib_error_t * error) { - unix_error_history_t * eh = um->error_history + um->error_history_index; + unix_error_history_t *eh = um->error_history + um->error_history_index; clib_error_free_vector (eh->error); eh->error = error; eh->time = vlib_time_now (um->vlib_main); @@ -172,25 +178,26 @@ unix_save_error (unix_main_t * um, clib_error_t * error) } /* Main function for Unix VLIB. */ -int vlib_unix_main (int argc, char * argv[]); +int vlib_unix_main (int argc, char *argv[]); /* Call to allocate/initialize physical DMA memory subsystem. This is not an init function so that users can explicitly enable/disable physmem when its not needed. */ -clib_error_t * unix_physmem_init (vlib_main_t * vm, - int fail_if_physical_memory_not_present); +clib_error_t *unix_physmem_init (vlib_main_t * vm, + int fail_if_physical_memory_not_present); static inline int unix_physmem_is_fake (vlib_main_t * vm) { - vlib_physmem_main_t * vpm = &vm->physmem_main; + vlib_physmem_main_t *vpm = &vm->physmem_main; return vpm->is_fake; } /* Set prompt for CLI. */ -void vlib_unix_cli_set_prompt (char * prompt); +void vlib_unix_cli_set_prompt (char *prompt); -static inline unix_main_t * vlib_unix_get_main (void) +static inline unix_main_t * +vlib_unix_get_main (void) { return &unix_main; } @@ -200,19 +207,24 @@ u8 **vlib_thread_stacks; /* utils */ -clib_error_t * -vlib_sysfs_write (char * file_name, char * fmt, ...); +clib_error_t *vlib_sysfs_write (char *file_name, char *fmt, ...); -clib_error_t * -vlib_sysfs_read (char * file_name, char * fmt, ...); +clib_error_t *vlib_sysfs_read (char *file_name, char *fmt, ...); -u8 * -vlib_sysfs_link_to_name(char * link); +u8 *vlib_sysfs_link_to_name (char *link); -clib_error_t * -foreach_directory_file (char * dir_name, - clib_error_t * (* f) (void * arg, u8 * path_name, - u8 * file_name), - void * arg, int scan_dirs); +clib_error_t *foreach_directory_file (char *dir_name, + clib_error_t * (*f) (void *arg, + u8 * path_name, + u8 * file_name), + void *arg, int scan_dirs); #endif /* included_unix_unix_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/vlib/vlib/unix/util.c b/vlib/vlib/unix/util.c index 7a3a2bf9c47..f4a2c810dfc 100644 --- a/vlib/vlib/unix/util.c +++ b/vlib/vlib/unix/util.c @@ -46,21 +46,21 @@ #include <dirent.h> clib_error_t * -foreach_directory_file (char * dir_name, - clib_error_t * (* f) (void * arg, u8 * path_name, u8 * file_name), - void * arg, +foreach_directory_file (char *dir_name, + clib_error_t * (*f) (void *arg, u8 * path_name, + u8 * file_name), void *arg, int scan_dirs) { - DIR * d; - struct dirent * e; - clib_error_t * error = 0; - u8 * s, * t; + DIR *d; + struct dirent *e; + clib_error_t *error = 0; + u8 *s, *t; d = opendir (dir_name); - if (! d) + if (!d) { if (errno == ENOENT) - return 0; + return 0; return clib_error_return_unix (0, "open `%s'", dir_name); } @@ -68,13 +68,12 @@ foreach_directory_file (char * dir_name, while (1) { e = readdir (d); - if (! e) + if (!e) break; if (scan_dirs) { if (e->d_type == DT_DIR - && (! strcmp (e->d_name, ".") - || ! strcmp (e->d_name, ".."))) + && (!strcmp (e->d_name, ".") || !strcmp (e->d_name, ".."))) continue; } else @@ -100,9 +99,9 @@ foreach_directory_file (char * dir_name, } clib_error_t * -vlib_sysfs_write (char * file_name, char * fmt, ...) +vlib_sysfs_write (char *file_name, char *fmt, ...) { - u8 * s; + u8 *s; int fd; fd = open (file_name, O_WRONLY); @@ -123,10 +122,10 @@ vlib_sysfs_write (char * file_name, char * fmt, ...) } clib_error_t * -vlib_sysfs_read (char * file_name, char * fmt, ...) +vlib_sysfs_read (char *file_name, char *fmt, ...) { unformat_input_t input; - u8 * s = 0; + u8 *s = 0; int fd; ssize_t sz; uword result; @@ -135,18 +134,18 @@ vlib_sysfs_read (char * file_name, char * fmt, ...) if (fd < 0) return clib_error_return_unix (0, "open `%s'", file_name); - vec_validate(s, 4095); + vec_validate (s, 4095); - sz = read(fd, s, vec_len (s)); + sz = read (fd, s, vec_len (s)); if (sz < 0) { - close(fd); - vec_free(s); + close (fd); + vec_free (s); return clib_error_return_unix (0, "read `%s'", file_name); } - _vec_len(s) = sz; - unformat_init_vector(&input, s); + _vec_len (s) = sz; + unformat_init_vector (&input, s); va_list va; va_start (va, fmt); @@ -163,27 +162,35 @@ vlib_sysfs_read (char * file_name, char * fmt, ...) } u8 * -vlib_sysfs_link_to_name(char * link) +vlib_sysfs_link_to_name (char *link) { char *p, buffer[64]; unformat_input_t in; u8 *s = 0; int r; - r = readlink(link, buffer, sizeof(buffer) - 1); + r = readlink (link, buffer, sizeof (buffer) - 1); if (r < 0) return 0; buffer[r] = 0; - p = strrchr(buffer, '/'); + p = strrchr (buffer, '/'); if (!p) return 0; - unformat_init_string (&in, p+1, strlen (p+1)); - unformat(&in, "%s", &s); + unformat_init_string (&in, p + 1, strlen (p + 1)); + unformat (&in, "%s", &s); unformat_free (&in); return s; } + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |