summaryrefslogtreecommitdiffstats
path: root/src/vlib
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2020-11-09 10:30:06 -0500
committerFlorin Coras <florin.coras@gmail.com>2020-11-09 16:40:02 +0000
commit85866e782c0e8d49b2fbc5f8fdf77065f1efae14 (patch)
treedf9721815106a6012208dcfbf72be30c315f8500 /src/vlib
parent8c4fa01d1360cd5315e671de96dfeff7dae246f5 (diff)
vlib: support macros in initial config file
Type: improvement Signed-off-by: Dave Barach <dave@barachs.net> Change-Id: If8a19eb6688755311a3430437331ddf13c7e28c8
Diffstat (limited to 'src/vlib')
-rw-r--r--src/vlib/unix/cli.c74
1 files changed, 59 insertions, 15 deletions
diff --git a/src/vlib/unix/cli.c b/src/vlib/unix/cli.c
index c7732ae431f..07ce414f616 100644
--- a/src/vlib/unix/cli.c
+++ b/src/vlib/unix/cli.c
@@ -3359,10 +3359,14 @@ unix_cli_exec (vlib_main_t * vm,
int fd;
unformat_input_t sub_input;
clib_error_t *error;
-
+ unix_cli_main_t *cm = &unix_cli_main;
+ unix_cli_file_t *cf;
+ u8 *file_data = 0;
file_name = 0;
fd = -1;
error = 0;
+ struct stat s;
+
if (!unformat (input, "%s", &file_name))
{
@@ -3379,23 +3383,63 @@ unix_cli_exec (vlib_main_t * vm,
}
/* Make sure its a regular file. */
- {
- struct stat s;
+ if (fstat (fd, &s) < 0)
+ {
+ error = clib_error_return_unix (0, "failed to stat `%s'", file_name);
+ goto done;
+ }
- if (fstat (fd, &s) < 0)
- {
- 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)))
+ {
+ error = clib_error_return (0, "not a regular file `%s'", file_name);
+ goto done;
+ }
- 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;
- }
- }
+ /* Read the file */
+ vec_validate (file_data, s.st_size);
+
+ if (read (fd, file_data, s.st_size) != s.st_size)
+ {
+ error = clib_error_return_unix (0, "Failed to read %d bytes from '%s'",
+ s.st_size, file_name);
+ vec_free (file_data);
+ goto done;
+ }
+
+ /* The macro expander expects a c string... */
+ vec_add1 (file_data, 0);
+
+ unformat_init_vector (&sub_input, file_data);
+
+ /* Run the file contents through the macro processor */
+ if (vec_len (sub_input.buffer) > 1)
+ {
+ u8 *expanded;
+ clib_macro_main_t *mm = 0;
+
+ /* Initial config process? Use the global macro table. */
+ if (pool_is_free_index
+ (cm->cli_file_pool, cm->current_input_file_index))
+ mm = &cm->macro_main;
+ else
+ {
+ /* Otherwise, use the per-cli-process macro table */
+ cf = pool_elt_at_index (cm->cli_file_pool,
+ cm->current_input_file_index);
+ mm = &cf->macro_main;
+ }
- unformat_init_clib_file (&sub_input, fd);
+ expanded = (u8 *) clib_macro_eval (mm,
+ (i8 *) sub_input.buffer,
+ 1 /* complain */ ,
+ 0 /* level */ ,
+ 8 /* max_level */ );
+ /* Macro processor NULL terminates the return */
+ _vec_len (expanded) -= 1;
+ vec_reset_length (sub_input.buffer);
+ vec_append (sub_input.buffer, expanded);
+ vec_free (expanded);
+ }
vlib_cli_input (vm, &sub_input, 0, 0);
unformat_free (&sub_input);
#fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*---------------------------------------------------------------------------
 * Copyright (c) 2009-2014 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *---------------------------------------------------------------------------
 */
#include <stdbool.h>
#include <vnet/fib/ip6_fib.h>
#include <vnet/ip/ip.h>
#include <vnet/ipip/ipip.h>
#include <vnet/vnet.h>
#include <vppinfra/error.h>

#define SIXRD_DEFAULT_MTU 1480	/* 1500 - IPv4 header */

#define foreach_sixrd_error                                                    \
  /* Must be first. */                                                         \
  _(NONE, "valid SIXRD packets")                                               \
  _(BAD_PROTOCOL, "bad protocol")                                              \
  _(SEC_CHECK, "security check failed")                                        \
  _(NO_TUNNEL, "no tunnel")


typedef enum
{
#define _(sym, str) SIXRD_ERROR_##sym,
  foreach_sixrd_error
#undef _
    SIXRD_N_ERROR,
} sixrd_error_t;

extern sixrd_main_t sixrd_main;

static_always_inline sixrd_tunnel_t *
find_tunnel_by_ip4_address (ip4_address_t * ip)
{
  sixrd_main_t *sm = &sixrd_main;
  uword *p;
  p = hash_get (sm->tunnel_by_ip, ip->as_u32);
  if (!p)
    return NULL;
  return pool_elt_at_index (sm->tunnels, p[0]);
}

static_always_inline sixrd_tunnel_t *
ip4_sixrd_get_tunnel (u32 sdi, ip4_address_t * addr, u8 * error)
{
  sixrd_tunnel_t *t = find_tunnel_by_ip4_address (addr);
  if (!t)
    {
      *error = SIXRD_ERROR_NO_TUNNEL;
      return NULL;
    }
  return t;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */