/* * Copyright (c) 2015 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. */ /* Copyright (c) 2005 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Turn data structures into byte streams for saving or transport. */ #include #include #include void serialize_64 (serialize_main_t * m, va_list * va) { u64 x = va_arg (*va, u64); u32 lo, hi; lo = x; hi = x >> 32; serialize_integer (m, lo, sizeof (lo)); serialize_integer (m, hi, sizeof (hi)); } void serialize_32 (serialize_main_t * m, va_list * va) { u32 x = va_arg (*va, u32); serialize_integer (m, x, sizeof (x)); } void serialize_16 (serialize_main_t * m, va_list * va) { u32 x = va_arg (*va, u32); serialize_integer (m, x, sizeof (u16)); } void serialize_8 (serialize_main_t * m, va_list * va) { u32 x = va_arg (*va, u32); serialize_integer (m, x, sizeof (u8)); } void unserialize_64 (serialize_main_t * m, va_list * va) { u64 *x = va_arg (*va, u64 *); u32 lo, hi; unserialize_integer (m, &lo, sizeof (lo)); unserialize_integer (m, &hi, sizeof (hi)); *x = ((u64) hi << 32) | (u64) lo; } void unserialize_32 (serialize_main_t * m, va_list * va) { u32 *x = va_arg (*va, u32 *); unserialize_integer (m, x, sizeof (x[0])); } void unserialize_16 (serialize_main_t * m, va_list * va) { u16 *x = va_arg (*va, u16 *); u32 t; unserialize_integer (m, &t, sizeof (x[0])); x[0] = t; } void unserialize_8 (serialize_main_t * m, va_list * va) { u8 *x = va_arg (*va, u8 *); u32 t; unserialize_integer (m, &t, sizeof (x[0])); x[0] = t; } void serialize_f64 (serialize_main_t * m, va_list * va) { f64 x = va_arg (*va, f64); union { f64 f; u64 i; } y; y.f = x; serialize (m, serialize_64, y.i); } void serialize_f32 (serialize_main_t * m, va_list * va) { f32 x = va_arg (*va, f64); union { f32 f; u32 i; } y; y.f = x; serialize_integer (m, y.i, sizeof (y.i)); } void unserialize_f64 (serialize_main_t * m, va_list * va) { f64 *x = va_arg (*va, f64 *); union { f64 f; u64 i; } y; unserialize (m, unserialize_64, &y.i); *x = y.f; } void unserialize_f32 (serialize_main_t * m, va_list * va) { f32 *x = va_arg (*va, f32 *); union { f32 f; u32 i; } y; unserialize_integer (m, &y.i, sizeof (y.i)); *x = y.f; } void serialize_cstring (serialize_main_t * m, char *s) { u32 len = s ? strlen (s) : 0; void *p; serialize_likely_small_unsigned_integer (m, len); if (len > 0) { p = serialize_get (m, len); clib_memcpy_fast (p, s, len); } } void unserialize_cstring (serialize_main_t * m, char **s) { char *p, *r = 0; u32 len; len = unserialize_likely_small_unsigned_integer (m); /* * Given broken enough data, we could get len = 0xFFFFFFFF. * Add one, it overflows, we call vec_new (char, 0), then * memcpy until we bus error. */ if (len > 0 && len != 0xFFFFFFFF) { r = vec_new (char, len + 1); p = unserialize_get (m, len); clib_memcpy_fast (r, p, len); /* Null terminate. */ r[len] = 0; } *s = r; } /* vec_serialize/vec_unserialize helper functions for basic vector types. */ void serialize_vec_8 (serialize_main_t * m, va_list * va) { u8 *s = va_arg (*va, u8 *); u32 n = va_arg (*va, u32); u8 *p = serialize_get (m, n * sizeof (u8)); clib_memcpy_fast (p, s, n * sizeof (u8)); } void unserialize_vec_8 (serialize_main_t * m, va_list * va) { u8 *s = va_arg (*va, u8 *); u32 n = va_arg (*va, u32); u8 *p = unserialize_get (m, n); clib_memcpy_fast (s, p, n); } #define _(n_bits) \ void serialize_vec_##n_bits (serialize_main_t * m, va_list * va) \ { \ u##n_bits * s = va_arg (*va, u##n_bits *); \ u32 n = va_arg (*va, u32); \ u##n_bits * p = serialize_get (m, n * sizeof (s[0])); \ \ while (n >= 4) \ { \ p[0] = clib_host_to_net_u##n_bits (s[0]); \ p[1] = clib_host_to_net_u##n_bits (s[1]); \ p[2] = clib_host_to_net_u##n_bits (s[2]); \ p[3] = clib_host_to_net_u##n_bits (s[3]); \ s += 4; \ p += 4; \ n -= 4; \ } \ \ while (n >= 1) \ { \ p[0] = clib_host_to_net_u##n_bits (s[0]); \ s += 1; \ p += 1; \ n -= 1; \ } \ } \ \ void unserialize_vec_##n_bits (serialize_main_t * m, va_list * va) \ { \ u##n_bits * s = va_arg (*va, u##n_bits *); \ u32 n = va_arg (*va, u32); \ u##n_bits * p = unserialize_get (m, n * sizeof (s[0])); \ \ while (n >= 4) \ { \ s[0] = clib_net_to_host_mem_u##n_bits (&p[0]); \ s[1] = clib_net_to_host_mem_u##n_bits (&p[1]); \ s[2] = clib_net_to_host_mem_u##n_bits (&p[2]); \ s[3] = clib_net_to_host_mem_u##n_bits (&p[3]); \ s += 4; \ p += 4; \ n -= 4; \ } \ \ while (n >= 1) \ { \ s[0] = clib_net_to_host_mem_u##n_bits (&p[0]); \ s += 1; \ p += 1; \ n -= 1; \ } \ } _(16); _(32); _(64); #undef _ #define SERIALIZE_VECTOR_CHUNK_SIZE 64 void serialize_vector (serialize_main_t * m, va_list * va) { void *vec = va_arg (*va, void *); u32 elt_bytes = va_arg (*va, u32); serialize_function_t *f = va_arg (*va, serialize_function_t *); u32 l = vec_len (vec); void *p = vec; serialize_integer (m, l, sizeof (l)); /* Serialize vector in chunks for cache locality. */ while (l != 0) { u32 n = clib_min (SERIALIZE_VECTOR_CHUNK_SIZE, l); serialize (m, f, p, n); l -= n; p += SERIALIZE_VECTOR_CHUNK_SIZE * elt_bytes; } } void * unserialize_vector_ha (serialize_main_t * m, u32 elt_bytes, u32 header_bytes, u32 align, u32 max_length, serialize_function_t * f) { void *v, *p; u32 l; unserialize_integer (m, &l, sizeof (l)); if (l > max_length) serialize_error (&m->header, clib_error_create ("bad vector length %d", l)); p = v = _vec_resize ((void *) 0, l, (uword) l * elt_bytes, header_bytes, /* align */ align); while (l != 0) { u32 n = clib_min (SERIALIZE_VECTOR_CHUNK_SIZE, l); unserialize (m, f, p, n); l -= n; p += SERIALIZE_VECTOR_CHUNK_SIZE * elt_bytes; } return v; } void unserialize_aligned_vector (serialize_main_t * m, va_list * va) { void **vec = va_arg (*va, void **); u32 elt_bytes = va_arg (*va, u32); serialize_function_t *f = va_arg (*va, serialize_function_t *); u32 align = va_arg (*va, u32); *vec = unserialize_vector_ha (m, elt_bytes, /* header_bytes */ 0, /* align */ align, /* max_length */ ~0, f); } void unserialize_vector (serialize_main_t * m, va_list * va) { void **vec = va_arg (*va, void **); u32 elt_bytes = va_arg (*va, u32); serialize_function_t *f = va_arg (*va, serialize_function_t *); *vec = unserialize_vector_ha (m, elt_bytes, /* header_bytes */ 0, /* align */ 0, /* max_length */ ~0, f); } void serialize_bitmap (serialize_main_t * m, uword * b) { u32 l, i, n_u32s; l = vec_len (b); n_u32s = l * sizeof (b[0]) / sizeof (u32); serialize_integer (m, n_u32s, sizeof (n_u32s)); /* Send 32 bit words, low-order word first on 64 bit. */ for (i = 0; i < l; i++) { serialize_integer (m, b[i], sizeof (u32)); if (BITS (uword) == 64) serialize_integer (m, (u64) b[i] >> (u64) 32, sizeof (u32)); } } uword * unserialize_bitmap (serialize_main_t * m) { uword *b = 0; u32 i, n_u32s; unserialize_integer (m, &n_u32s, sizeof (n_u32s)); if (n_u32s == 0) return b; i = (n_u32s * size
/*
 * http_static.c - skeleton vpp-api-test plug-in
 *
 * Copyright (c) <current-year> <your-organization>
 * 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 <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vppinfra/error.h>

uword unformat_sw_if_index (unformat_input_t * input, va_list * args);

/* Declare message IDs */
#include <http_static/http_static.api_enum.h>
#include <http_static/http_static.api_types.h>

typedef struct
{
  /* API message ID base */
  u16 msg_id_base;
  vat_main_t *vat_main;
} http_static_test_main_t;

http_static_test_main_t http_static_test_main;

#define __plugin_msg_base http_static_test_main.msg_id_base
#include <vlibapi/vat_helper_macros.h>

static int
api_http_static_enable (vat_main_t * vam)
{
  unformat_input_t *line_input = vam->input;
  vl_api_http_static_enable_t *mp;
  u64 tmp;
  u8 *www_root = 0;
  u8 *uri = 0;
  u32 prealloc_fifos = 0;
  u32 private_segment_size = 0;
  u32 fifo_size = 8 << 10;
  u32 cache_size_limit = 1 << 20;
  int ret;

  /* Parse args required to build the message */
  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "www-root %s", &www_root))
	;
      else if (unformat (line_input, "prealloc-fifos %d", &prealloc_fifos))
	;
      else if (unformat (line_input, "private-segment-size %U",
			 unformat_memory_size, &tmp))
	{
	  if (tmp >= 0x100000000ULL)
	    {
	      errmsg ("private segment size %llu, too large", tmp);
	      return -99;
	    }
	  private_segment_size = (u32) tmp;
	}
      else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
			 &tmp))
	{
	  if (tmp >= 0x100000000ULL)
	    {
	      errmsg ("fifo-size %llu, too large", tmp);
	      return -99;
	    }
	  fifo_size = (u32) tmp;
	}
      else if (unformat (line_input, "cache-size %U", unformat_memory_size,
			 &tmp))
	{
	  if (tmp < (128ULL << 10))
	    {
	      errmsg ("cache-size must be at least 128kb");
	      return -99;
	    }
	  cache_size_limit = (u32) tmp;
	}

      else if (unformat (line_input, "uri %s", &uri))
	;
      else
	{
	  errmsg ("unknown input `%U'", format_unformat_error, line_input);
	  return -99;
	}
    }

  if (www_root == 0)
    {
      errmsg ("Must specify www-root");
      return -99;
    }

  if (uri == 0)
    uri = format (0, "tcp://0.0.0.0/80%c", 0);



  /* Construct the API message */
  M (HTTP_STATIC_ENABLE, mp);
  strncpy_s ((char *) mp->www_root, 256, (const char *) www_root