VPP-254 Coding standard cleanup - vnet/vnet/hdlc

Change-Id: I125b1ca20a5b30d199d4a79ad0034533818a5e9c
Signed-off-by: Swarup Nayak <swarupnpvt@gmail.com>
This commit is contained in:
Swarup Nayak
2017-11-22 16:41:57 +05:30
committed by Chris Luke
parent eb2945cf6f
commit cc42db82f8
5 changed files with 197 additions and 126 deletions

View File

@ -43,11 +43,12 @@
/* Global main structure. */ /* Global main structure. */
hdlc_main_t hdlc_main; hdlc_main_t hdlc_main;
u8 * format_hdlc_protocol (u8 * s, va_list * args) u8 *
format_hdlc_protocol (u8 * s, va_list * args)
{ {
hdlc_protocol_t p = va_arg (*args, u32); hdlc_protocol_t p = va_arg (*args, u32);
hdlc_main_t * pm = &hdlc_main; hdlc_main_t *pm = &hdlc_main;
hdlc_protocol_info_t * pi = hdlc_get_protocol_info (pm, p); hdlc_protocol_info_t *pi = hdlc_get_protocol_info (pm, p);
if (pi) if (pi)
s = format (s, "%s", pi->name); s = format (s, "%s", pi->name);
@ -57,10 +58,11 @@ u8 * format_hdlc_protocol (u8 * s, va_list * args)
return s; return s;
} }
u8 * format_hdlc_header_with_length (u8 * s, va_list * args) u8 *
format_hdlc_header_with_length (u8 * s, va_list * args)
{ {
hdlc_main_t * pm = &hdlc_main; hdlc_main_t *pm = &hdlc_main;
hdlc_header_t * h = va_arg (*args, hdlc_header_t *); hdlc_header_t *h = va_arg (*args, hdlc_header_t *);
u32 max_header_bytes = va_arg (*args, u32); u32 max_header_bytes = va_arg (*args, u32);
hdlc_protocol_t p = clib_net_to_host_u16 (h->protocol); hdlc_protocol_t p = clib_net_to_host_u16 (h->protocol);
u32 indent, header_bytes; u32 indent, header_bytes;
@ -80,8 +82,8 @@ u8 * format_hdlc_header_with_length (u8 * s, va_list * args)
if (max_header_bytes != 0 && header_bytes > max_header_bytes) if (max_header_bytes != 0 && header_bytes > max_header_bytes)
{ {
hdlc_protocol_info_t * pi = hdlc_get_protocol_info (pm, p); hdlc_protocol_info_t *pi = hdlc_get_protocol_info (pm, p);
vlib_node_t * node = vlib_get_node (pm->vlib_main, pi->node_index); vlib_node_t *node = vlib_get_node (pm->vlib_main, pi->node_index);
if (node->format_buffer) if (node->format_buffer)
s = format (s, "\n%U%U", s = format (s, "\n%U%U",
format_white_space, indent, format_white_space, indent,
@ -92,24 +94,24 @@ u8 * format_hdlc_header_with_length (u8 * s, va_list * args)
return s; return s;
} }
u8 * format_hdlc_header (u8 * s, va_list * args) u8 *
format_hdlc_header (u8 * s, va_list * args)
{ {
hdlc_header_t * h = va_arg (*args, hdlc_header_t *); hdlc_header_t *h = va_arg (*args, hdlc_header_t *);
return format (s, "%U", format_hdlc_header_with_length, h, 0); return format (s, "%U", format_hdlc_header_with_length, h, 0);
} }
/* Returns hdlc protocol as an int in host byte order. */ /* Returns hdlc protocol as an int in host byte order. */
uword uword
unformat_hdlc_protocol_host_byte_order (unformat_input_t * input, unformat_hdlc_protocol_host_byte_order (unformat_input_t * input,
va_list * args) va_list * args)
{ {
u16 * result = va_arg (*args, u16 *); u16 *result = va_arg (*args, u16 *);
hdlc_main_t * pm = &hdlc_main; hdlc_main_t *pm = &hdlc_main;
int p, i; int p, i;
/* Numeric type. */ /* Numeric type. */
if (unformat (input, "0x%x", &p) if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
|| unformat (input, "%d", &p))
{ {
if (p >= (1 << 16)) if (p >= (1 << 16))
return 0; return 0;
@ -121,7 +123,7 @@ unformat_hdlc_protocol_host_byte_order (unformat_input_t * input,
if (unformat_user (input, unformat_vlib_number_by_name, if (unformat_user (input, unformat_vlib_number_by_name,
pm->protocol_info_by_name, &i)) pm->protocol_info_by_name, &i))
{ {
hdlc_protocol_info_t * pi = vec_elt_at_index (pm->protocol_infos, i); hdlc_protocol_info_t *pi = vec_elt_at_index (pm->protocol_infos, i);
*result = pi->protocol; *result = pi->protocol;
return 1; return 1;
} }
@ -131,24 +133,23 @@ unformat_hdlc_protocol_host_byte_order (unformat_input_t * input,
uword uword
unformat_hdlc_protocol_net_byte_order (unformat_input_t * input, unformat_hdlc_protocol_net_byte_order (unformat_input_t * input,
va_list * args) va_list * args)
{ {
u16 * result = va_arg (*args, u16 *); u16 *result = va_arg (*args, u16 *);
if (! unformat_user (input, unformat_hdlc_protocol_host_byte_order, result)) if (!unformat_user (input, unformat_hdlc_protocol_host_byte_order, result))
return 0; return 0;
*result = clib_host_to_net_u16 ((u16) *result); *result = clib_host_to_net_u16 ((u16) * result);
return 1; return 1;
} }
uword uword
unformat_hdlc_header (unformat_input_t * input, va_list * args) unformat_hdlc_header (unformat_input_t * input, va_list * args)
{ {
u8 ** result = va_arg (*args, u8 **); u8 **result = va_arg (*args, u8 **);
hdlc_header_t _h, * h = &_h; hdlc_header_t _h, *h = &_h;
u16 p; u16 p;
if (! unformat (input, "%U", if (!unformat (input, "%U", unformat_hdlc_protocol_host_byte_order, &p))
unformat_hdlc_protocol_host_byte_order, &p))
return 0; return 0;
h->address = 0xff; h->address = 0xff;
@ -157,7 +158,7 @@ unformat_hdlc_header (unformat_input_t * input, va_list * args)
/* Add header to result. */ /* Add header to result. */
{ {
void * p; void *p;
u32 n_bytes = sizeof (h[0]); u32 n_bytes = sizeof (h[0]);
vec_add2 (*result, p, n_bytes); vec_add2 (*result, p, n_bytes);
@ -167,28 +168,28 @@ unformat_hdlc_header (unformat_input_t * input, va_list * args)
return 1; return 1;
} }
static u8* static u8 *
hdlc_build_rewrite (vnet_main_t * vnm, hdlc_build_rewrite (vnet_main_t * vnm,
u32 sw_if_index, u32 sw_if_index,
vnet_link_t link_type, vnet_link_t link_type, const void *dst_address)
const void *dst_address)
{ {
hdlc_header_t * h; hdlc_header_t *h;
u8* rewrite = NULL; u8 *rewrite = NULL;
hdlc_protocol_t protocol; hdlc_protocol_t protocol;
switch (link_type) { switch (link_type)
{
#define _(a,b) case VNET_LINK_##a: protocol = HDLC_PROTOCOL_##b; break #define _(a,b) case VNET_LINK_##a: protocol = HDLC_PROTOCOL_##b; break
_ (IP4, ip4); _(IP4, ip4);
_ (IP6, ip6); _(IP6, ip6);
_ (MPLS, mpls_unicast); _(MPLS, mpls_unicast);
#undef _ #undef _
default: default:
return (NULL); return (NULL);
} }
vec_validate(rewrite, sizeof(*h)-1); vec_validate (rewrite, sizeof (*h) - 1);
h = (hdlc_header_t *)rewrite; h = (hdlc_header_t *) rewrite;
h->address = 0x0f; h->address = 0x0f;
h->control = 0x00; h->control = 0x00;
h->protocol = clib_host_to_net_u16 (protocol); h->protocol = clib_host_to_net_u16 (protocol);
@ -196,6 +197,7 @@ hdlc_build_rewrite (vnet_main_t * vnm,
return (rewrite); return (rewrite);
} }
/* *INDENT-OFF* */
VNET_HW_INTERFACE_CLASS (hdlc_hw_interface_class) = { VNET_HW_INTERFACE_CLASS (hdlc_hw_interface_class) = {
.name = "HDLC", .name = "HDLC",
.format_header = format_hdlc_header_with_length, .format_header = format_hdlc_header_with_length,
@ -203,12 +205,12 @@ VNET_HW_INTERFACE_CLASS (hdlc_hw_interface_class) = {
.build_rewrite = hdlc_build_rewrite, .build_rewrite = hdlc_build_rewrite,
.flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P, .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
}; };
/* *INDENT-ON* */
static void add_protocol (hdlc_main_t * pm, static void
hdlc_protocol_t protocol, add_protocol (hdlc_main_t * pm, hdlc_protocol_t protocol, char *protocol_name)
char * protocol_name)
{ {
hdlc_protocol_info_t * pi; hdlc_protocol_info_t *pi;
u32 i; u32 i;
vec_add2 (pm->protocol_infos, pi, 1); vec_add2 (pm->protocol_infos, pi, 1);
@ -222,9 +224,10 @@ static void add_protocol (hdlc_main_t * pm,
hash_set_mem (pm->protocol_info_by_name, pi->name, i); hash_set_mem (pm->protocol_info_by_name, pi->name, i);
} }
static clib_error_t * hdlc_init (vlib_main_t * vm) static clib_error_t *
hdlc_init (vlib_main_t * vm)
{ {
hdlc_main_t * pm = &hdlc_main; hdlc_main_t *pm = &hdlc_main;
memset (pm, 0, sizeof (pm[0])); memset (pm, 0, sizeof (pm[0]));
pm->vlib_main = vm; pm->vlib_main = vm;
@ -235,15 +238,23 @@ static clib_error_t * hdlc_init (vlib_main_t * vm)
#define _(n,s) add_protocol (pm, HDLC_PROTOCOL_##s, #s); #define _(n,s) add_protocol (pm, HDLC_PROTOCOL_##s, #s);
foreach_hdlc_protocol foreach_hdlc_protocol
#undef _ #undef _
return vlib_call_init_function (vm, hdlc_input_init);
return vlib_call_init_function (vm, hdlc_input_init);
} }
VLIB_INIT_FUNCTION (hdlc_init); VLIB_INIT_FUNCTION (hdlc_init);
hdlc_main_t * hdlc_get_main (vlib_main_t * vm) hdlc_main_t *
hdlc_get_main (vlib_main_t * vm)
{ {
vlib_call_init_function (vm, hdlc_init); vlib_call_init_function (vm, hdlc_init);
return &hdlc_main; return &hdlc_main;
} }
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -46,16 +46,18 @@
extern vnet_hw_interface_class_t hdlc_hw_interface_class; extern vnet_hw_interface_class_t hdlc_hw_interface_class;
typedef enum { typedef enum
{
#define hdlc_error(n,s) HDLC_ERROR_##n, #define hdlc_error(n,s) HDLC_ERROR_##n,
#include <vnet/hdlc/error.def> #include <vnet/hdlc/error.def>
#undef hdlc_error #undef hdlc_error
HDLC_N_ERROR, HDLC_N_ERROR,
} hdlc_error_t; } hdlc_error_t;
typedef struct { typedef struct
{
/* Name (a c string). */ /* Name (a c string). */
char * name; char *name;
/* HDLC protocol type in host byte order. */ /* HDLC protocol type in host byte order. */
hdlc_protocol_t protocol; hdlc_protocol_t protocol;
@ -67,19 +69,20 @@ typedef struct {
u32 next_index; u32 next_index;
} hdlc_protocol_info_t; } hdlc_protocol_info_t;
typedef struct { typedef struct
vlib_main_t * vlib_main; {
vlib_main_t *vlib_main;
hdlc_protocol_info_t * protocol_infos; hdlc_protocol_info_t *protocol_infos;
/* Hash tables mapping name/protocol to protocol info index. */ /* Hash tables mapping name/protocol to protocol info index. */
uword * protocol_info_by_name, * protocol_info_by_protocol; uword *protocol_info_by_name, *protocol_info_by_protocol;
} hdlc_main_t; } hdlc_main_t;
always_inline hdlc_protocol_info_t * always_inline hdlc_protocol_info_t *
hdlc_get_protocol_info (hdlc_main_t * em, hdlc_protocol_t protocol) hdlc_get_protocol_info (hdlc_main_t * em, hdlc_protocol_t protocol)
{ {
uword * p = hash_get (em->protocol_info_by_protocol, protocol); uword *p = hash_get (em->protocol_info_by_protocol, protocol);
return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0; return p ? vec_elt_at_index (em->protocol_infos, p[0]) : 0;
} }
@ -88,8 +91,7 @@ extern hdlc_main_t hdlc_main;
/* Register given node index to take input for given hdlc type. */ /* Register given node index to take input for given hdlc type. */
void void
hdlc_register_input_type (vlib_main_t * vm, hdlc_register_input_type (vlib_main_t * vm,
hdlc_protocol_t protocol, hdlc_protocol_t protocol, u32 node_index);
u32 node_index);
format_function_t format_hdlc_protocol; format_function_t format_hdlc_protocol;
format_function_t format_hdlc_header; format_function_t format_hdlc_header;
@ -107,8 +109,8 @@ unformat_function_t unformat_pg_hdlc_header;
always_inline void always_inline void
hdlc_setup_node (vlib_main_t * vm, u32 node_index) hdlc_setup_node (vlib_main_t * vm, u32 node_index)
{ {
vlib_node_t * n = vlib_get_node (vm, node_index); vlib_node_t *n = vlib_get_node (vm, node_index);
pg_node_t * pn = pg_get_node (node_index); pg_node_t *pn = pg_get_node (node_index);
n->format_buffer = format_hdlc_header_with_length; n->format_buffer = format_hdlc_header_with_length;
n->unformat_buffer = unformat_hdlc_header; n->unformat_buffer = unformat_hdlc_header;
@ -117,7 +119,14 @@ hdlc_setup_node (vlib_main_t * vm, u32 node_index)
void void
hdlc_register_input_protocol (vlib_main_t * vm, hdlc_register_input_protocol (vlib_main_t * vm,
hdlc_protocol_t protocol, hdlc_protocol_t protocol, u32 node_index);
u32 node_index);
#endif /* included_hdlc_h */ #endif /* included_hdlc_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -46,43 +46,46 @@
_ (PUNT, "error-punt") \ _ (PUNT, "error-punt") \
_ (DROP, "error-drop") _ (DROP, "error-drop")
typedef enum { typedef enum
{
#define _(s,n) HDLC_INPUT_NEXT_##s, #define _(s,n) HDLC_INPUT_NEXT_##s,
foreach_hdlc_input_next foreach_hdlc_input_next
#undef _ #undef _
HDLC_INPUT_N_NEXT, HDLC_INPUT_N_NEXT,
} hdlc_input_next_t; } hdlc_input_next_t;
typedef struct { typedef struct
{
u8 packet_data[32]; u8 packet_data[32];
} hdlc_input_trace_t; } hdlc_input_trace_t;
static u8 * format_hdlc_input_trace (u8 * s, va_list * va) static u8 *
format_hdlc_input_trace (u8 * s, va_list * va)
{ {
CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *); CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *); CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
hdlc_input_trace_t * t = va_arg (*va, hdlc_input_trace_t *); hdlc_input_trace_t *t = va_arg (*va, hdlc_input_trace_t *);
s = format (s, "%U", format_hdlc_header, t->packet_data); s = format (s, "%U", format_hdlc_header, t->packet_data);
return s; return s;
} }
typedef struct { typedef struct
{
/* Sparse vector mapping hdlc protocol in network byte order /* Sparse vector mapping hdlc protocol in network byte order
to next index. */ to next index. */
u16 * next_by_protocol; u16 *next_by_protocol;
u32 * sparse_index_by_next_index; u32 *sparse_index_by_next_index;
} hdlc_input_runtime_t; } hdlc_input_runtime_t;
static uword static uword
hdlc_input (vlib_main_t * vm, hdlc_input (vlib_main_t * vm,
vlib_node_runtime_t * node, vlib_node_runtime_t * node, vlib_frame_t * from_frame)
vlib_frame_t * from_frame)
{ {
hdlc_input_runtime_t * rt = (void *) node->runtime_data; hdlc_input_runtime_t *rt = (void *) node->runtime_data;
u32 n_left_from, next_index, i_next, * from, * to_next; u32 n_left_from, next_index, i_next, *from, *to_next;
from = vlib_frame_vector_args (from_frame); from = vlib_frame_vector_args (from_frame);
n_left_from = from_frame->n_vectors; n_left_from = from_frame->n_vectors;
@ -101,19 +104,18 @@ hdlc_input (vlib_main_t * vm,
{ {
u32 n_left_to_next; u32 n_left_to_next;
vlib_get_next_frame (vm, node, next_index, vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
to_next, n_left_to_next);
while (n_left_from >= 4 && n_left_to_next >= 2) while (n_left_from >= 4 && n_left_to_next >= 2)
{ {
u32 bi0, bi1; u32 bi0, bi1;
vlib_buffer_t * b0, * b1; vlib_buffer_t *b0, *b1;
hdlc_header_t * h0, * h1; hdlc_header_t *h0, *h1;
u32 i0, i1, len0, len1, protocol0, protocol1, enqueue_code; u32 i0, i1, len0, len1, protocol0, protocol1, enqueue_code;
/* Prefetch next iteration. */ /* Prefetch next iteration. */
{ {
vlib_buffer_t * b2, * b3; vlib_buffer_t *b2, *b3;
b2 = vlib_get_buffer (vm, from[2]); b2 = vlib_get_buffer (vm, from[2]);
b3 = vlib_get_buffer (vm, from[3]); b3 = vlib_get_buffer (vm, from[3]);
@ -157,12 +159,19 @@ hdlc_input (vlib_main_t * vm,
b1->current_length -= len1; b1->current_length -= len1;
/* Index sparse array with network byte order. */ /* Index sparse array with network byte order. */
sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0, &i1); sparse_vec_index2 (rt->next_by_protocol, protocol0, protocol1, &i0,
&i1);
b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE]; b0->error =
b1->error = node->errors[i1 == SPARSE_VEC_INVALID_INDEX ? HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE]; node->errors[i0 ==
SPARSE_VEC_INVALID_INDEX ?
HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
b1->error =
node->errors[i1 ==
SPARSE_VEC_INVALID_INDEX ?
HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
enqueue_code = (i0 != i_next) + 2*(i1 != i_next); enqueue_code = (i0 != i_next) + 2 * (i1 != i_next);
if (PREDICT_FALSE (enqueue_code != 0)) if (PREDICT_FALSE (enqueue_code != 0))
{ {
@ -173,29 +182,38 @@ hdlc_input (vlib_main_t * vm,
to_next[-2] = bi1; to_next[-2] = bi1;
to_next -= 1; to_next -= 1;
n_left_to_next += 1; n_left_to_next += 1;
vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i0), bi0); vlib_set_next_frame_buffer (vm, node,
vec_elt (rt->next_by_protocol,
i0), bi0);
break; break;
case 2: case 2:
/* A A B */ /* A A B */
to_next -= 1; to_next -= 1;
n_left_to_next += 1; n_left_to_next += 1;
vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i1), bi1); vlib_set_next_frame_buffer (vm, node,
vec_elt (rt->next_by_protocol,
i1), bi1);
break; break;
case 3: case 3:
/* A B B or A B C */ /* A B B or A B C */
to_next -= 2; to_next -= 2;
n_left_to_next += 2; n_left_to_next += 2;
vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i0), bi0); vlib_set_next_frame_buffer (vm, node,
vlib_set_next_frame_buffer (vm, node, vec_elt (rt->next_by_protocol, i1), bi1); vec_elt (rt->next_by_protocol,
i0), bi0);
vlib_set_next_frame_buffer (vm, node,
vec_elt (rt->next_by_protocol,
i1), bi1);
if (i0 == i1) if (i0 == i1)
{ {
vlib_put_next_frame (vm, node, next_index, vlib_put_next_frame (vm, node, next_index,
n_left_to_next); n_left_to_next);
i_next = i1; i_next = i1;
next_index = vec_elt (rt->next_by_protocol, i_next); next_index = vec_elt (rt->next_by_protocol, i_next);
vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); vlib_get_next_frame (vm, node, next_index, to_next,
n_left_to_next);
} }
} }
} }
@ -204,8 +222,8 @@ hdlc_input (vlib_main_t * vm,
while (n_left_from > 0 && n_left_to_next > 0) while (n_left_from > 0 && n_left_to_next > 0)
{ {
u32 bi0; u32 bi0;
vlib_buffer_t * b0; vlib_buffer_t *b0;
hdlc_header_t * h0; hdlc_header_t *h0;
u32 i0, len0, protocol0; u32 i0, len0, protocol0;
bi0 = from[0]; bi0 = from[0];
@ -230,7 +248,10 @@ hdlc_input (vlib_main_t * vm,
i0 = sparse_vec_index (rt->next_by_protocol, protocol0); i0 = sparse_vec_index (rt->next_by_protocol, protocol0);
b0->error = node->errors[i0 == SPARSE_VEC_INVALID_INDEX ? HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE]; b0->error =
node->errors[i0 ==
SPARSE_VEC_INVALID_INDEX ?
HDLC_ERROR_UNKNOWN_PROTOCOL : HDLC_ERROR_NONE];
/* Sent packet to wrong next? */ /* Sent packet to wrong next? */
if (PREDICT_FALSE (i0 != i_next)) if (PREDICT_FALSE (i0 != i_next))
@ -256,12 +277,13 @@ hdlc_input (vlib_main_t * vm,
return from_frame->n_vectors; return from_frame->n_vectors;
} }
static char * hdlc_error_strings[] = { static char *hdlc_error_strings[] = {
#define hdlc_error(n,s) s, #define hdlc_error(n,s) s,
#include "error.def" #include "error.def"
#undef hdlc_error #undef hdlc_error
}; };
/* *INDENT-OFF* */
VLIB_REGISTER_NODE (hdlc_input_node) = { VLIB_REGISTER_NODE (hdlc_input_node) = {
.function = hdlc_input, .function = hdlc_input,
.name = "hdlc-input", .name = "hdlc-input",
@ -284,14 +306,16 @@ VLIB_REGISTER_NODE (hdlc_input_node) = {
.format_trace = format_hdlc_input_trace, .format_trace = format_hdlc_input_trace,
.unformat_buffer = unformat_hdlc_header, .unformat_buffer = unformat_hdlc_header,
}; };
/* *INDENT-ON* */
static clib_error_t * hdlc_input_runtime_init (vlib_main_t * vm) static clib_error_t *
hdlc_input_runtime_init (vlib_main_t * vm)
{ {
hdlc_input_runtime_t * rt; hdlc_input_runtime_t *rt;
rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index); rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
rt->next_by_protocol = sparse_vec_new rt->next_by_protocol = sparse_vec_new
(/* elt bytes */ sizeof (rt->next_by_protocol[0]), ( /* elt bytes */ sizeof (rt->next_by_protocol[0]),
/* bits in index */ BITS (((hdlc_header_t *) 0)->protocol)); /* bits in index */ BITS (((hdlc_header_t *) 0)->protocol));
vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_DROP); vec_validate (rt->sparse_index_by_next_index, HDLC_INPUT_NEXT_DROP);
@ -304,11 +328,12 @@ static clib_error_t * hdlc_input_runtime_init (vlib_main_t * vm)
return 0; return 0;
} }
static clib_error_t * hdlc_input_init (vlib_main_t * vm) static clib_error_t *
hdlc_input_init (vlib_main_t * vm)
{ {
{ {
clib_error_t * error = vlib_call_init_function (vm, hdlc_init); clib_error_t *error = vlib_call_init_function (vm, hdlc_init);
if (error) if (error)
clib_error_report (error); clib_error_report (error);
} }
@ -324,30 +349,29 @@ VLIB_WORKER_INIT_FUNCTION (hdlc_input_runtime_init);
void void
hdlc_register_input_protocol (vlib_main_t * vm, hdlc_register_input_protocol (vlib_main_t * vm,
hdlc_protocol_t protocol, hdlc_protocol_t protocol, u32 node_index)
u32 node_index)
{ {
hdlc_main_t * em = &hdlc_main; hdlc_main_t *em = &hdlc_main;
hdlc_protocol_info_t * pi; hdlc_protocol_info_t *pi;
hdlc_input_runtime_t * rt; hdlc_input_runtime_t *rt;
u16 * n; u16 *n;
u32 i; u32 i;
{ {
clib_error_t * error = vlib_call_init_function (vm, hdlc_input_init); clib_error_t *error = vlib_call_init_function (vm, hdlc_input_init);
if (error) if (error)
clib_error_report (error); clib_error_report (error);
} }
pi = hdlc_get_protocol_info (em, protocol); pi = hdlc_get_protocol_info (em, protocol);
pi->node_index = node_index; pi->node_index = node_index;
pi->next_index = vlib_node_add_next (vm, pi->next_index = vlib_node_add_next (vm, hdlc_input_node.index, node_index);
hdlc_input_node.index,
node_index);
/* Setup hdlc protocol -> next index sparse vector mapping. */ /* Setup hdlc protocol -> next index sparse vector mapping. */
rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index); rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
n = sparse_vec_validate (rt->next_by_protocol, clib_host_to_net_u16 (protocol)); n =
sparse_vec_validate (rt->next_by_protocol,
clib_host_to_net_u16 (protocol));
n[0] = pi->next_index; n[0] = pi->next_index;
/* Rebuild next index -> sparse index inverse mapping when sparse vector /* Rebuild next index -> sparse index inverse mapping when sparse vector
@ -356,3 +380,11 @@ hdlc_register_input_protocol (vlib_main_t * vm,
for (i = 1; i < vec_len (rt->next_by_protocol); i++) for (i = 1; i < vec_len (rt->next_by_protocol); i++)
rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i; rt->sparse_index_by_next_index[rt->next_by_protocol[i]] = i;
} }
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -49,13 +49,15 @@
_ (0x86dd, ip6) \ _ (0x86dd, ip6) \
_ (0xfefe, osi) _ (0xfefe, osi)
typedef enum { typedef enum
{
#define _(n,f) HDLC_PROTOCOL_##f = n, #define _(n,f) HDLC_PROTOCOL_##f = n,
foreach_hdlc_protocol foreach_hdlc_protocol
#undef _ #undef _
} hdlc_protocol_t; } hdlc_protocol_t;
typedef struct { typedef struct
{
/* Set to 0x0f for unicast; 0x8f for broadcast. */ /* Set to 0x0f for unicast; 0x8f for broadcast. */
u8 address; u8 address;
@ -70,3 +72,11 @@ typedef struct {
} hdlc_header_t; } hdlc_header_t;
#endif /* included_vnet_hdlc_packet_h */ #endif /* included_vnet_hdlc_packet_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -41,7 +41,8 @@
#include <vnet/pg/pg.h> #include <vnet/pg/pg.h>
#include <vnet/hdlc/hdlc.h> #include <vnet/hdlc/hdlc.h>
typedef struct { typedef struct
{
pg_edit_t address; pg_edit_t address;
pg_edit_t control; pg_edit_t control;
pg_edit_t protocol; pg_edit_t protocol;
@ -58,8 +59,8 @@ pg_hdlc_header_init (pg_hdlc_header_t * e)
uword uword
unformat_pg_hdlc_header (unformat_input_t * input, va_list * args) unformat_pg_hdlc_header (unformat_input_t * input, va_list * args)
{ {
pg_stream_t * s = va_arg (*args, pg_stream_t *); pg_stream_t *s = va_arg (*args, pg_stream_t *);
pg_hdlc_header_t * h; pg_hdlc_header_t *h;
u32 group_index, error; u32 group_index, error;
h = pg_create_edit_group (s, sizeof (h[0]), sizeof (hdlc_header_t), h = pg_create_edit_group (s, sizeof (h[0]), sizeof (hdlc_header_t),
@ -70,15 +71,15 @@ unformat_pg_hdlc_header (unformat_input_t * input, va_list * args)
pg_edit_set_fixed (&h->control, 0x00); pg_edit_set_fixed (&h->control, 0x00);
error = 1; error = 1;
if (! unformat (input, "%U", if (!unformat (input, "%U",
unformat_pg_edit, unformat_pg_edit,
unformat_hdlc_protocol_net_byte_order, &h->protocol)) unformat_hdlc_protocol_net_byte_order, &h->protocol))
goto done; goto done;
{ {
hdlc_main_t * pm = &hdlc_main; hdlc_main_t *pm = &hdlc_main;
hdlc_protocol_info_t * pi = 0; hdlc_protocol_info_t *pi = 0;
pg_node_t * pg_node = 0; pg_node_t *pg_node = 0;
if (h->protocol.type == PG_EDIT_FIXED) if (h->protocol.type == PG_EDIT_FIXED)
{ {
@ -92,14 +93,22 @@ unformat_pg_hdlc_header (unformat_input_t * input, va_list * args)
&& unformat_user (input, pg_node->unformat_edit, s)) && unformat_user (input, pg_node->unformat_edit, s))
; ;
else if (! unformat_user (input, unformat_pg_payload, s)) else if (!unformat_user (input, unformat_pg_payload, s))
goto done; goto done;
} }
error = 0; error = 0;
done: done:
if (error) if (error)
pg_free_edit_group (s); pg_free_edit_group (s);
return error == 0; return error == 0;
} }
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/