blob: 5b33ed6d091efdbb0f403b50001145e95b12b663 [file] [log] [blame]
Pablo Camarillofb380952016-12-07 18:34:18 +01001/*
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>
Pablo Camarillofb380952016-12-07 18:34:18 +010027
28unsigned char srv6_localsid_name[32] = "Sample-SRv6-LocalSID-plugin";
29unsigned char keyword_str[32] = "new_srv6_localsid";
30unsigned char def_str[64] = "This is a definition of a sample new_srv6_localsid";
31unsigned char params_str[32] = "<fib_table>";
32
33/*****************************************/
34/* SRv6 LocalSID instantiation and removal functions */
35static int
36srv6_localsid_creation_fn (ip6_sr_localsid_t *localsid)
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 */
44 srv6_localsid_sample_per_sid_memory_t *ls_mem = localsid->plugin_mem;
45 ls_mem->fib_table += 1;
46 return 0;
47}
48
49static int
50srv6_localsid_removal_fn (ip6_sr_localsid_t *localsid)
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 */
71u8 *
72format_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 */
85uword
86unformat_srv6_localsid_sample (unformat_input_t * input, va_list * args)
87{
88 void **plugin_mem = va_arg (*args, void **);
89 srv6_localsid_sample_per_sid_memory_t *ls_mem;
90 u32 table_id;
91 if (unformat (input, "new_srv6_localsid %u", &table_id))
92 {
93 /* Allocate a portion of memory */
Damjan Marion8a96c6d2022-03-21 15:06:57 +010094 ls_mem = clib_mem_alloc (sizeof (srv6_localsid_sample_per_sid_memory_t));
Pablo Camarillofb380952016-12-07 18:34:18 +010095
96 /* Set to zero the memory */
Dave Barachb7b92992018-10-17 10:38:51 -040097 clib_memset (ls_mem, 0, sizeof(srv6_localsid_sample_per_sid_memory_t));
Pablo Camarillofb380952016-12-07 18:34:18 +010098
99 /* Our brand-new car is ready */
100 ls_mem->fib_table = table_id;
101
102 /* Dont forget to add it to the localsid */
103 *plugin_mem = ls_mem;
104 return 1;
105 }
106 return 0;
107}
108
109/*************************/
110/* SRv6 LocalSID FIB DPO */
111static u8 *
112format_srv6_localsid_sample_dpo (u8 * s, va_list * args)
113{
114 index_t index = va_arg (*args, index_t);
115 CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
116
117 return (format (s, "SR: localsid_sample_index:[%u]", index));
118}
119
120void
121srv6_localsid_sample_dpo_lock (dpo_id_t * dpo)
122{
123}
124
125void
126srv6_localsid_sample_dpo_unlock (dpo_id_t * dpo)
127{
128}
129
130const static dpo_vft_t srv6_localsid_sample_vft = {
131 .dv_lock = srv6_localsid_sample_dpo_lock,
132 .dv_unlock = srv6_localsid_sample_dpo_unlock,
133 .dv_format = format_srv6_localsid_sample_dpo,
134};
135
136const static char *const srv6_localsid_sample_ip6_nodes[] = {
137 "srv6-localsid-sample",
138 NULL,
139};
140
141const static char *const *const srv6_localsid_sample_nodes[DPO_PROTO_NUM] = {
142 [DPO_PROTO_IP6] = srv6_localsid_sample_ip6_nodes,
143};
144
145/**********************/
146static clib_error_t * srv6_localsid_sample_init (vlib_main_t * vm)
147{
148 srv6_localsid_sample_main_t * sm = &srv6_localsid_sample_main;
149 int rv = 0;
150 /* Create DPO */
151 sm->srv6_localsid_sample_dpo_type = dpo_register_new_type (
152 &srv6_localsid_sample_vft, srv6_localsid_sample_nodes);
153
154 /* Register SRv6 LocalSID */
155 rv = sr_localsid_register_function (vm,
156 srv6_localsid_name,
157 keyword_str,
158 def_str,
159 params_str,
160 &sm->srv6_localsid_sample_dpo_type,
161 format_srv6_localsid_sample,
162 unformat_srv6_localsid_sample,
163 srv6_localsid_creation_fn,
164 srv6_localsid_removal_fn);
165 if (rv < 0)
166 clib_error_return (0, "SRv6 LocalSID function could not be registered.");
167 else
168 sm->srv6_localsid_behavior_id = rv;
169
170 return 0;
171}
172
173VLIB_INIT_FUNCTION (srv6_localsid_sample_init);
174
175VLIB_PLUGIN_REGISTER () = {
176 .version = "1.0",
177};