LCOV - code coverage report
Current view: top level - vlib - format.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 54 101 53.5 %
Date: 2023-07-05 22:20:52 Functions: 7 10 70.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2015 Cisco and/or its affiliates.
       3             :  * Licensed under the Apache License, Version 2.0 (the "License");
       4             :  * you may not use this file except in compliance with the License.
       5             :  * You may obtain a copy of the License at:
       6             :  *
       7             :  *     http://www.apache.org/licenses/LICENSE-2.0
       8             :  *
       9             :  * Unless required by applicable law or agreed to in writing, software
      10             :  * distributed under the License is distributed on an "AS IS" BASIS,
      11             :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             :  * See the License for the specific language governing permissions and
      13             :  * limitations under the License.
      14             :  */
      15             : /*
      16             :  * format.c: generic network formatting/unformating
      17             :  *
      18             :  * Copyright (c) 2008 Eliot Dresselhaus
      19             :  *
      20             :  * Permission is hereby granted, free of charge, to any person obtaining
      21             :  * a copy of this software and associated documentation files (the
      22             :  * "Software"), to deal in the Software without restriction, including
      23             :  * without limitation the rights to use, copy, modify, merge, publish,
      24             :  * distribute, sublicense, and/or sell copies of the Software, and to
      25             :  * permit persons to whom the Software is furnished to do so, subject to
      26             :  * the following conditions:
      27             :  *
      28             :  * The above copyright notice and this permission notice shall be
      29             :  * included in all copies or substantial portions of the Software.
      30             :  *
      31             :  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      32             :  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      33             :  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
      34             :  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      35             :  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
      36             :  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
      37             :  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      38             :  */
      39             : 
      40             : #include <vlib/vlib.h>
      41             : 
      42             : u8 *
      43          28 : format_vlib_rx_tx (u8 * s, va_list * args)
      44             : {
      45          28 :   vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t);
      46             :   char *t;
      47             : 
      48          28 :   switch (r)
      49             :     {
      50          14 :     case VLIB_RX:
      51          14 :       t = "rx";
      52          14 :       break;
      53          14 :     case VLIB_TX:
      54          14 :       t = "tx";
      55          14 :       break;
      56           0 :     default:
      57           0 :       t = "INVALID";
      58           0 :       break;
      59             :     }
      60             : 
      61          28 :   vec_add (s, t, strlen (t));
      62          28 :   return s;
      63             : }
      64             : 
      65             : u8 *
      66           0 : format_vlib_read_write (u8 * s, va_list * args)
      67             : {
      68           0 :   vlib_rx_or_tx_t r = va_arg (*args, vlib_rx_or_tx_t);
      69             :   char *t;
      70             : 
      71           0 :   switch (r)
      72             :     {
      73           0 :     case VLIB_READ:
      74           0 :       t = "read";
      75           0 :       break;
      76           0 :     case VLIB_WRITE:
      77           0 :       t = "write";
      78           0 :       break;
      79           0 :     default:
      80           0 :       t = "INVALID";
      81           0 :       break;
      82             :     }
      83             : 
      84           0 :   vec_add (s, t, strlen (t));
      85           0 :   return s;
      86             : }
      87             : 
      88             : /* Formats buffer data as printable ascii or as hex. */
      89             : u8 *
      90           0 : format_vlib_buffer_data (u8 * s, va_list * args)
      91             : {
      92           0 :   u8 *data = va_arg (*args, u8 *);
      93           0 :   u32 n_data_bytes = va_arg (*args, u32);
      94             :   u32 i, is_printable;
      95             : 
      96           0 :   is_printable = 1;
      97           0 :   for (i = 0; i < n_data_bytes && is_printable; i++)
      98             :     {
      99           0 :       u8 c = data[i];
     100           0 :       if (c < 0x20)
     101           0 :         is_printable = 0;
     102           0 :       else if (c >= 0x7f)
     103           0 :         is_printable = 0;
     104             :     }
     105             : 
     106           0 :   if (is_printable)
     107           0 :     vec_add (s, data, n_data_bytes);
     108             :   else
     109           0 :     s = format (s, "%U", format_hex_bytes, data, n_data_bytes);
     110             : 
     111           0 :   return s;
     112             : }
     113             : 
     114             : /* Enable/on => 1; disable/off => 0. */
     115             : uword
     116          28 : unformat_vlib_enable_disable (unformat_input_t * input, va_list * args)
     117             : {
     118          28 :   int *result = va_arg (*args, int *);
     119             :   int enable;
     120             : 
     121          28 :   if (unformat (input, "enable") || unformat (input, "on"))
     122          14 :     enable = 1;
     123          14 :   else if (unformat (input, "disable") || unformat (input, "off"))
     124          14 :     enable = 0;
     125             :   else
     126           0 :     return 0;
     127             : 
     128          28 :   *result = enable;
     129          28 :   return 1;
     130             : }
     131             : 
     132             : /* rx/tx => VLIB_RX/VLIB_TX. */
     133             : uword
     134           0 : unformat_vlib_rx_tx (unformat_input_t * input, va_list * args)
     135             : {
     136           0 :   int *result = va_arg (*args, int *);
     137           0 :   if (unformat (input, "rx"))
     138           0 :     *result = VLIB_RX;
     139           0 :   else if (unformat (input, "tx"))
     140           0 :     *result = VLIB_TX;
     141             :   else
     142           0 :     return 0;
     143           0 :   return 1;
     144             : }
     145             : 
     146             : /* Parse an int either %d or 0x%x. */
     147             : uword
     148          15 : unformat_vlib_number (unformat_input_t * input, va_list * args)
     149             : {
     150          15 :   int *result = va_arg (*args, int *);
     151             : 
     152          15 :   return (unformat (input, "0x%x", result) || unformat (input, "%d", result));
     153             : }
     154             : 
     155             : /* Parse a-zA-Z0-9_ token and hash to value. */
     156             : uword
     157          27 : unformat_vlib_number_by_name (unformat_input_t * input, va_list * args)
     158             : {
     159          27 :   uword *hash = va_arg (*args, uword *);
     160          27 :   int *result = va_arg (*args, int *);
     161             :   uword *p;
     162             :   u8 *token;
     163             :   int i;
     164             : 
     165          27 :   if (!unformat_user (input, unformat_token, "a-zA-Z0-9_", &token))
     166           5 :     return 0;
     167             : 
     168             :   /* Null terminate. */
     169          22 :   if (vec_len (token) > 0 && token[vec_len (token) - 1] != 0)
     170          22 :     vec_add1 (token, 0);
     171             : 
     172             :   /* Check for exact match. */
     173          44 :   p = hash_get_mem (hash, token);
     174          22 :   if (p)
     175          12 :     goto done;
     176             : 
     177             :   /* Convert to upper case & try match. */
     178          60 :   for (i = 0; i < vec_len (token); i++)
     179          50 :     if (token[i] >= 'a' && token[i] <= 'z')
     180           0 :       token[i] = 'A' + token[i] - 'a';
     181          20 :   p = hash_get_mem (hash, token);
     182             : 
     183          22 : done:
     184          22 :   vec_free (token);
     185          22 :   if (p)
     186          12 :     *result = p[0];
     187          22 :   return p != 0;
     188             : }
     189             : 
     190             : /* Parse a filename to dump debug info */
     191             : uword
     192           6 : unformat_vlib_tmpfile (unformat_input_t * input, va_list * args)
     193             : {
     194           6 :   u8 **chroot_filename = va_arg (*args, u8 **);
     195             :   u8 *filename;
     196             : 
     197           6 :   if (!unformat (input, "%s", &filename))
     198           0 :     return 0;
     199             : 
     200             :   /* Brain-police user path input */
     201           6 :   if (strstr ((char *) filename, "..") || index ((char *) filename, '/'))
     202             :     {
     203           0 :       vec_free (filename);
     204           0 :       return 0;
     205             :     }
     206             : 
     207           6 :   *chroot_filename = format (0, "/tmp/%s%c", filename, 0);
     208           6 :   vec_free (filename);
     209             : 
     210           6 :   return 1;
     211             : }
     212             : 
     213             : u8 *
     214           6 : format_vlib_thread_name (u8 * s, va_list * args)
     215             : {
     216           6 :   u32 thread_index = va_arg (*args, u32);
     217             : 
     218           6 :   if (thread_index == 0)
     219           6 :     return format (s, "main");
     220             : 
     221           0 :   if (thread_index < vec_len (vlib_worker_threads))
     222           0 :     return format (s, "%s", vlib_worker_threads[thread_index].name);
     223           0 :   return s;
     224             : }
     225             : 
     226             : u8 *
     227           6 : format_vlib_thread_name_and_index (u8 * s, va_list * args)
     228             : {
     229           6 :   u32 thread_index = va_arg (*args, u32);
     230             : 
     231           6 :   return format (s, "%U (%u)", format_vlib_thread_name, thread_index,
     232             :                  thread_index);
     233             : }
     234             : 
     235             : /*
     236             :  * fd.io coding-style-patch-verification: ON
     237             :  *
     238             :  * Local Variables:
     239             :  * eval: (c-set-style "gnu")
     240             :  * End:
     241             :  */

Generated by: LCOV version 1.14