Example #1
0
static value_t*
Closure_IFn_invoke (int nargs, environment_t *env, value_t *arg1, value_t *arg2, value_t *arg3, value_t *argrest)
{
	closure_t *c = (closure_t*)arg1;

	assert (nargs >= 1);
	assert (arg1->ptable->type == TYPE_Closure);

	if (argrest == VALUE_NONE)
		return c->fn (nargs - 1, c->env, arg2, arg3, VALUE_NONE, VALUE_NONE);
	else
		return c->fn (nargs - 1, c->env, arg2, arg3, ARG_FIRST (argrest), ARG_NEXT (argrest));
}
Example #2
0
File: test.c Project: ariavie/bcm
cmd_result_t
test_run(int u, args_t *a)
/*
 * Function:    test_run
 * Purpose:     Perform "run" command for test.
 * Parameters:  none - Run all selected tests with loaded parameters.
 *              "test [options]"
 * Returns:     Test result.
 */
{
    test_t      *test;
    int         rv;
    int         loops = -1;

    if (test_active) {
        cli_out("%s: Already running: %s\n", ARG_CMD(a), test_active->t_name);
        return(CMD_FAIL);
    } else if (!sh_check_attached(ARG_CMD(a), u)) {
        return(CMD_FAIL);
    }

    if (!ARG_CNT(a)) {
        rv = test_run_selected(u);
    } else {
        if (NULL == (test = test_find(ARG_CUR(a)))) {
            cli_out("%s: Unknown test: %s\n", ARG_CMD(a), ARG_CUR(a));
            return(CMD_FAIL);
        }

        if (!(test->t_flags & _test_chip(u)) && 
            !(test_options & TEST_O_OVERRIDE)) {
            cli_out("Error: Test %d (%s) not supported on %s\n"
                    "Error: use \"testmode +override\" to override and run\n",
                    test->t_test, test->t_name, SOC_UNIT_GROUP(u));
            return(CMD_FAIL);
        }           
        ARG_NEXT(a);                    /* Move arg pointer */
        /* If there is an argument which is an integer, it's a loop count */
        if (ARG_CUR(a) && isint(ARG_CUR(a))) {
            loops = strtoul(_ARG_GET(a), NULL, 10);
        }
        rv = test_dispatch(u, test, loops, ARG_CNT(a) ? a : NULL);
    }
    return(rv == TEST_INTR ? CMD_INTR : rv);
}
Example #3
0
File: ctrans.c Project: ariavie/bcm
cmd_result_t
ct_setup(int unit, args_t *args)
{
    parse_table_t  pt;
    char *subcmd;
    int rv;
    int db_idx = cur_db;

    COMPILER_REFERENCE(unit);

    if ((subcmd = ARG_GET(args)) == NULL) {
        cli_out("Next Hop is%s running\n",
                next_hop_running() ? "" : " not");
        cli_out("ATP is%s running\n",
                atp_running() ? "" : " not");
        return CMD_OK;
    }

    if (sal_strcasecmp(subcmd, "RX") == 0) {
        char *next_arg;

        if ((next_arg = ARG_GET(args)) == NULL) {
            return CMD_USAGE;
        }
        if (sal_strcasecmp(next_arg, "START")) {
            return CMD_USAGE;
        }
        if (pkt_preinit() < 0) {
            cli_out("Failed to start BCM RX\n");
            return CMD_FAIL;
        }
        
    } else if (sal_strcasecmp(subcmd, "NEXTHOP") == 0) {
        int idx;
        const cpudb_key_t *key;
        int s_unit = 0, s_port = 0, duplex = 0;
        int did_print = FALSE;

        if (cputrans_trans_count() == 0) {
            cputrans_trans_add(&bcm_trans_ptr);
        }

        if (ARG_CUR(args) == NULL) {
            for (idx = 0; idx < next_hop_num_ports_get(); idx++) {
                if (next_hop_port_get(idx, &s_unit, &s_port, &duplex)
                    == BCM_E_NONE) {
                    cli_out("NH idx %d:  Unit %d, Port %d: %sarked duplex.\n",
                            idx, s_unit, s_port, duplex ? "M" : "Not m");
                } else {
                    cli_out("NH idx %d:  INVALID?\n", idx);
                }
                did_print = TRUE;
            }
            for (idx = 0; idx < next_hop_max_entries_get(); idx++) {
                key = next_hop_key_get(idx);
                if (key != NULL) {
                    cli_out("KEY %x:%x:%x:%x:%x:%x is in NH DB\n",
                            (*key).key[0], (*key).key[1], (*key).key[2],
                            (*key).key[3], (*key).key[4], (*key).key[5]);
                    did_print = TRUE;
                }
            }
            if (!did_print) {
                cli_out("Next Hop database and ports empty\n");
            }
        } else {
            int thrd_pri = -1;
            int rx_pri = -1;
            int nhvlan = -1, nhcos = -1, src_mod = -1;
            int add_ports = 0;
            const cpudb_base_t *base;

            parse_table_init(unit, &pt);
            parse_table_add(&pt, "DBidx", PQ_DFL|PQ_INT, 0, &db_idx, 0);
            parse_table_add(&pt, "THRDpri", PQ_DFL|PQ_INT, 0, &thrd_pri, 0);
            parse_table_add(&pt, "RXpri", PQ_DFL|PQ_INT, 0, &rx_pri, 0);
            parse_table_add(&pt, "COS", PQ_DFL|PQ_INT, 0, &nhcos, 0);
            parse_table_add(&pt, "VLAN", PQ_DFL|PQ_INT, 0, &nhvlan, 0);
            parse_table_add(&pt, "SrcMOD", PQ_DFL|PQ_INT, 0, &src_mod, 0);
            parse_table_add(&pt, "Add", PQ_DFL|PQ_INT, 0, &add_ports, 0);
            if (parse_arg_eq(args, &pt) < 0) {
                parse_arg_eq_done(&pt);
                return CMD_USAGE;
            }
            if (db_idx < 0 || db_idx >= MAX_DBS ||
                    db_refs[db_idx] == CPUDB_REF_NULL) {
                cli_out("Bad db index: %d\n", db_idx);
                return CMD_FAIL;
            }
            if (db_refs[db_idx]->local_entry == NULL) {
                cli_out("No local DB entry in cpudb %d\n", db_idx);
                return CMD_FAIL;
            }

            if (nhcos >= 0) {
                next_hop_cos_set(nhcos);
            }
            if (nhvlan >= 0) {
                next_hop_vlan_set(nhvlan);
                /* Load the next hop mac addr into L2 tables */
                rv = nh_tx_dest_install(TRUE, nhvlan);
                if (rv < 0) {
                    cli_out("NH TX dest install failed: %s\n", bcm_errmsg(rv));
                    return CMD_FAIL;
                }
            }
            if (src_mod >= 0) {
                int mod, port;

                nh_tx_src_get(&mod, &port);
                mod = src_mod;
                nh_tx_src_set(mod, port);
                nh_tx_unknown_modid_set(src_mod);
            }
            
            if (pkt_preinit() < 0) {
                cli_out("NH: Pre-init failed\n");
                return CMD_FAIL;
            }
            
            if (next_hop_running()) {
                cli_out("Next hop is running; stopping: %d\n",
                        next_hop_stop());
            }
            
            if (thrd_pri >= 0 || rx_pri >= 0) {
                if (next_hop_config_set(NULL, thrd_pri, rx_pri) < 0) {
                    cli_out("Warning: config set failed\n");
                }
            }

            base = &db_refs[db_idx]->local_entry->base;
            if (add_ports > 0) {
                int i, u, p;
                for (i = 0; i < base->num_stk_ports; i++) {
                    bcm_port_encap_config_t mode;
                    u = base->stk_ports[i].unit;
                    p = base->stk_ports[i].port;

                    rv = bcm_port_encap_config_get(u, p, &mode);
                    if (BCM_FAILURE(rv)) {
                        continue;
                    }
                    if (mode.encap == BCM_PORT_ENCAP_IEEE) {
                        continue;
                    }
                    rv = next_hop_port_add(u, p, 1);
                    if (rv < 0) {
                        cli_out("nexthop add (%d.%d) failed: %s\n",
                                u, p, bcm_errmsg(rv));
                    }
                }
            }
            rv = next_hop_start(base);
            if (rv < 0) {
                cli_out("ERROR: next_hop_start failed: %s\n", bcm_errmsg(rv));
            }
            parse_arg_eq_done(&pt);
        }
        return CMD_OK;
#if defined(BROADCOM_DEBUG)
    } else if (sal_strcasecmp(subcmd, "NHSHOW") == 0) {
        next_hop_dump();
        return CMD_OK;
#endif /* BROADCOM_DEBUG */
    } else if (sal_strcasecmp(subcmd, "TXLIMIT") == 0) {
        int cos = -1;
        int limit = 0;
        int rv;

        parse_table_init(unit, &pt);
        parse_table_add(&pt, "COS", PQ_DFL|PQ_INT, 0, &cos, 0);
        parse_table_add(&pt, "Limit", PQ_DFL|PQ_INT, 0, &limit, 0);

        if (parse_arg_eq(args, &pt) < 0) {
            parse_arg_eq_done(&pt);
            return CMD_USAGE;
        }

        if (cos == -1) {
            for (cos = 0; cos <= BCM_COS_MAX; cos++) {
                cputrans_tx_alloc_limit_get(cos, &limit);
                cli_out("Limit for COS %d currently %d\n", cos, limit);
            }
            parse_arg_eq_done(&pt);
            return CMD_OK;
        }

        rv = cputrans_tx_alloc_limit_set(cos, limit);
        if (rv < 0) {
            cli_out("WARNING: limit set returned %d: %s\n", rv, bcm_errmsg(rv));
        }

        parse_arg_eq_done(&pt);
        return CMD_OK;
        
    } else if (sal_strcasecmp(subcmd, "ATP") == 0) {
        int tx_pri = -1;
        int rx_pri = -1;
        int atp_cos = -1, atp_vlan = -1;

#if defined(BROADCOM_DEBUG)
        if ((subcmd = ARG_CUR(args)) != NULL) {
            if (sal_strcasecmp(subcmd, "SHOW") == 0) {
                ARG_NEXT(args);
                atp_dump(1);
                return CMD_OK;
            }
        }
#endif /* BROADCOM_DEBUG */

#ifdef INCLUDE_ATPTRANS_SOCKET        
        if ((subcmd = ARG_CUR(args)) != NULL) {
            if (parse_cmp("TRANSport", subcmd, '\0')) {
                ARG_NEXT(args);
                if ((subcmd = ARG_GET(args)) == NULL) {
                    return CMD_USAGE;
                }
                if (db_idx < 0 || db_idx >= MAX_DBS ||
                    db_refs[db_idx] == CPUDB_REF_NULL) {
                    cli_out("Bad or empty DB index: %d\n", db_idx);
                    return CMD_FAIL;
                }
                if (db_refs[db_idx]->local_entry == NULL) {
                    cli_out("No local DB entry in cpudb %d\n", db_idx);
                    return CMD_FAIL;
                }
                if (parse_cmp("SOCKet", subcmd, '\0')) {
                    return atptrans_socket_subcmd(unit, args, db_refs[db_idx]);
                }
                return CMD_USAGE;
            }
        }
#endif /* INCLUDE_ATPTRANS_SOCKET */

        if (cputrans_trans_count() == 0) {
            cputrans_trans_add(&bcm_trans_ptr);
        }

        if (pkt_preinit() < 0) {
#ifdef INCLUDE_ATPTRANS_SOCKET
            /* If ATP over sockets is running, then device RX is not
               required for ATP */
            if (!atptrans_socket_server_running()) {
                cli_out("ATP: Pre-init failed\n");
                return CMD_FAIL;
            }
#else
            cli_out("ATP: Pre-init failed\n");
            return CMD_FAIL;
#endif
        }

        parse_table_init(unit, &pt);
        parse_table_add(&pt, "DBidx", PQ_DFL|PQ_INT, 0, &db_idx, 0);
        parse_table_add(&pt, "TXPrio", PQ_DFL|PQ_INT, 0, &tx_pri, 0);
        parse_table_add(&pt, "RXPrio", PQ_DFL|PQ_INT, 0, &rx_pri, 0);
        parse_table_add(&pt, "COS", PQ_DFL|PQ_INT, 0, &atp_cos, 0);
        parse_table_add(&pt, "VLAN", PQ_DFL|PQ_INT, 0, &atp_vlan, 0);
        if (parse_arg_eq(args, &pt) < 0) {
            parse_arg_eq_done(&pt);
            return CMD_USAGE;
        }
        if (db_idx < 0 || db_idx >= MAX_DBS ||
                db_refs[db_idx] == CPUDB_REF_NULL) {
            cli_out("Bad db index: %d\n", db_idx);
            parse_arg_eq_done(&pt);
            return CMD_FAIL;
        }
        if (db_refs[db_idx]->local_entry == NULL) {
            cli_out("No local DB entry in cpudb %d\n", db_idx);
            parse_arg_eq_done(&pt);
            return CMD_FAIL;
        }

        atp_cos_vlan_set(atp_cos, atp_vlan);
        rv = atp_config_set(tx_pri, rx_pri, NULL);
        if (rv < 0) {
            cli_out("ERROR: ATP config set failed: %s\n", bcm_errmsg(rv));
        }
        atp_db_update(db_refs[db_idx]);
        rv = atp_start(ATP_F_LEARN_SLF,
                       ATP_UNITS_ALL,
                       BCM_RCO_F_ALL_COS);
        if (rv < 0) {
            cli_out("ERROR: ATP start failed: %s\n", bcm_errmsg(rv));
        }
        parse_arg_eq_done(&pt);
#if defined(BROADCOM_DEBUG)
    } else if (sal_strcasecmp(subcmd, "C2C") == 0) {
        if ((subcmd = ARG_GET(args)) != NULL) {
            if (sal_strcasecmp(subcmd, "SHOW") == 0) {
                c2c_dump();
                return CMD_OK;
            }
        }
#endif /* BROADCOM_DEBUG */
    } else if (sal_strcasecmp(subcmd, "STOP") == 0) {
        rv = atp_stop();
        if (rv < 0) {
            cli_out("ERROR: ATP stop failed: %s\n", bcm_errmsg(rv));
        }
    } else if (sal_strcasecmp(subcmd, "TIMEOUT") == 0) {
        int retry_us = -1, retries = -1;
        
        if (ARG_CUR(args) == NULL) {
            atp_timeout_get(&retry_us, &retries);
            cli_out("Retries occur after %d usecs and will be made %d times\n",
                    retry_us, retries);
        } else {
            parse_table_init(unit, &pt);
            parse_table_add(&pt, "RetryTO", PQ_DFL|PQ_INT, 0, &retry_us, 0);
            parse_table_add(&pt, "RETRIES", PQ_DFL|PQ_INT, 0, &retries, 0);
            if (parse_arg_eq(args, &pt) < 0) {
                parse_arg_eq_done(&pt);
                return CMD_USAGE;
            }
            cli_out("Retry set(%d, %d) returns %d\n", retry_us, retries,
                    atp_timeout_set(retry_us, retries));
            parse_arg_eq_done(&pt);
        }
    } else if (sal_strcasecmp(subcmd, "SEGLEN") == 0) {
        int seg_len = -1;
        char *next_arg;

        if ((next_arg = ARG_GET(args)) == NULL) {
            seg_len = atp_segment_len_get();
            cli_out("Current segmentation length is %d\n", seg_len);
        } else {
            seg_len = strtoul(next_arg, NULL, 0);
            cli_out("Seg_len set(%d) returns %d\n", seg_len,
                    atp_segment_len_set(seg_len));
        }
    } else if (sal_strcasecmp(subcmd, "POOLSIZE") == 0) {
        int tx_pool, rx_pool;
        /* Get current pool sizes in case one is not specified */
        atp_pool_size_get(&tx_pool, &rx_pool);
        if (ARG_CUR(args) == NULL) {
            cli_out("Current TX pool: %d.  RX pool %d.\n", tx_pool, rx_pool);
        } else {
            parse_table_init(unit, &pt);
            parse_table_add(&pt, "TX", PQ_DFL|PQ_INT, 0, &tx_pool, 0);
            parse_table_add(&pt, "RX", PQ_DFL|PQ_INT, 0, &rx_pool, 0);
            if (parse_arg_eq(args, &pt) < 0) {
                parse_arg_eq_done(&pt);
                return CMD_USAGE;
            }
            cli_out("Pool size set returns %d\n",
                    atp_pool_size_set(tx_pool, rx_pool));
            parse_arg_eq_done(&pt);
        }
    } else {
        cli_out("Subcommand not found: %s\n", subcmd);
        return CMD_USAGE;
    }

    return CMD_OK;
}
Example #4
0
cmd_result_t
sh_config(int u, args_t *a)
/*
 * Function: 	sh_config
 * Purpose:	Configure switch management information.
 * Parameters:
 * Returns:
 */
{
    int	rv, add;
    char *name, *value, *c = ARG_CUR(a);

    rv = CMD_OK;

    if (c == NULL || !sal_strcasecmp(c, "show")) {
        name = NULL;
        while (kconfig_get_next(&name, &value) >= 0) {
            sal_printf("\t%s=%s\n", name, value);
        }
        if (NULL != c) {
            ARG_NEXT(a);
        }
        return CMD_OK;
    }

    if (!sal_strcasecmp(c, "delete")) {
	ARG_NEXT(a);
	while ((c = ARG_GET(a)) != NULL) {
	    if (kconfig_set(c, 0) != 0) {
		printk("%s: Variable not found: %s\n", ARG_CMD(a), c);
		rv = CMD_FAIL;
	    }
	}
	return rv;
    }

    if (!sal_strcasecmp(c, "add")) {
	ARG_NEXT(a);
	add = 1;
    } else {
	add = 0;
    }

    if (ARG_CNT(a)) {
	while ((c = ARG_GET(a)) != NULL) {
	    char *s;
	    s = strchr(c, '=');
	    if (s == NULL) {
		printk("%s: Invalid assignment: %s\n", ARG_CMD(a), c);
		rv = CMD_FAIL;
	    } else {
                *s++ = 0;
                if (!add && kconfig_get(c) == NULL) {
                    printk("%s: Must use 'add' to create new variable: %s\n",
                           ARG_CMD(a), c);
                    rv = CMD_FAIL;
                } else {
                    if (kconfig_set(c, s) < 0) {
                        rv = CMD_FAIL;
                    }
		}
	    }
	}
    }

    return rv;
}
Example #5
0
/****************
 * Scan the provided buffer and return the S expression in our internal
 * format.  Returns a newly allocated expression.  If erroff is not NULL and
 * a parsing error has occurred, the offset into buffer will be returned.
 * If ARGFLAG is true, the function supports some printf like
 * expressions.
 *  These are:
 *	%m - MPI
 *	%s - string (no autoswitch to secure allocation)
 *	%d - integer stored as string (no autoswitch to secure allocation)
 *      %b - memory buffer; this takes _two_ arguments: an integer with the
 *           length of the buffer and a pointer to the buffer.
 *      %S - Copy an gcry_sexp_t here.  The S-expression needs to be a
 *           regular one, starting with a parenthesis.
 *           (no autoswitch to secure allocation)
 *  all other format elements are currently not defined and return an error.
 *  this includes the "%%" sequence becauce the percent sign is not an
 *  allowed character.
 * FIXME: We should find a way to store the secure-MPIs not in the string
 * but as reference to somewhere - this can help us to save huge amounts
 * of secure memory.  The problem is, that if only one element is secure, all
 * other elements are automagicaly copied to secure memory too, so the most
 * common operation gcry_sexp_cdr_mpi() will always return a secure MPI
 * regardless whether it is needed or not.
 */
static gcry_error_t
vsexp_sscan (gcry_sexp_t *retsexp, size_t *erroff,
	     const char *buffer, size_t length, int argflag,
	     void **arg_list, va_list arg_ptr)
{
  gcry_err_code_t err = 0;
  static const char tokenchars[] =
    "abcdefghijklmnopqrstuvwxyz"
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "0123456789-./_:*+=";
  const char *p;
  size_t n;
  const char *digptr = NULL;
  const char *quoted = NULL;
  const char *tokenp = NULL;
  const char *hexfmt = NULL;
  const char *base64 = NULL;
  const char *disphint = NULL;
  const char *percent = NULL;
  int hexcount = 0;
  int quoted_esc = 0;
  int datalen = 0;
  size_t dummy_erroff;
  struct make_space_ctx c;
  int arg_counter = 0;
  int level = 0;

  if (!erroff)
    erroff = &dummy_erroff;

  /* Depending on whether ARG_LIST is non-zero or not, this macro gives
     us the next argument, either from the variable argument list as
     specified by ARG_PTR or from the argument array ARG_LIST.  */
#define ARG_NEXT(storage, type)                          \
  do                                                     \
    {                                                    \
      if (!arg_list)                                     \
	storage = va_arg (arg_ptr, type);                \
      else                                               \
	storage = *((type *) (arg_list[arg_counter++])); \
    }                                                    \
  while (0)

  /* The MAKE_SPACE macro is used before each store operation to
     ensure that the buffer is large enough.  It requires a global
     context named C and jumps out to the label LEAVE on error! It
     also sets ERROFF using the variables BUFFER and P.  */
#define MAKE_SPACE(n)  do {                                                \
                            gpg_err_code_t _ms_err = make_space (&c, (n)); \
                            if (_ms_err)                                   \
                              {                                            \
                                err = _ms_err;                             \
                                *erroff = p - buffer;                      \
                                goto leave;                                \
                              }                                            \
                       } while (0)

  /* The STORE_LEN macro is used to store the length N at buffer P. */
#define STORE_LEN(p,n) do {						   \
			    DATALEN ashort = (n);			   \
			    memcpy ( (p), &ashort, sizeof(ashort) );	   \
			    (p) += sizeof (ashort);			   \
			} while (0)

  /* We assume that the internal representation takes less memory than
     the provided one.  However, we add space for one extra datalen so
     that the code which does the ST_CLOSE can use MAKE_SPACE */
  c.allocated = length + sizeof(DATALEN);
  if (buffer && length && gcry_is_secure (buffer))
    c.sexp = gcry_malloc_secure (sizeof *c.sexp + c.allocated - 1);
  else
    c.sexp = gcry_malloc (sizeof *c.sexp + c.allocated - 1);
  if (!c.sexp)
    {
      err = gpg_err_code_from_errno (errno);
      *erroff = 0;
      goto leave;
    }
  c.pos = c.sexp->d;

  for (p = buffer, n = length; n; p++, n--)
    {
      if (tokenp && !hexfmt)
	{
	  if (strchr (tokenchars, *p))
	    continue;
	  else
	    {
	      datalen = p - tokenp;
	      MAKE_SPACE (datalen);
	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, datalen);
	      memcpy (c.pos, tokenp, datalen);
	      c.pos += datalen;
	      tokenp = NULL;
	    }
	}

      if (quoted)
	{
	  if (quoted_esc)
	    {
	      switch (*p)
		{
		case 'b': case 't': case 'v': case 'n': case 'f':
		case 'r': case '"': case '\'': case '\\':
		  quoted_esc = 0;
		  break;

		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7':
		  if (!((n > 2)
                        && (p[1] >= '0') && (p[1] <= '7')
                        && (p[2] >= '0') && (p[2] <= '7')))
		    {
		      *erroff = p - buffer;
		      /* Invalid octal value.  */
		      err = GPG_ERR_SEXP_BAD_QUOTATION;
                      goto leave;
		    }
		  p += 2;
		  n -= 2;
		  quoted_esc = 0;
		  break;

		case 'x':
		  if (!((n > 2) && hexdigitp (p+1) && hexdigitp (p+2)))
		    {
		      *erroff = p - buffer;
		      /* Invalid hex value.  */
		      err = GPG_ERR_SEXP_BAD_QUOTATION;
                      goto leave;
		    }
		  p += 2;
		  n -= 2;
		  quoted_esc = 0;
		  break;

		case '\r':
		  /* ignore CR[,LF] */
		  if (n && (p[1] == '\n'))
		    {
		      p++;
		      n--;
		    }
		  quoted_esc = 0;
		  break;

		case '\n':
		  /* ignore LF[,CR] */
		  if (n && (p[1] == '\r'))
		    {
		      p++;
		      n--;
		    }
		  quoted_esc = 0;
		  break;

		default:
		  *erroff = p - buffer;
		  /* Invalid quoted string escape.  */
		  err = GPG_ERR_SEXP_BAD_QUOTATION;
                  goto leave;
		}
	    }
	  else if (*p == '\\')
	    quoted_esc = 1;
	  else if (*p == '\"')
	    {
	      /* Keep it easy - we know that the unquoted string will
		 never be larger. */
	      unsigned char *save;
	      size_t len;

	      quoted++; /* Skip leading quote.  */
	      MAKE_SPACE (p - quoted);
	      *c.pos++ = ST_DATA;
	      save = c.pos;
	      STORE_LEN (c.pos, 0); /* Will be fixed up later.  */
	      len = unquote_string (quoted, p - quoted, c.pos);
	      c.pos += len;
	      STORE_LEN (save, len);
	      quoted = NULL;
	    }
	}
      else if (hexfmt)
	{
	  if (isxdigit (*p))
	    hexcount++;
	  else if (*p == '#')
	    {
	      if ((hexcount & 1))
		{
		  *erroff = p - buffer;
		  err = GPG_ERR_SEXP_ODD_HEX_NUMBERS;
                  goto leave;
		}

	      datalen = hexcount / 2;
	      MAKE_SPACE (datalen);
	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, datalen);
	      for (hexfmt++; hexfmt < p; hexfmt++)
		{
                  int tmpc;

		  if (whitespacep (hexfmt))
		    continue;
		  tmpc = hextonibble (*(const unsigned char*)hexfmt);
                  for (hexfmt++; hexfmt < p && whitespacep (hexfmt); hexfmt++)
		    ;
                  if (hexfmt < p)
                    {
                      tmpc *= 16;
                      tmpc += hextonibble (*(const unsigned char*)hexfmt);
                    }
                  *c.pos++ = tmpc;
		}
	      hexfmt = NULL;
	    }
	  else if (!whitespacep (p))
	    {
	      *erroff = p - buffer;
	      err = GPG_ERR_SEXP_BAD_HEX_CHAR;
              goto leave;
	    }
	}
      else if (base64)
	{
	  if (*p == '|')
	    base64 = NULL;
	}
      else if (digptr)
	{
	  if (digitp (p))
	    ;
	  else if (*p == ':')
	    {
	      datalen = atoi (digptr); /* FIXME: check for overflow.  */
	      digptr = NULL;
	      if (datalen > n - 1)
		{
		  *erroff = p - buffer;
		  /* Buffer too short.  */
		  err = GPG_ERR_SEXP_STRING_TOO_LONG;
                  goto leave;
		}
	      /* Make a new list entry.  */
	      MAKE_SPACE (datalen);
	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, datalen);
	      memcpy (c.pos, p + 1, datalen);
	      c.pos += datalen;
	      n -= datalen;
	      p += datalen;
	    }
	  else if (*p == '\"')
	    {
	      digptr = NULL; /* We ignore the optional length.  */
	      quoted = p;
	      quoted_esc = 0;
	    }
	  else if (*p == '#')
	    {
	      digptr = NULL; /* We ignore the optional length.  */
	      hexfmt = p;
	      hexcount = 0;
	    }
	  else if (*p == '|')
	    {
	      digptr = NULL; /* We ignore the optional length.  */
	      base64 = p;
	    }
	  else
	    {
	      *erroff = p - buffer;
	      err = GPG_ERR_SEXP_INV_LEN_SPEC;
              goto leave;
	    }
	}
      else if (percent)
	{
	  if (*p == 'm' || *p == 'M')
	    {
	      /* Insert an MPI.  */
	      gcry_mpi_t m;
	      size_t nm = 0;
              int mpifmt = *p == 'm'? GCRYMPI_FMT_STD: GCRYMPI_FMT_USG;

	      ARG_NEXT (m, gcry_mpi_t);

              if (gcry_mpi_get_flag (m, GCRYMPI_FLAG_OPAQUE))
                {
                  void *mp;
                  unsigned int nbits;

                  mp = gcry_mpi_get_opaque (m, &nbits);
                  nm = (nbits+7)/8;
                  if (mp && nm)
                    {
                      MAKE_SPACE (nm);
                      if (!gcry_is_secure (c.sexp->d)
                          && gcry_mpi_get_flag (m, GCRYMPI_FLAG_SECURE))
                        {
                          /* We have to switch to secure allocation.  */
                          gcry_sexp_t newsexp;
                          byte *newhead;

                          newsexp = gcry_malloc_secure (sizeof *newsexp
                                                        + c.allocated - 1);
                          if (!newsexp)
                            {
                              err = gpg_err_code_from_errno (errno);
                              goto leave;
                            }
                          newhead = newsexp->d;
                          memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d));
                          c.pos = newhead + (c.pos - c.sexp->d);
                          gcry_free (c.sexp);
                          c.sexp = newsexp;
                        }

                      *c.pos++ = ST_DATA;
                      STORE_LEN (c.pos, nm);
                      memcpy (c.pos, mp, nm);
                      c.pos += nm;
                    }
                }
              else
                {
                  if (gcry_mpi_print (mpifmt, NULL, 0, &nm, m))
                    BUG ();

                  MAKE_SPACE (nm);
                  if (!gcry_is_secure (c.sexp->d)
                      && gcry_mpi_get_flag ( m, GCRYMPI_FLAG_SECURE))
                    {
                      /* We have to switch to secure allocation.  */
                      gcry_sexp_t newsexp;
                      byte *newhead;

                      newsexp = gcry_malloc_secure (sizeof *newsexp
                                                    + c.allocated - 1);
                      if (!newsexp)
                        {
                          err = gpg_err_code_from_errno (errno);
                          goto leave;
                        }
                      newhead = newsexp->d;
                      memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d));
                      c.pos = newhead + (c.pos - c.sexp->d);
                      gcry_free (c.sexp);
                      c.sexp = newsexp;
                    }

                  *c.pos++ = ST_DATA;
                  STORE_LEN (c.pos, nm);
                  if (gcry_mpi_print (mpifmt, c.pos, nm, &nm, m))
                    BUG ();
                  c.pos += nm;
                }
	    }
	  else if (*p == 's')
	    {
	      /* Insert an string.  */
	      const char *astr;
	      size_t alen;

	      ARG_NEXT (astr, const char *);
	      alen = strlen (astr);

	      MAKE_SPACE (alen);
	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, alen);
	      memcpy (c.pos, astr, alen);
	      c.pos += alen;
	    }
	  else if (*p == 'b')
	    {
	      /* Insert a memory buffer.  */
	      const char *astr;
	      int alen;

	      ARG_NEXT (alen, int);
	      ARG_NEXT (astr, const char *);

	      MAKE_SPACE (alen);
	      if (alen
                  && !gcry_is_secure (c.sexp->d)
		  && gcry_is_secure (astr))
              {
		  /* We have to switch to secure allocation.  */
		  gcry_sexp_t newsexp;
		  byte *newhead;

		  newsexp = gcry_malloc_secure (sizeof *newsexp
                                                + c.allocated - 1);
                  if (!newsexp)
                    {
                      err = gpg_err_code_from_errno (errno);
                      goto leave;
                    }
		  newhead = newsexp->d;
		  memcpy (newhead, c.sexp->d, (c.pos - c.sexp->d));
		  c.pos = newhead + (c.pos - c.sexp->d);
		  gcry_free (c.sexp);
		  c.sexp = newsexp;
		}

	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, alen);
	      memcpy (c.pos, astr, alen);
	      c.pos += alen;
	    }
	  else if (*p == 'd')
	    {
	      /* Insert an integer as string.  */
	      int aint;
	      size_t alen;
	      char buf[35];

	      ARG_NEXT (aint, int);
	      sprintf (buf, "%d", aint);
	      alen = strlen (buf);
	      MAKE_SPACE (alen);
	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, alen);
	      memcpy (c.pos, buf, alen);
	      c.pos += alen;
	    }
	  else if (*p == 'u')
	    {
	      /* Insert an unsigned integer as string.  */
	      unsigned int aint;
	      size_t alen;
	      char buf[35];

	      ARG_NEXT (aint, unsigned int);
	      sprintf (buf, "%u", aint);
	      alen = strlen (buf);
	      MAKE_SPACE (alen);
	      *c.pos++ = ST_DATA;
	      STORE_LEN (c.pos, alen);
	      memcpy (c.pos, buf, alen);
	      c.pos += alen;
	    }
	  else if (*p == 'S')
	    {
	      /* Insert a gcry_sexp_t.  */
	      gcry_sexp_t asexp;
	      size_t alen, aoff;

	      ARG_NEXT (asexp, gcry_sexp_t);
              alen = get_internal_buffer (asexp, &aoff);
              if (alen)
                {
                  MAKE_SPACE (alen);
                  memcpy (c.pos, asexp->d + aoff, alen);
                  c.pos += alen;
                }
	    }
	  else
	    {
	      *erroff = p - buffer;
	      /* Invalid format specifier.  */
	      err = GPG_ERR_SEXP_INV_LEN_SPEC;
              goto leave;
	    }
	  percent = NULL;
	}
      else if (*p == '(')
	{
	  if (disphint)
	    {
	      *erroff = p - buffer;
	      /* Open display hint.  */
	      err = GPG_ERR_SEXP_UNMATCHED_DH;
              goto leave;
	    }
	  MAKE_SPACE (0);
	  *c.pos++ = ST_OPEN;
	  level++;
	}
      else if (*p == ')')
	{
	  /* Walk up.  */
	  if (disphint)
	    {
	      *erroff = p - buffer;
	      /* Open display hint.  */
	      err = GPG_ERR_SEXP_UNMATCHED_DH;
              goto leave;
	    }
	  MAKE_SPACE (0);
	  *c.pos++ = ST_CLOSE;
	  level--;
	}
      else if (*p == '\"')
	{
	  quoted = p;
	  quoted_esc = 0;
	}
      else if (*p == '#')
	{
	  hexfmt = p;
	  hexcount = 0;
	}
      else if (*p == '|')
	base64 = p;
      else if (*p == '[')
	{
	  if (disphint)
	    {
	      *erroff = p - buffer;
	      /* Open display hint.  */
	      err = GPG_ERR_SEXP_NESTED_DH;
              goto leave;
	    }
	  disphint = p;
	}
      else if (*p == ']')
	{
	  if (!disphint)
	    {
	      *erroff = p - buffer;
	      /* Open display hint.  */
	      err = GPG_ERR_SEXP_UNMATCHED_DH;
              goto leave;
	    }
	  disphint = NULL;
	}
      else if (digitp (p))
	{
	  if (*p == '0')
	    {
	      /* A length may not begin with zero.  */
	      *erroff = p - buffer;
	      err = GPG_ERR_SEXP_ZERO_PREFIX;
              goto leave;
	    }
	  digptr = p;
	}
      else if (strchr (tokenchars, *p))
	tokenp = p;
      else if (whitespacep (p))
	;
      else if (*p == '{')
	{
	  /* fixme: handle rescanning: we can do this by saving our
	     current state and start over at p+1 -- Hmmm. At this
	     point here we are in a well defined state, so we don't
	     need to save it.  Great.  */
	  *erroff = p - buffer;
	  err = GPG_ERR_SEXP_UNEXPECTED_PUNC;
          goto leave;
	}
      else if (strchr ("&\\", *p))
	{
	  /* Reserved punctuation.  */
	  *erroff = p - buffer;
	  err = GPG_ERR_SEXP_UNEXPECTED_PUNC;
          goto leave;
	}
      else if (argflag && (*p == '%'))
	percent = p;
      else
	{
	  /* Bad or unavailable.  */
	  *erroff = p - buffer;
	  err = GPG_ERR_SEXP_BAD_CHARACTER;
          goto leave;
	}
    }
  MAKE_SPACE (0);
  *c.pos++ = ST_STOP;

  if (level && !err)
    err = GPG_ERR_SEXP_UNMATCHED_PAREN;

 leave:
  if (err)
    {
      /* Error -> deallocate.  */
      if (c.sexp)
        {
          /* Extra paranoid wipe on error. */
          if (gcry_is_secure (c.sexp))
            wipememory (c.sexp, sizeof (struct gcry_sexp) + c.allocated - 1);
          gcry_free (c.sexp);
        }
      /* This might be expected by existing code...  */
      *retsexp = NULL;
    }
  else
    *retsexp = normalize (c.sexp);

  return gcry_error (err);
#undef MAKE_SPACE
#undef STORE_LEN
}
Example #6
0
static value_t*
cljc_core_apply (int nargs, environment_t *env, value_t *f, value_t *arg1, value_t *arg2, value_t *argrest)
{
	int ndirect = 0;
	int nrest;

	assert (nargs > 1);

	switch (nargs) {
		case 2:
			ndirect = 0;
			argrest = arg1;
			break;
		case 3:
			ndirect = 1;
			argrest = arg2;
			break;
		default:
			ndirect = 2;
			argrest = ARG_FLATTEN_TAIL (argrest);
			break;
	}

	nrest = ARG_COUNT (argrest);

	switch (ndirect) {
		case 0:
			switch (nrest) {
				case 0:
					arg1 = arg2 = argrest = VALUE_NONE;
					break;
				case 1:
					arg1 = ARG_FIRST (argrest);
					arg2 = argrest = VALUE_NONE;
					break;
				case 2:
					arg1 = ARG_FIRST (argrest);
					argrest = ARG_NEXT (argrest);
					arg2 = ARG_FIRST (argrest);
					argrest = VALUE_NONE;
					break;
				default:
					arg1 = ARG_FIRST (argrest);
					argrest = ARG_NEXT (argrest);
					arg2 = ARG_FIRST (argrest);
					argrest = ARG_NEXT (argrest);
					break;
			}
			break;
		case 1:
			switch (nrest) {
				case 0:
					arg2 = argrest = VALUE_NONE;
					break;
				case 1:
					arg2 = ARG_FIRST (argrest);
					argrest = VALUE_NONE;
					break;
				default:
					arg2 = ARG_FIRST (argrest);
					argrest = ARG_NEXT (argrest);
					break;
			}
			break;
		case 2:
			switch (nrest) {
				case 0:
					argrest = VALUE_NONE;
					break;
				default:
					argrest = argrest;
					break;
			}
			break;
		default:
			assert (false);
	}

	return invoken (f, ndirect + nrest, arg1, arg2, argrest);
}
Example #7
0
File: ipfix.c Project: ariavie/bcm
cmd_result_t
cmd_ipfix(int unit, args_t *arg)
{
    char *subcmd;
    parse_table_t pt;
    bcm_ipfix_config_t config;
    int rv, i;
    int port = 0;
    bcm_ip_t src_ip4_mask = 0, dst_ip4_mask = 0;
    bcm_ip_t tunnel_src_ip4_mask = 0, tunnel_dst_ip4_mask = 0;
    bcm_ip6_t src_ip6_mask, dst_ip6_mask;
    bcm_ip6_t tunnel_src_ip6_mask, tunnel_dst_ip6_mask;
    int enable_l2 = 0, enable_ip4 = 0, enable_ip6 = 0;
    int record_non_discard = 1, record_discard = 0, check_flow_end = 0;
    int use_l2_for_ip4 = 0, use_l2_for_ip6 = 0;
    int key_src_ip = 0, key_dst_ip = 0; 
    int  key_ip_prot = 0, key_ip_dscp = 0, key_ip_ecn = 0;
    int key_l4_src_port = 0, key_l4_dst_port = 0, key_ip6_flow = 0;
    int key_icmp_type = 0, key_icmp_code = 0, key_macda = 0, key_macsa = 0;
    int key_vlan_id = 0, key_vlan_pri = 0, key_ether_type = 0; 
    int key_vlan_tagged = 0, key_source_port = 0;
    int stage = 0, dscp_idx = 0, entry_limit = 8191;
    int min_time = 0, max_time = 0;
    int max_idle_time = 32767, sample_rate = 1;
    char str[IP6ADDR_STR_LEN];

    if (!ARG_CNT(arg)) {                  /* Nothing passed */
        cli_out("IPFIX Deamon Status: %s.\n",
                (ipfix_is_running[unit]) ? "Running" : "Not Running");

        cli_out("Reporting is enabled for: ");
        parse_mask_format(80, ipfix_report_table, ipfix_report[unit]);
        cli_out("Reporting is disabled for: ");
        parse_mask_format(80, ipfix_report_table, ipfix_report[unit] ^ ~0);

        cli_out("Number of records received: %d\n", ipfix_report_count[unit]);
        return(CMD_OK);
    }

    subcmd = ARG_GET(arg);
    if (subcmd == NULL) {
        return CMD_USAGE;
    }

    rv = BCM_E_NONE;
    sal_memset(src_ip6_mask, 0, sizeof(bcm_ip6_t));
    sal_memset(dst_ip6_mask, 0, sizeof(bcm_ip6_t));
    sal_memset(tunnel_src_ip6_mask, 0, sizeof(bcm_ip6_t));
    sal_memset(tunnel_dst_ip6_mask, 0, sizeof(bcm_ip6_t));

    if (!sal_strcasecmp(subcmd, "start")) {
        if (ipfix_is_running[unit]) {
            cli_out("%s: IPFIX thread already running.\n", ARG_CMD(arg));
            return CMD_OK;
        }
        ipfix_report_count[unit] = 0;
        ipfix_is_running[unit] = 1;
        rv = bcm_ipfix_export_fifo_control(unit, 1000000);
        if (rv < 0) {
            cli_out("%s: ERROR: %s\n", ARG_CMD(arg), bcm_errmsg(rv));
            return CMD_FAIL;
        }
        rv = bcm_ipfix_register(unit, _ipfix_callback, NULL);
    } else if (!sal_strcasecmp(subcmd, "stop")) {
        if (!ipfix_is_running[unit]) {
            cli_out("%s: ERROR: IPFIX thread already stopped.\n", ARG_CMD(arg));
            return CMD_FAIL;
        }
        ipfix_is_running[unit] = 0;
        rv = bcm_ipfix_export_fifo_control(unit, 0);
        if (rv < 0) {
            cli_out("%s: ERROR: %s\n", ARG_CMD(arg), bcm_errmsg(rv));
            return CMD_FAIL;
        }
        rv = bcm_ipfix_unregister(unit, _ipfix_callback, NULL);
    } else if (!sal_strcasecmp(subcmd, "set")) {
        parse_table_init(unit, &pt);
        parse_table_add(&pt, "Port", PQ_DFL | PQ_INT, (void *)-1, &port, 0);
        parse_table_add(&pt, "Stage", PQ_DFL | PQ_INT, (void *)0, &stage, 0);
        parse_table_add(&pt, "EnableL2", PQ_DFL | PQ_INT, (void *)0,
                        &enable_l2, 0);
        parse_table_add(&pt, "EnableIp4", PQ_DFL | PQ_INT, (void *)0,
                        &enable_ip4, 0);
        parse_table_add(&pt, "EnableIp6", PQ_DFL | PQ_INT, (void *)0,
                        &enable_ip6, 0);
        parse_table_add(&pt, "RecordNonDiscard", PQ_DFL | PQ_INT, (void *)0,
                        &record_non_discard, 0);
        parse_table_add(&pt, "RecordDiscard", PQ_DFL | PQ_INT, (void *)0,
                        &record_discard, 0);
        parse_table_add(&pt, "CheckFlowEnd", PQ_DFL | PQ_INT, (void *)0,
                        &check_flow_end, 0);
        parse_table_add(&pt, "UseL2ForIp4", PQ_DFL | PQ_INT, (void *)0,
                        &use_l2_for_ip4, 0);
        parse_table_add(&pt, "UseL2ForIp6", PQ_DFL | PQ_INT, (void *)0,
                        &use_l2_for_ip6, 0);
        parse_table_add(&pt, "KeySrcIp", PQ_DFL | PQ_INT, (void *)0,
                        &key_src_ip, 0);
        parse_table_add(&pt, "KeyIpProt", PQ_DFL | PQ_INT, (void *)0,
                        &key_ip_prot, 0);
        parse_table_add(&pt, "KeyIpDscp", PQ_DFL | PQ_INT, (void *)0,
                        &key_ip_dscp, 0);
        parse_table_add(&pt, "KeyIpEcn", PQ_DFL | PQ_INT, (void *)0,
                        &key_ip_ecn, 0);
        parse_table_add(&pt, "KeyDstIp", PQ_DFL | PQ_INT, (void *)0,
                        &key_dst_ip, 0);
        parse_table_add(&pt, "KeyIpEcn", PQ_DFL | PQ_INT, (void *)0,
                        &key_ip_ecn, 0);
        parse_table_add(&pt, "KeyL4SrcPort", PQ_DFL | PQ_INT, (void *)0,
                        &key_l4_src_port, 0);
        parse_table_add(&pt, "KeyL4DstPort", PQ_DFL | PQ_INT, (void *)0,
                        &key_l4_dst_port, 0);
        parse_table_add(&pt, "KeyIp6Flow", PQ_DFL | PQ_INT, (void *)0,
                        &key_ip6_flow, 0);
        parse_table_add(&pt, "KeyIcmpType", PQ_DFL | PQ_INT, (void *)0,
                        &key_icmp_type, 0);
        parse_table_add(&pt, "KeyIcmpCode", PQ_DFL | PQ_INT, (void *)0,
                        &key_icmp_code, 0);
        parse_table_add(&pt, "KeyMacDa", PQ_DFL | PQ_INT, (void *)0,
                        &key_macda, 0);
        parse_table_add(&pt, "KeyMacSa", PQ_DFL | PQ_INT, (void *)0,
                        &key_macsa, 0);
        parse_table_add(&pt, "KeyVlanId", PQ_DFL | PQ_INT, (void *)0,
                        &key_vlan_id, 0);
        parse_table_add(&pt, "KeyVlanPri", PQ_DFL | PQ_INT, (void *)0,
                        &key_vlan_pri, 0);
        parse_table_add(&pt, "KeyEtherType", PQ_DFL | PQ_INT, (void *)0,
                        &key_ether_type, 0);
        parse_table_add(&pt, "KeyVlanTagged", PQ_DFL | PQ_INT, (void *)0,
                        &key_vlan_tagged, 0);
        parse_table_add(&pt, "KeySourcePort", PQ_DFL | PQ_INT, (void *)0,
                        &key_source_port, 0);
        parse_table_add(&pt, "Ip4SrcMask", PQ_DFL | PQ_IP, 0, &src_ip4_mask,
                        0);
        parse_table_add(&pt, "Ip4DstMask", PQ_DFL | PQ_IP, 0, &dst_ip4_mask,
                        0);
        parse_table_add(&pt, "TunnelIp4SrcMask", PQ_DFL | PQ_IP, 0,
                        &tunnel_src_ip4_mask, 0);
        parse_table_add(&pt, "TunnelIp4DstMask", PQ_DFL | PQ_IP, 0,
                        &tunnel_dst_ip4_mask, 0);
        parse_table_add(&pt, "Ip6SrcMask", PQ_DFL | PQ_IP6, 0, &src_ip6_mask,
                        0);
        parse_table_add(&pt, "Ip6DstMask", PQ_DFL | PQ_IP6, 0, &dst_ip6_mask,
                        0);
        parse_table_add(&pt, "TunnelIp6SrcMask", PQ_DFL | PQ_IP6, 0,
                        &tunnel_src_ip6_mask, 0);
        parse_table_add(&pt, "TunnelIp6DstMask", PQ_DFL | PQ_IP6, 0,
                        &tunnel_dst_ip6_mask, 0);
        parse_table_add(&pt, "DscpMap", PQ_DFL | PQ_MULTI, 0, &dscp_idx,
                        dscp_names);
        parse_table_add(&pt, "Limit", PQ_DFL | PQ_INT, (void *)0,
                        &entry_limit, 0);
        parse_table_add(&pt, "MinTime", PQ_DFL | PQ_INT, (void *)0,
                        &min_time, 0);
        parse_table_add(&pt, "MaxTime", PQ_DFL | PQ_INT, (void *)0,
                        &max_time, 0);
        parse_table_add(&pt, "MaxIdleTime", PQ_DFL | PQ_INT, (void *)0,
                        &max_idle_time, 0);
        parse_table_add(&pt, "SampleRate", PQ_DFL | PQ_INT, (void *)0,
                        &sample_rate, 0);

        if (parse_arg_eq(arg, &pt) < 0) {
            parse_arg_eq_done(&pt);
            return CMD_USAGE;
        }
        parse_arg_eq_done(&pt);

        sal_memset(&config, 0, sizeof(config));
        config.flags |= enable_l2 ? BCM_IPFIX_CONFIG_ENABLE_NON_IP : 0;
        config.flags |= enable_ip4 ? BCM_IPFIX_CONFIG_ENABLE_IP4 : 0;
        config.flags |= enable_ip6 ? BCM_IPFIX_CONFIG_ENABLE_IP6 : 0;
        config.flags |= check_flow_end ? BCM_IPFIX_CONFIG_TCP_END_DETECT : 0;
        config.flags |= record_non_discard ?
            BCM_IPFIX_CONFIG_RECORD_NON_DISCARD_PKT : 0;
        config.flags |= record_discard ?
            BCM_IPFIX_CONFIG_RECORD_DISCARD_PKT : 0;
        config.flags |= use_l2_for_ip4 ? BCM_IPFIX_CONFIG_KEY_IP4_USE_L2 : 0;
        config.flags |= use_l2_for_ip6 ? BCM_IPFIX_CONFIG_KEY_IP6_USE_L2 : 0;
        config.flags |= key_src_ip ? BCM_IPFIX_CONFIG_KEY_SRC_IP : 0;
        config.flags |= key_dst_ip ? BCM_IPFIX_CONFIG_KEY_DST_IP : 0;
        config.flags |= key_ip_prot ? BCM_IPFIX_CONFIG_KEY_IP_PROT : 0;
        config.flags |= key_ip_dscp ? BCM_IPFIX_CONFIG_KEY_IP_DSCP : 0;
        config.flags |= key_ip_ecn ? BCM_IPFIX_CONFIG_KEY_IP_ECN : 0;
        config.flags |= key_l4_src_port ? BCM_IPFIX_CONFIG_KEY_L4_SRC_PORT : 0;
        config.flags |= key_l4_dst_port ? BCM_IPFIX_CONFIG_KEY_L4_DST_PORT : 0;
        config.flags |= key_ip6_flow ? BCM_IPFIX_CONFIG_KEY_IP6_FLOW : 0;
        config.flags |= key_icmp_type ? BCM_IPFIX_CONFIG_KEY_ICMP_TYPE : 0;
        config.flags |= key_icmp_code ? BCM_IPFIX_CONFIG_KEY_ICMP_CODE : 0;
        config.flags |= key_macda ? BCM_IPFIX_CONFIG_KEY_MACDA : 0;
        config.flags |= key_macsa ? BCM_IPFIX_CONFIG_KEY_MACSA : 0;
        config.flags |= key_vlan_id ? BCM_IPFIX_CONFIG_KEY_VLAN_ID : 0;
        config.flags |= key_vlan_pri ? BCM_IPFIX_CONFIG_KEY_VLAN_PRI : 0;
        config.flags |= key_ether_type ? BCM_IPFIX_CONFIG_KEY_ETHER_TYPE : 0;
        config.flags |= key_vlan_tagged ? BCM_IPFIX_CONFIG_KEY_VLAN_TAGGED : 0;
        config.flags |= key_source_port ?
            BCM_IPFIX_CONFIG_KEY_SOURCE_PORT_OR_INTERFACE : 0;

        if (dscp_idx == 0) {
            for (i = 0; i < 64; i++) {
                config.dscp_mask[i] = i;
            }
        } else {
            for (i = 0; i < 64; i++) {
                config.dscp_mask[i] = 0;
            }
        }
        config.src_ip4_mask = src_ip4_mask;
        config.dst_ip4_mask = dst_ip4_mask;
        config.tunnel_src_ip4_mask = tunnel_src_ip4_mask;
        config.tunnel_dst_ip4_mask = tunnel_dst_ip4_mask;
        SAL_IP6_ADDR_TO_UINT32(src_ip6_mask, (uint32 *)config.src_ip6_mask);
        SAL_IP6_ADDR_TO_UINT32(dst_ip6_mask, (uint32 *)config.dst_ip6_mask);
        SAL_IP6_ADDR_TO_UINT32(tunnel_src_ip6_mask,
                               (uint32 *)config.tunnel_src_ip6_mask);
        SAL_IP6_ADDR_TO_UINT32(tunnel_dst_ip6_mask,
                               (uint32 *)config.tunnel_dst_ip6_mask);
        config.entry_limit = entry_limit;
        config.min_time = min_time;
        config.max_time = max_time;
        config.max_idle_time = max_idle_time;
        config.sample_rate = sample_rate;
        rv = bcm_ipfix_config_set(unit, stage, port, &config);
    } else if (!sal_strcasecmp(subcmd, "get")) {
        parse_table_init(unit, &pt);
        parse_table_add(&pt, "Port", PQ_DFL | PQ_INT, (void *)-1, &port, 0);
        parse_table_add(&pt, "Stage", PQ_DFL | PQ_INT, (void *)0, &stage, 0);
        if (parse_arg_eq(arg, &pt) < 0) {
            parse_arg_eq_done(&pt);
            return CMD_USAGE;
        }
        parse_arg_eq_done(&pt);

        rv = bcm_ipfix_config_get(unit, stage, port, &config);
        if (rv >= 0) {
            if (config.flags & BCM_IPFIX_CONFIG_ENABLE_NON_IP) {
                cli_out("BCM_IPFIX_CONFIG_ENABLE_NON_IP\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_ENABLE_IP4) {
                cli_out("BCM_IPFIX_CONFIG_ENABLE_IP4\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_ENABLE_IP6) {
                cli_out("BCM_IPFIX_CONFIG_ENABLE_IP6\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_TCP_END_DETECT) {
                cli_out("BCM_IPFIX_CONFIG_TCP_END_DETECT\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_RECORD_NON_DISCARD_PKT) {
                cli_out("BCM_IPFIX_CONFIG_RECORD_NON_DISCARD_PKT\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_RECORD_DISCARD_PKT) {
                cli_out("BCM_IPFIX_CONFIG_RECORD_DISCARD_PKT\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_IP4_USE_L2) {
                cli_out("BCM_IPFIX_CONFIG_KEY_IP4_USE_L2\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_IP6_USE_L2) {
                cli_out("BCM_IPFIX_CONFIG_KEY_IP6_USE_L2\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_SRC_IP) {
                cli_out("BCM_IPFIX_CONFIG_KEY_SRC_IP\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_DST_IP) {
                cli_out("BCM_IPFIX_CONFIG_KEY_DST_IP\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_IP_PROT) {
                cli_out("BCM_IPFIX_CONFIG_KEY_IP_PROT\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_IP_DSCP) {
                cli_out("BCM_IPFIX_CONFIG_KEY_IP_DSCP\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_IP_ECN) {
                cli_out("BCM_IPFIX_CONFIG_KEY_IP_ECN\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_L4_SRC_PORT) {
                cli_out("BCM_IPFIX_CONFIG_KEY_L4_SRC_PORT\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_L4_DST_PORT) {
                cli_out("BCM_IPFIX_CONFIG_KEY_L4_DST_PORT\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_IP6_FLOW) {
                cli_out("BCM_IPFIX_CONFIG_KEY_IP6_FLOW\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_ICMP_TYPE) {
                cli_out("BCM_IPFIX_CONFIG_KEY_ICMP_TYPE\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_ICMP_CODE) {
                cli_out("BCM_IPFIX_CONFIG_KEY_ICMP_CODE\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_MACDA) {
                cli_out("BCM_IPFIX_CONFIG_KEY_MACDA\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_MACSA) {
                cli_out("BCM_IPFIX_CONFIG_KEY_MACSA\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_VLAN_ID) {
                cli_out("BCM_IPFIX_CONFIG_KEY_VLAN_ID\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_VLAN_PRI) {
                cli_out("BCM_IPFIX_CONFIG_KEY_VLAN_PRI\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_ETHER_TYPE) {
                cli_out("BCM_IPFIX_CONFIG_KEY_ETHER_TYPE\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_KEY_VLAN_TAGGED) {
                cli_out("BCM_IPFIX_CONFIG_KEY_VLAN_TAGGED\n");
            }
            if (config.flags & BCM_IPFIX_CONFIG_TCP_FLAGS_LAST) {
                cli_out("BCM_IPFIX_CONFIG_TCP_FLAGS_LAST\n");
            }
            cli_out("entry_limit=%d min_time=%d max_time=%d\n",
                    config.entry_limit, config.min_time, config.max_time);
            cli_out("max_idle_time=%d sample_rate=%d\n",
                    config.max_idle_time, config.sample_rate);
            format_ipaddr(str, config.src_ip4_mask);
            cli_out("src_ip4_mask %s\n", str);
            format_ipaddr(str, config.dst_ip4_mask);
            cli_out("dst_ip4_mask %s\n", str);
            format_ip6addr(str, config.src_ip6_mask);
            cli_out("src_ip6_mask %s\n", str);
            format_ip6addr(str, config.dst_ip6_mask);
            cli_out("dst_ip6_mask %s\n", str);
        }
    } else if (!sal_strcasecmp(subcmd, "report")) {
        if (ARG_CNT(arg)) {
            while ((subcmd = ARG_CUR(arg)) != NULL &&
                   !parse_mask(subcmd, ipfix_report_table, &ipfix_report[unit])) {
                ARG_NEXT(arg);        /* Bump arg if matched report */
            }
        } else {                    /* Print values */
            cli_out("IPFIX Reporting on for: ");
            parse_mask_format(50, ipfix_report_table, ipfix_report[unit]);
            cli_out("IPFIX Reporting off for: ");
            parse_mask_format(50, ipfix_report_table, ~ipfix_report[unit]);
        }
    } else {
        return CMD_USAGE;
    }

    if (rv < 0) {
        cli_out("%s: ERROR: %s\n", ARG_CMD(arg), bcm_errmsg(rv));
        return CMD_FAIL;
    }

    return CMD_OK;
}
Example #8
0
File: if.c Project: ariavie/bcm
cmd_result_t
cmd_if_config(int u, args_t *a)
/*
 * Function: 	if_config
 * Purpose:	Perform a variety of configuration functions on an interface.
 * Parameters:	u - unit number to act on.
 *		a - Parameters.
 * Returns:	CMD_USAGE/CMD_FAIL/CMD_OK.
 */
{
    if_soc_t	*ifs;
    int		interface;
    cmd_result_t rv;
    if_soc_t	newif;
#if VX_VERSION == 66 || VX_VERSION == 68
    char gw_cfg_str[32];
#endif

    /*
     * Just dump all of our information.
     */
    if (0 == ARG_CNT(a)) {
	if_dump_table(u, -1, FALSE);
	return(CMD_OK);
    }

    /* Pick up interface number */

    if (!isint(ARG_CUR(a))) {
        cli_out("%s: Invalid interface number: %s\n",
                ARG_CMD(a), ARG_CUR(a));
        return(CMD_FAIL);
    }

    interface = parse_integer(ARG_GET(a));
    if (interface >= MAX_INTERFACE) {
        cli_out("%s: ERROR: Interface # too high, only permitted %d\n",
                ARG_CMD(a), MAX_INTERFACE);
        return(CMD_FAIL);
    }

    /* If no parameters left now, display information on the port */

    if (0 == ARG_CNT(a)) {
	if_dump_table(u, interface, TRUE);
	return(CMD_OK);
    }

    if (if_table[u] == NULL) {
	sal_memset(&newif, 0, sizeof(newif));
	ifs = &newif;
    } else {
	ifs = &if_table[u][interface];
    }
    ifs->ifs_net_name = SOC_END_NAME;
    ifs->ifs_net_interface = interface;

    /* Check for UP/DOWN */

    if (sal_strcasecmp("up", _ARG_CUR(a)) == 0) {
	ARG_NEXT(a);			/* Passed UP */

	if (ifs->ifs_sal) {
	    cli_out("%s: ERROR: Interface %d already running\n",
                    ARG_CMD(a), interface);
	    return(CMD_FAIL);
	}

	/* Parse the Parameters and try to configure the device */

	if (CMD_OK != (rv = if_config_parse(u, ifs, a))) {
	    return(rv);
	}
	if (ARG_CNT(a)) {
	    return(CMD_USAGE);
	}

	ifs->ifs_sal = sal_if_config(ARG_CMD(a), u,
				     ifs->ifs_net_name,
				     ifs->ifs_net_interface,
				     ifs->ifs_net_host,
				     ifs->ifs_net_mac,
				     ifs->ifs_net_vlan,
				     ifs->ifs_net_ip,
				     ifs->ifs_net_mask);

	if (ifs->ifs_sal == NULL) {
	    return CMD_FAIL;
	}
	if (if_table[u] == NULL) {
	    if_table[u] = sal_alloc(sizeof(*ifs) * MAX_INTERFACE, "if_table");
	    if (if_table[u] == NULL) {
		cli_out("%s: ERROR: cannot allocate interface table\n",
                        ARG_CMD(a));
		return CMD_FAIL;
	    }
	    sal_memset(if_table[u], 0, sizeof(*ifs) * MAX_INTERFACE);
	    if_table[u][interface] = *ifs;
	}

        /* Add default gateway */
        if (ifs->ifs_gateway != 0) {
            char          gateway_str[SAL_IPADDR_STR_LEN];

           format_ipaddr(gateway_str, ifs->ifs_gateway);
#if VX_VERSION == 66 || VX_VERSION == 68
            sprintf(gw_cfg_str, "add default %s", gateway_str);
            LOG_INFO(BSL_LS_APPL_END,
                     (BSL_META_U(u,
                                 "Default gateway: %s\n"), gw_cfg_str));
            if (ERROR == routec(gw_cfg_str)) {
                cli_out("Warning: Failed to add default route gatway = %s\n",
                        gateway_str);
            }
#else        
#ifdef VXWORKS_NETWORK_STACK_6_5
#ifdef VXWORKS_NETWORK_STACK_FIXME_SHOW
#error VXWORKS_NETWORK_STACK_FIXME_SHOW
#endif 
#else
           if (OK != routeAdd("0.0.0.0", gateway_str)) {
                cli_out("Warning: Failed to add default route gatway = %s\n",
                        gateway_str);
           }
#endif /* VXWORKS_NETWORK_STACK_6_5 */
#endif
        }
	return CMD_OK;
    } else if (sal_strcasecmp("down", _ARG_CUR(a)) == 0) {
	ARG_NEXT(a);

#if VX_VERSION == 66 || VX_VERSION == 68
        if (ifs->ifs_gateway != 0) {
           char          gateway_str[SAL_IPADDR_STR_LEN];

           format_ipaddr(gateway_str, ifs->ifs_gateway);
           sprintf(gw_cfg_str, "delete default %s", gateway_str);
           if (OK != routec(gw_cfg_str)) {
                cli_out("Warning: Failed to delete default route gatway = %s\n", 
                        gateway_str);
           }
        }
#endif /* VX_VERSION == 66 || VX_VERSION == 68 */

	if (!ifs->ifs_sal) {
	    cli_out("%s: Interface %d not running\n",
                    ARG_CMD(a), interface);
	    return(CMD_FAIL);
	}
	if (sal_if_deconfig(ARG_CMD(a), ifs->ifs_sal, ifs->ifs_net_name,
			    ifs->ifs_net_interface)) {
	    ifs->ifs_sal = NULL;
	    return CMD_FAIL;
	}
	ifs->ifs_sal = NULL;
	return CMD_OK;
    }
    cli_out("%s: ERROR: Invalid option %s: [up|down] expected\n",
            ARG_CMD(a), ARG_CUR(a));
    return(CMD_USAGE);
}
Example #9
0
File: test.c Project: ariavie/bcm
cmd_result_t
test_print_list(int u, args_t *a)
/*
 * Function:    test_print_list
 * Purpose:     Print some/all of the tests and status.
 * Parameters:  u - unit #
 *              "*" for all tests
 *              List of tests
 *              Nothing for all (same as *)
 * Returns:     CMD_OK - success
 *              CMD_FAIL - failed to find test or if a test failed.
 */
{
    test_t      *test;
    char        *s;
    int         header = FALSE;
    int         i, supported, all, fail;
    cmd_result_t result = CMD_OK;
    uint32      chip = _test_chip(u);
    int         num_fail = 0;

    COMPILER_REFERENCE(u);

    /* Check for print all */

    if (ARG_CNT(a) > 0) {
        supported = (0 == strcmp(_ARG_CUR(a), "*"));
        all       = (0 == sal_strcasecmp(_ARG_CUR(a), "all"));
        fail = (0 == sal_strcasecmp(_ARG_CUR(a), "fail"));
        if (supported || all || fail) {         /* Consume Argument  */
            ARG_NEXT(a);
        }
    } else if (ARG_CNT(a) == 0) {
        supported = TRUE;
        all       = FALSE;
        fail   = FALSE;
    } else {
        supported = FALSE;
        all       = FALSE;
        fail   = FALSE;
    }   

    /* If "all" or supported, consume argument and list tests */

        if (fail) {
            for (i = 0; i < test_cnt; i++) {
                if (test_list[i].t_fail) {
                    cli_out("%d  ", test_list[i].t_test);
                    num_fail++;
                }
            }
            if (num_fail == 0) {
                    cli_out("All tests passed");
            }
            cli_out("\n");
            return(result);
        }

    if (all || supported) {
        int t_loops = 0, t_runs = 0, t_success = 0, t_fail = 0;
        if (chip == 0) {                /* No chip - print all */
            all = TRUE;
        }
        test_print_header(TRUE);
        test_print_separator();
        for (i = 0; i < test_cnt; i++) {
            if (all || 
                (supported && (chip & test_list[i].t_flags)) 
                || (test_list[i].t_flags & T_F_SEL_ALL)) {
                test_print_entry(u, &test_list[i]);
                t_loops += test_list[i].t_loops;
                t_runs += test_list[i].t_runs;
                t_success += test_list[i].t_success;
                t_fail += test_list[i].t_fail;
            }
        }
        test_print_summary(t_loops, t_runs, t_success, t_fail);
        test_print_separator();

        /*
          In order for the automated tests to automatically diagnose
          that a test failed, we need this function to return 
          something when an individual test fails.
         */
        if (t_runs == 0) {
            /*
             * We want return CMD_FAIL if no tests were run 
             */
            result = CMD_FAIL;
        } else if (t_fail > 0) {
            /*
             * We also want to return CMD_FAIL if any tests failed.
             */
            result = CMD_FAIL;
        }
        return(result);
    } 

    while ((s = ARG_GET(a)) != NULL) {
        test = test_find(s);
        if (!test) {
            cli_out("%s: Unable to locate test: %s\n", ARG_CMD(a), s);
            result = CMD_FAIL;
            continue;
        } 
        if (!header) {
            test_print_header(FALSE);
            test_print_separator();
            header = TRUE;
        }
        test_print_entry(u, test);
    }
    return(result);
}