vpp收包发包插件示例
时间: 2023-07-08 07:41:16 浏览: 288
以下是使用VPP进行收包发包的插件示例:
1. 接收插件示例:
```
#include <vlib/vlib.h>
static uword
my_input_node (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame)
{
u32 n_left_from, * from, * to_next;
my_input_next_t next_index;
my_input_trace_t * t;
vnet_main_t * vnm = vnet_get_main();
from = vlib_frame_vector_args (frame);
n_left_from = frame->n_vectors;
next_index = node->cached_next_index;
while (n_left_from > 0)
{
u32 n_left_to_next;
vlib_get_next_frame (vm, node, next_index,
to_next, n_left_to_next);
while (n_left_from > 0 && n_left_to_next > 0)
{
u32 bi0;
vlib_buffer_t * b0;
u32 next0;
bi0 = from[0];
from += 1;
n_left_from -= 1;
to_next[0] = bi0;
to_next += 1;
n_left_to_next -= 1;
b0 = vlib_get_buffer (vm, bi0);
t = vlib_add_trace (vm, node, b0, sizeof (*t));
t->sw_if_index = vnet_buffer(b0)->sw_if_index[VLIB_RX];
next0 = MY_INPUT_NEXT_NORMAL;
vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
to_next, n_left_to_next,
bi0, next0);
}
vlib_put_next_frame (vm, node, next_index, n_left_to_next);
}
return frame->n_vectors;
}
```
2. 发送插件示例:
```
#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
static u32
my_output_node_fn (vlib_main_t * vm,
vlib_node_runtime_t * node,
vlib_frame_t * frame)
{
u32 n_left_from, * from;
u32 n_left_to_next, * to_next;
my_output_next_t next_index;
u32 pkts_sent = 0;
vlib_frame_t *f;
u32 thread_index = vm->thread_index;
from = vlib_frame_vector_args (frame);
n_left_from = frame->n_vectors;
next_index = node->cached_next_index;
while (n_left_from > 0)
{
u32 n_left_to_next;
vlib_get_next_frame (vm, node, next_index,
to_next, n_left_to_next);
while (n_left_from > 0 && n_left_to_next > 0)
{
u32 bi0;
vlib_buffer_t * b0;
u32 next0 = 0;
u32 error0 = 0;
u32 sw_if_index0;
bi0 = from[0];
from += 1;
n_left_from -= 1;
to_next[0] = bi0;
to_next += 1;
n_left_to_next -= 1;
b0 = vlib_get_buffer (vm, bi0);
sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
vlib_buffer_advance (b0, -sizeof (ethernet_header_t));
ethernet_header_t *eth = vlib_buffer_get_current (b0);
eth->type = clib_host_to_net_u16 (ETHERNET_TYPE_IP4);
if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
{
my_output_trace_t *tr = vlib_add_trace (vm, node,
b0, sizeof (*tr));
tr->sw_if_index = sw_if_index0;
tr->next_index = next0;
}
vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
to_next, n_left_to_next,
bi0, next0);
pkts_sent++;
}
vlib_put_next_frame (vm, node, next_index, n_left_to_next);
}
vlib_node_increment_counter (vm, my_output_node.index,
MY_OUTPUT_ERROR_TX,
pkts_sent);
return frame->n_vectors;
}
```
这里仅是简单的示例,实际使用中需要根据具体需求进行修改和完善。
阅读全文