FD.io VPP  v17.04.2-2-ga8f93f8
Vector Packet Processing
srv6_localsid_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  *------------------------------------------------------------------
17  * srv6_localsid_sample.c - Simple SRv6 LocalSID
18  *------------------------------------------------------------------
19  */
20 
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <srv6-localsid/srv6_localsid_sample.h>
24 
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28 
29 unsigned char srv6_localsid_name[32] = "Sample-SRv6-LocalSID-plugin";
30 unsigned char keyword_str[32] = "new_srv6_localsid";
31 unsigned char def_str[64] = "This is a definition of a sample new_srv6_localsid";
32 unsigned char params_str[32] = "<fib_table>";
33 
34 /*****************************************/
35 /* SRv6 LocalSID instantiation and removal functions */
36 static int
38 {
39  /*
40  * Do you want to do anything fancy upon localsid instantiation?
41  * You can do it here
42  * (If return != 0 the localsid creation will be cancelled.)
43  */
44  /* As an example Im going to do a +1 to the fib table inserted by the user */
46  ls_mem->fib_table += 1;
47  return 0;
48 }
49 
50 static int
52 {
53  /* Do you want to do anything fancy upon localsid removal?
54  * You can do it here
55  * (If return != 0 the localsid removal will be cancelled.)
56  */
57  /*
58  * BTW if you stored something in localsid->plugin_mem you should clean it now
59  */
60 
61  //In this example we are only cleaning the memory allocated per localsid
62  clib_mem_free(localsid->plugin_mem);
63  return 0;
64 }
65 
66 /**********************************/
67 /* SRv6 LocalSID format functions */
68 /*
69  * Prints nicely the parameters of a localsid
70  * Example: print "Table 5"
71  */
72 u8 *
73 format_srv6_localsid_sample (u8 * s, va_list * args)
74 {
75  srv6_localsid_sample_per_sid_memory_t *ls_mem = va_arg (*args, void *);
76  return (format (s, "Table: %u", ls_mem->fib_table));
77 }
78 
79 /*
80  * Process the parameters of a localsid
81  * Example: process from:
82  * sr localsid address cafe::1 behavior new_srv6_localsid 5
83  * everything from behavior on... so in this case 'new_srv6_localsid 5'
84  * Notice that it MUST match the keyword_str and params_str defined above.
85  */
86 uword
88 {
89  void **plugin_mem = va_arg (*args, void **);
91  u32 table_id;
92  if (unformat (input, "new_srv6_localsid %u", &table_id))
93  {
94  /* Allocate a portion of memory */
96  sizeof(srv6_localsid_sample_per_sid_memory_t), 0, 0, 1);
97 
98  /* Set to zero the memory */
99  memset (ls_mem, 0, sizeof(srv6_localsid_sample_per_sid_memory_t));
100 
101  /* Our brand-new car is ready */
102  ls_mem->fib_table = table_id;
103 
104  /* Dont forget to add it to the localsid */
105  *plugin_mem = ls_mem;
106  return 1;
107  }
108  return 0;
109 }
110 
111 /*************************/
112 /* SRv6 LocalSID FIB DPO */
113 static u8 *
114 format_srv6_localsid_sample_dpo (u8 * s, va_list * args)
115 {
116  index_t index = va_arg (*args, index_t);
117  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
118 
119  return (format (s, "SR: localsid_sample_index:[%u]", index));
120 }
121 
122 void
124 {
125 }
126 
127 void
129 {
130 }
131 
132 const static dpo_vft_t srv6_localsid_sample_vft = {
134  .dv_unlock = srv6_localsid_sample_dpo_unlock,
135  .dv_format = format_srv6_localsid_sample_dpo,
136 };
137 
138 const static char *const srv6_localsid_sample_ip6_nodes[] = {
139  "srv6-localsid-sample",
140  NULL,
141 };
142 
143 const static char *const *const srv6_localsid_sample_nodes[DPO_PROTO_NUM] = {
145 };
146 
147 /**********************/
149 {
151  int rv = 0;
152  /* Create DPO */
154  &srv6_localsid_sample_vft, srv6_localsid_sample_nodes);
155 
156  /* Register SRv6 LocalSID */
159  keyword_str,
160  def_str,
161  params_str,
167  if (rv < 0)
168  clib_error_return (0, "SRv6 LocalSID function could not be registered.");
169  else
170  sm->srv6_localsid_behavior_id = rv;
171 
172  return 0;
173 }
174 
176 
178  .version = "1.0",
179 };
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:335
uword unformat_srv6_localsid_sample(unformat_input_t *input, va_list *args)
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:330
unsigned char srv6_localsid_name[32]
static void * clib_mem_alloc_aligned_at_offset(uword size, uword align, uword align_offset, int os_out_of_memory_on_failure)
Definition: mem.h:72
void srv6_localsid_sample_dpo_unlock(dpo_id_t *dpo)
SR LocalSID.
Definition: sr.h:101
static int srv6_localsid_removal_fn(ip6_sr_localsid_t *localsid)
unsigned char params_str[32]
#define NULL
Definition: clib.h:55
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
static int srv6_localsid_creation_fn(ip6_sr_localsid_t *localsid)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
unsigned char keyword_str[32]
static const char *const srv6_localsid_sample_ip6_nodes[]
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
#define clib_error_return(e, args...)
Definition: error.h:111
dpo_type_t dpo_register_new_type(const dpo_vft_t *vft, const char *const *const *nodes)
Create and register a new DPO type.
Definition: dpo.c:261
static u8 * format_srv6_localsid_sample_dpo(u8 *s, va_list *args)
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:146
static clib_error_t * srv6_localsid_sample_init(vlib_main_t *vm)
struct _unformat_input_t unformat_input_t
unsigned char def_str[64]
static const char *const *const srv6_localsid_sample_nodes[DPO_PROTO_NUM]
void srv6_localsid_sample_dpo_lock(dpo_id_t *dpo)
vlib_main_t * vm
Definition: buffer.c:276
unsigned int u32
Definition: types.h:88
void * plugin_mem
Memory to be used by the plugin callback functions.
Definition: sr.h:123
static void clib_mem_free(void *p)
Definition: mem.h:176
srv6_localsid_sample_main_t srv6_localsid_sample_main
u64 uword
Definition: types.h:112
#define DPO_PROTO_NUM
Definition: dpo.h:73
unsigned char u8
Definition: types.h:56
VLIB_PLUGIN_REGISTER()
int sr_localsid_register_function(vlib_main_t *vm, u8 *fn_name, u8 *keyword_str, u8 *def_str, u8 *params_str, dpo_type_t *dpo, format_function_t *ls_format, unformat_function_t *ls_unformat, sr_plugin_callback_t *creation_fn, sr_plugin_callback_t *removal_fn)
SR LocalSID plugin registry.
Definition: sr_localsid.c:1366
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:971
u8 * format_srv6_localsid_sample(u8 *s, va_list *args)