FD.io VPP  v21.06-3-gbb25fbf28
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 
28 unsigned char srv6_localsid_name[32] = "Sample-SRv6-LocalSID-plugin";
29 unsigned char keyword_str[32] = "new_srv6_localsid";
30 unsigned char def_str[64] = "This is a definition of a sample new_srv6_localsid";
31 unsigned char params_str[32] = "<fib_table>";
32 
33 /*****************************************/
34 /* SRv6 LocalSID instantiation and removal functions */
35 static int
37 {
38  /*
39  * Do you want to do anything fancy upon localsid instantiation?
40  * You can do it here
41  * (If return != 0 the localsid creation will be cancelled.)
42  */
43  /* As an example Im going to do a +1 to the fib table inserted by the user */
45  ls_mem->fib_table += 1;
46  return 0;
47 }
48 
49 static int
51 {
52  /* Do you want to do anything fancy upon localsid removal?
53  * You can do it here
54  * (If return != 0 the localsid removal will be cancelled.)
55  */
56  /*
57  * BTW if you stored something in localsid->plugin_mem you should clean it now
58  */
59 
60  //In this example we are only cleaning the memory allocated per localsid
61  clib_mem_free(localsid->plugin_mem);
62  return 0;
63 }
64 
65 /**********************************/
66 /* SRv6 LocalSID format functions */
67 /*
68  * Prints nicely the parameters of a localsid
69  * Example: print "Table 5"
70  */
71 u8 *
72 format_srv6_localsid_sample (u8 * s, va_list * args)
73 {
74  srv6_localsid_sample_per_sid_memory_t *ls_mem = va_arg (*args, void *);
75  return (format (s, "Table: %u", ls_mem->fib_table));
76 }
77 
78 /*
79  * Process the parameters of a localsid
80  * Example: process from:
81  * sr localsid address cafe::1 behavior new_srv6_localsid 5
82  * everything from behavior on... so in this case 'new_srv6_localsid 5'
83  * Notice that it MUST match the keyword_str and params_str defined above.
84  */
85 uword
87 {
88  void **plugin_mem = va_arg (*args, void **);
90  u32 table_id;
91  if (unformat (input, "new_srv6_localsid %u", &table_id))
92  {
93  /* Allocate a portion of memory */
95  sizeof(srv6_localsid_sample_per_sid_memory_t), 0, 0, 1);
96 
97  /* Set to zero the memory */
99 
100  /* Our brand-new car is ready */
101  ls_mem->fib_table = table_id;
102 
103  /* Dont forget to add it to the localsid */
104  *plugin_mem = ls_mem;
105  return 1;
106  }
107  return 0;
108 }
109 
110 /*************************/
111 /* SRv6 LocalSID FIB DPO */
112 static u8 *
113 format_srv6_localsid_sample_dpo (u8 * s, va_list * args)
114 {
115  index_t index = va_arg (*args, index_t);
116  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
117 
118  return (format (s, "SR: localsid_sample_index:[%u]", index));
119 }
120 
121 void
123 {
124 }
125 
126 void
128 {
129 }
130 
133  .dv_unlock = srv6_localsid_sample_dpo_unlock,
134  .dv_format = format_srv6_localsid_sample_dpo,
135 };
136 
137 const static char *const srv6_localsid_sample_ip6_nodes[] = {
138  "srv6-localsid-sample",
139  NULL,
140 };
141 
142 const static char *const *const srv6_localsid_sample_nodes[DPO_PROTO_NUM] = {
144 };
145 
146 /**********************/
148 {
150  int rv = 0;
151  /* Create DPO */
154 
155  /* Register SRv6 LocalSID */
158  keyword_str,
159  def_str,
160  params_str,
166  if (rv < 0)
167  clib_error_return (0, "SRv6 LocalSID function could not be registered.");
168  else
170 
171  return 0;
172 }
173 
175 
177  .version = "1.0",
178 };
dpo_register_new_type
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:349
api.h
ip6_sr_localsid_t
SR LocalSID.
Definition: sr.h:122
sr_localsid_register_function
int sr_localsid_register_function(vlib_main_t *vm, u8 *fn_name, u8 *keyword_str, u8 *def_str, u8 *params_str, u8 prefix_length, 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:2347
srv6_localsid_sample_dpo_lock
void srv6_localsid_sample_dpo_lock(dpo_id_t *dpo)
Definition: srv6_localsid_sample.c:122
clib_mem_free
static void clib_mem_free(void *p)
Definition: mem.h:311
ip6_sr_localsid_t::plugin_mem
void * plugin_mem
Memory to be used by the plugin callback functions.
Definition: sr.h:155
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
api.h
srv6_localsid_sample_vft
const static dpo_vft_t srv6_localsid_sample_vft
Definition: srv6_localsid_sample.c:131
def_str
unsigned char def_str[64]
Definition: srv6_localsid_sample.c:30
srv6_localsid_sample_dpo_unlock
void srv6_localsid_sample_dpo_unlock(dpo_id_t *dpo)
Definition: srv6_localsid_sample.c:127
unformat_input_t
struct _unformat_input_t unformat_input_t
format_srv6_localsid_sample
u8 * format_srv6_localsid_sample(u8 *s, va_list *args)
Definition: srv6_localsid_sample.c:72
srv6_localsid_sample_main_t::srv6_localsid_sample_dpo_type
dpo_type_t srv6_localsid_sample_dpo_type
Definition: srv6_localsid_sample.h:36
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
VLIB_PLUGIN_REGISTER
VLIB_PLUGIN_REGISTER()
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
srv6_localsid_sample_main_t
Definition: srv6_localsid_sample.h:27
srv6_localsid_creation_fn
static int srv6_localsid_creation_fn(ip6_sr_localsid_t *localsid)
Definition: srv6_localsid_sample.c:36
index_t
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:43
uword
u64 uword
Definition: types.h:112
srv6_localsid_sample_per_sid_memory_t
Definition: srv6_localsid_sample.h:47
dpo_vft_t_::dv_lock
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:428
format_srv6_localsid_sample_dpo
static u8 * format_srv6_localsid_sample_dpo(u8 *s, va_list *args)
Definition: srv6_localsid_sample.c:113
params_str
unsigned char params_str[32]
Definition: srv6_localsid_sample.c:31
plugin.h
keyword_str
unsigned char keyword_str[32]
Definition: srv6_localsid_sample.c:29
index
u32 index
Definition: flow_types.api:221
format
description fragment has unexpected format
Definition: map.api:433
DPO_PROTO_IP6
@ DPO_PROTO_IP6
Definition: dpo.h:65
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
table_id
u32 table_id
Definition: wireguard.api:102
srv6_localsid_sample_init
static clib_error_t * srv6_localsid_sample_init(vlib_main_t *vm)
Definition: srv6_localsid_sample.c:147
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
dpo_vft_t_
A virtual function table regisitered for a DPO type.
Definition: dpo.h:423
unformat_srv6_localsid_sample
uword unformat_srv6_localsid_sample(unformat_input_t *input, va_list *args)
Definition: srv6_localsid_sample.c:86
srv6_localsid_sample_ip6_nodes
const static char *const srv6_localsid_sample_ip6_nodes[]
Definition: srv6_localsid_sample.c:137
srv6_localsid_name
unsigned char srv6_localsid_name[32]
Definition: srv6_localsid_sample.c:28
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
clib_mem_alloc_aligned_at_offset
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:222
dpo_id_t_
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:172
srv6_localsid_sample_nodes
const static char *const *const srv6_localsid_sample_nodes[DPO_PROTO_NUM]
Definition: srv6_localsid_sample.c:142
rv
int __clib_unused rv
Definition: application.c:491
srv6_localsid_sample_main_t::srv6_localsid_behavior_id
u32 srv6_localsid_behavior_id
Definition: srv6_localsid_sample.h:39
vnet.h
srv6_localsid_removal_fn
static int srv6_localsid_removal_fn(ip6_sr_localsid_t *localsid)
Definition: srv6_localsid_sample.c:50
DPO_PROTO_NUM
#define DPO_PROTO_NUM
Definition: dpo.h:72
srv6_localsid_sample_per_sid_memory_t::fib_table
u32 fib_table
Definition: srv6_localsid_sample.h:48
srv6_localsid_sample_main
srv6_localsid_sample_main_t srv6_localsid_sample_main