VPP节点添加学习

 1 #include <vnet/plugin/plugin.h>
 2 #include <ck_sample/ck_sample.h>
 3 
 4 ck_sample_main_t ck_sample_main;
 5 
 6 
 7 int ck_sample_enable_disable(u32 sw_if_index, int enable)
 8 {
 9         if (pool_is_free_index (ck_sample_main.vnet_main->interface_main.sw_interfaces, 
10                                 sw_if_index))
11                 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
12 
13         vnet_feature_enable_disable("ip4-unicast",
14                 "ck_sample",
15                 sw_if_index, enable, 0, 0);
16         return 0;
17 }
18 
19 
20 static clib_error_t*
21 ck_sample_enable_disable_command_fn(vlib_main_t* vm,
22                                     unformat_input_t *input,
23                                     vlib_cli_command_t *cmd)
24 {
25         u32 sw_if_index = ~0;
26         int enable_disable = 1;
27 
28         while(unformat_check_input(input) != UNFORMAT_END_OF_INPUT) {
29                 if (unformat(input, "disable"))
30                         enable_disable = 0;
31                 else if (unformat(input, "%U",
32                         unformat_vnet_sw_interface,
33                         ck_sample_main.vnet_main, &sw_if_index));
34                 else
35                         break;
36         }
37 
38         if (sw_if_index == ~0)
39                 return clib_error_return(0, "Please specify an interface...");
40 
41         ck_sample_enable_disable(sw_if_index, enable_disable);
42 
43         return 0;
44 }
45 
46 VLIB_CLI_COMMAND (ck_sample_command, static) = {
47     .path = "ck sample",
48     .short_help = 
49     "ck sample <interface-name> [disable]",
50     .function = ck_sample_enable_disable_command_fn,
51 };
52 
53 
54 VLIB_PLUGIN_REGISTER () = {
55     .version = CK_SAMPLE_PLUGIN_BUILD_VER,
56     .description = "Sample of VPP Plugin",
57 };
58 
59 static clib_error_t *ck_sample_init(vlib_main_t* vm)
60 {
61     ck_sample_main.vnet_main = vnet_get_main();
62     return 0;
63 }
64 
65 VLIB_INIT_FUNCTION(ck_sample_init);
66 
67 VNET_FEATURE_INIT(ck_sample, static) = 
68 {
69     .arc_name = "ip4-unicast",
70     .node_name = "ck_sample",
71     .runs_before = VNET_FEATURES("ip4-lookup"),
72 };
 1 #ifndef __included_ck_sample_h__
 2 #define __included_ck_sample_h__
 3 
 4 #include <vnet/vnet.h>
 5 #include <vnet/ip/ip.h>
 6 
 7 #include <vppinfra/hash.h>
 8 #include <vppinfra/error.h>
 9 #include <vppinfra/elog.h>
10 
11 typedef struct {
12     /* API message ID base */
13     u16 msg_id_base;
14 
15     /* convenience */
16     vnet_main_t * vnet_main;
17 } ck_sample_main_t;
18 
19 extern ck_sample_main_t ck_sample_main;
20 
21 extern vlib_node_registration_t ck_sample_node;
22 
23 #define CK_SAMPLE_PLUGIN_BUILD_VER "1.0"
24 
25 #endif /* __included_ck_sample_h__ */
  1 #include <vlib/vlib.h>
  2 #include <vnet/vnet.h>
  3 #include <vnet/pg/pg.h>
  4 #include <vnet/ethernet/ethernet.h>
  5 #include <vppinfra/error.h>
  6 #include <ck_sample/ck_sample.h>
  7 
  8 typedef enum
  9 {
 10   CK_SAMPLE_NEXT_IP4,
 11   CK_SAMPLE_DROP,
 12   CK_SAMPLE_NEXT_N,
 13 } ck_sample_next_t;
 14 
 15 typedef struct
 16 {
 17   u32 next_index;
 18   u32 sw_if_index;
 19   u8 new_src_mac[6];
 20   u8 new_dst_mac[6];
 21 } ck_sample_trace_t;
 22 
 23 #define foreach_ck_sample_error 
 24 _(SHOWED, "show packets processed")
 25 
 26 typedef enum
 27 {
 28 #define _(sym,str) SAMPLE_ERROR_##sym,
 29   foreach_ck_sample_error
 30 #undef _
 31     SAMPLE_N_ERROR,
 32 } ck_ssample_error_t;
 33 
 34 
 35 static char *ck_sample_error_strings[] = {
 36 #define _(sym, str) str,
 37         foreach_ck_sample_error
 38 #undef _
 39 };
 40 
 41 extern vlib_node_registration_t ck_sample_node;
 42 
 43 static u8 *
 44 format_ck_sample_trace (u8 * s, va_list * args)
 45 {
 46         s = format(s, "To Do!
");
 47         return s;
 48 }
 49 
 50 static uword ck_sample_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node,
 51         vlib_frame_t * frame)
 52 {
 53         u32 n_left_from, *from, *to_next;
 54         ck_sample_next_t     next_index;
 55 
 56         from        = vlib_frame_vector_args(frame);
 57         n_left_from = frame->n_vectors;
 58         next_index  = node->cached_next_index;
 59 
 60         while(n_left_from > 0){
 61                 u32 n_left_to_next;
 62                 vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
 63 
 64                 while(n_left_from > 0 && n_left_to_next > 0){
 65                         vlib_buffer_t  *b0;
 66                         u32             bi0, next0 = 0;
 67 
 68                         bi0 = to_next[0] = from[0];
 69                         from           += 1;
 70                         to_next        += 1;
 71                         n_left_to_next -= 1;
 72                         n_left_from    -= 1;
 73 
 74                         b0 = vlib_get_buffer(vm, bi0);
 75                         
 76             void *en0 = vlib_buffer_get_current(b0);
 77                         int i = 0;
 78                         for (i = 0; i < 20; i++)
 79                         {
 80                                 printf("%02x ", *(u8*)(en0+i));
 81                         }
 82                         printf("
");
 83                         vlib_validate_buffer_enqueue_x1(vm, node, next_index,
 84                                 to_next, n_left_to_next, bi0, next0);
 85                 }
 86 
 87                 vlib_put_next_frame(vm, node, next_index, n_left_to_next);
 88         }
 89 
 90         return frame->n_vectors;
 91 }
 92 
 93 
 94 VLIB_REGISTER_NODE (ck_sample_node) = {
 95         .name        = "ck_sample",
 96         .function       = ck_sample_node_fn,
 97         .vector_size    = sizeof(u32),
 98         .format_trace   = format_ck_sample_trace,
 99         .type           = VLIB_NODE_TYPE_INTERNAL,
100         .n_errors       = ARRAY_LEN(ck_sample_error_strings),
101         .error_strings  = ck_sample_error_strings,
102         .n_next_nodes   = CK_SAMPLE_NEXT_N,
103         .next_nodes     = {
104                 [CK_SAMPLE_NEXT_IP4]    = "ip4-lookup",
105                 [CK_SAMPLE_DROP]        = "error-drop",
106         },
107 };
原文地址:https://www.cnblogs.com/mysky007/p/12350458.html