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,7 +43,8 @@
/* Global main structure. */
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_main_t *pm = &hdlc_main;
@ -57,7 +58,8 @@ u8 * format_hdlc_protocol (u8 * s, va_list * args)
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_header_t *h = va_arg (*args, hdlc_header_t *);
@ -92,7 +94,8 @@ u8 * format_hdlc_header_with_length (u8 * s, va_list * args)
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 *);
return format (s, "%U", format_hdlc_header_with_length, h, 0);
@ -108,8 +111,7 @@ unformat_hdlc_protocol_host_byte_order (unformat_input_t * input,
int p, i;
/* Numeric type. */
if (unformat (input, "0x%x", &p)
|| unformat (input, "%d", &p))
if (unformat (input, "0x%x", &p) || unformat (input, "%d", &p))
{
if (p >= (1 << 16))
return 0;
@ -147,8 +149,7 @@ unformat_hdlc_header (unformat_input_t * input, va_list * args)
hdlc_header_t _h, *h = &_h;
u16 p;
if (! unformat (input, "%U",
unformat_hdlc_protocol_host_byte_order, &p))
if (!unformat (input, "%U", unformat_hdlc_protocol_host_byte_order, &p))
return 0;
h->address = 0xff;
@ -170,14 +171,14 @@ unformat_hdlc_header (unformat_input_t * input, va_list * args)
static u8 *
hdlc_build_rewrite (vnet_main_t * vnm,
u32 sw_if_index,
vnet_link_t link_type,
const void *dst_address)
vnet_link_t link_type, const void *dst_address)
{
hdlc_header_t *h;
u8 *rewrite = NULL;
hdlc_protocol_t protocol;
switch (link_type) {
switch (link_type)
{
#define _(a,b) case VNET_LINK_##a: protocol = HDLC_PROTOCOL_##b; break
_(IP4, ip4);
_(IP6, ip6);
@ -196,6 +197,7 @@ hdlc_build_rewrite (vnet_main_t * vnm,
return (rewrite);
}
/* *INDENT-OFF* */
VNET_HW_INTERFACE_CLASS (hdlc_hw_interface_class) = {
.name = "HDLC",
.format_header = format_hdlc_header_with_length,
@ -203,10 +205,10 @@ VNET_HW_INTERFACE_CLASS (hdlc_hw_interface_class) = {
.build_rewrite = hdlc_build_rewrite,
.flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
};
/* *INDENT-ON* */
static void add_protocol (hdlc_main_t * pm,
hdlc_protocol_t protocol,
char * protocol_name)
static void
add_protocol (hdlc_main_t * pm, hdlc_protocol_t protocol, char *protocol_name)
{
hdlc_protocol_info_t *pi;
u32 i;
@ -222,7 +224,8 @@ static void add_protocol (hdlc_main_t * pm,
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;
@ -235,15 +238,23 @@ static clib_error_t * hdlc_init (vlib_main_t * vm)
#define _(n,s) add_protocol (pm, HDLC_PROTOCOL_##s, #s);
foreach_hdlc_protocol
#undef _
return vlib_call_init_function (vm, hdlc_input_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);
return &hdlc_main;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -46,14 +46,16 @@
extern vnet_hw_interface_class_t hdlc_hw_interface_class;
typedef enum {
typedef enum
{
#define hdlc_error(n,s) HDLC_ERROR_##n,
#include <vnet/hdlc/error.def>
#undef hdlc_error
HDLC_N_ERROR,
} hdlc_error_t;
typedef struct {
typedef struct
{
/* Name (a c string). */
char *name;
@ -67,7 +69,8 @@ typedef struct {
u32 next_index;
} hdlc_protocol_info_t;
typedef struct {
typedef struct
{
vlib_main_t *vlib_main;
hdlc_protocol_info_t *protocol_infos;
@ -88,8 +91,7 @@ extern hdlc_main_t hdlc_main;
/* Register given node index to take input for given hdlc type. */
void
hdlc_register_input_type (vlib_main_t * vm,
hdlc_protocol_t protocol,
u32 node_index);
hdlc_protocol_t protocol, u32 node_index);
format_function_t format_hdlc_protocol;
format_function_t format_hdlc_header;
@ -117,7 +119,14 @@ hdlc_setup_node (vlib_main_t * vm, u32 node_index)
void
hdlc_register_input_protocol (vlib_main_t * vm,
hdlc_protocol_t protocol,
u32 node_index);
hdlc_protocol_t protocol, u32 node_index);
#endif /* included_hdlc_h */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/

View File

@ -46,18 +46,21 @@
_ (PUNT, "error-punt") \
_ (DROP, "error-drop")
typedef enum {
typedef enum
{
#define _(s,n) HDLC_INPUT_NEXT_##s,
foreach_hdlc_input_next
#undef _
HDLC_INPUT_N_NEXT,
} hdlc_input_next_t;
typedef struct {
typedef struct
{
u8 packet_data[32];
} 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_node_t * node) = va_arg (*va, vlib_node_t *);
@ -68,7 +71,8 @@ static u8 * format_hdlc_input_trace (u8 * s, va_list * va)
return s;
}
typedef struct {
typedef struct
{
/* Sparse vector mapping hdlc protocol in network byte order
to next index. */
u16 *next_by_protocol;
@ -78,8 +82,7 @@ typedef struct {
static uword
hdlc_input (vlib_main_t * vm,
vlib_node_runtime_t * node,
vlib_frame_t * from_frame)
vlib_node_runtime_t * node, vlib_frame_t * from_frame)
{
hdlc_input_runtime_t *rt = (void *) node->runtime_data;
u32 n_left_from, next_index, i_next, *from, *to_next;
@ -101,8 +104,7 @@ hdlc_input (vlib_main_t * vm,
{
u32 n_left_to_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);
while (n_left_from >= 4 && n_left_to_next >= 2)
{
@ -157,10 +159,17 @@ hdlc_input (vlib_main_t * vm,
b1->current_length -= len1;
/* 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];
b1->error = node->errors[i1 == 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];
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);
@ -173,29 +182,38 @@ hdlc_input (vlib_main_t * vm,
to_next[-2] = bi1;
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;
case 2:
/* A A B */
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;
case 3:
/* A B B or A B C */
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, vec_elt (rt->next_by_protocol, i1), bi1);
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,
i1), bi1);
if (i0 == i1)
{
vlib_put_next_frame (vm, node, next_index,
n_left_to_next);
i_next = i1;
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);
}
}
}
@ -230,7 +248,10 @@ hdlc_input (vlib_main_t * vm,
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? */
if (PREDICT_FALSE (i0 != i_next))
@ -262,6 +283,7 @@ static char * hdlc_error_strings[] = {
#undef hdlc_error
};
/* *INDENT-OFF* */
VLIB_REGISTER_NODE (hdlc_input_node) = {
.function = hdlc_input,
.name = "hdlc-input",
@ -284,8 +306,10 @@ VLIB_REGISTER_NODE (hdlc_input_node) = {
.format_trace = format_hdlc_input_trace,
.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;
rt = vlib_node_get_runtime_data (vm, hdlc_input_node.index);
@ -304,7 +328,8 @@ static clib_error_t * hdlc_input_runtime_init (vlib_main_t * vm)
return 0;
}
static clib_error_t * hdlc_input_init (vlib_main_t * vm)
static clib_error_t *
hdlc_input_init (vlib_main_t * vm)
{
{
@ -324,8 +349,7 @@ VLIB_WORKER_INIT_FUNCTION (hdlc_input_runtime_init);
void
hdlc_register_input_protocol (vlib_main_t * vm,
hdlc_protocol_t protocol,
u32 node_index)
hdlc_protocol_t protocol, u32 node_index)
{
hdlc_main_t *em = &hdlc_main;
hdlc_protocol_info_t *pi;
@ -341,13 +365,13 @@ hdlc_register_input_protocol (vlib_main_t * vm,
pi = hdlc_get_protocol_info (em, protocol);
pi->node_index = node_index;
pi->next_index = vlib_node_add_next (vm,
hdlc_input_node.index,
node_index);
pi->next_index = vlib_node_add_next (vm, hdlc_input_node.index, node_index);
/* Setup hdlc protocol -> next index sparse vector mapping. */
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;
/* 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++)
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) \
_ (0xfefe, osi)
typedef enum {
typedef enum
{
#define _(n,f) HDLC_PROTOCOL_##f = n,
foreach_hdlc_protocol
#undef _
} hdlc_protocol_t;
typedef struct {
typedef struct
{
/* Set to 0x0f for unicast; 0x8f for broadcast. */
u8 address;
@ -70,3 +72,11 @@ typedef struct {
} hdlc_header_t;
#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/hdlc/hdlc.h>
typedef struct {
typedef struct
{
pg_edit_t address;
pg_edit_t control;
pg_edit_t protocol;
@ -103,3 +104,11 @@ unformat_pg_hdlc_header (unformat_input_t * input, va_list * args)
return error == 0;
}
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/