sr: SRv6 uN behavior
[vpp.git] / src / plugins / srv6-un / un.c
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  * un.c - SRv6 Masquerading Proxy (AM) function
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/adj/adj.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <srv6-un/un.h>
26
27 unsigned char function_name[] = "SRv6-uN";
28 unsigned char keyword_str[] = "uN(32b+16b)";
29 unsigned char def_str[] = "SRv6 uSID uN";
30 unsigned char params_str[] = "";
31 u8 prefix_length = 48;
32
33 srv6_un_main_t srv6_un_main;
34
35 /*****************************************/
36 /* SRv6 LocalSID instantiation and removal functions */
37 static int
38 srv6_un_localsid_creation_fn (ip6_sr_localsid_t * localsid)
39 {
40   srv6_un_localsid_t *ls_mem = localsid->plugin_mem;
41   //Nothing to do here.
42   ls_mem->shift = 16;
43   return 0;
44 }
45
46 static int
47 srv6_un_localsid_removal_fn (ip6_sr_localsid_t * localsid)
48 {
49   //Nothing to do here.
50
51   /* Clean up local SID memory */
52   clib_mem_free (localsid->plugin_mem);
53
54   return 0;
55 }
56
57 /**********************************/
58 /* SRv6 LocalSID format functions */
59 /*
60  * Prints nicely the parameters of a localsid
61  * Example: print "Table 5"
62  */
63 u8 *
64 format_srv6_un_localsid (u8 * s, va_list * args)
65 {
66   srv6_un_localsid_t *ls_mem = va_arg (*args, void *);
67
68   return (format (s, "Shift:\t\t%u", ls_mem->shift));
69 }
70
71 /*
72  * Process the parameters of a localsid
73  * Example: process from:
74  * sr localsid address cafe::1 behavior new_srv6_localsid 5
75  * everything from behavior on... so in this case 'new_srv6_localsid 5'
76  * Notice that it MUST match the keyword_str and params_str defined above.
77  */
78 uword
79 unformat_srv6_un_localsid (unformat_input_t * input, va_list * args)
80 {
81   void **plugin_mem_p = va_arg (*args, void **);
82   srv6_un_localsid_t *ls_mem;
83
84   if (unformat (input, "uN(32b+16b)"))
85     {
86       /* Allocate a portion of memory */
87       ls_mem = clib_mem_alloc_aligned_at_offset (sizeof *ls_mem, 0, 0, 1);
88
89       /* Set to zero the memory */
90       clib_memset (ls_mem, 0, sizeof *ls_mem);
91
92       /* Dont forget to add it to the localsid */
93       *plugin_mem_p = ls_mem;
94       return 1;
95     }
96   return 0;
97 }
98
99 /*************************/
100 /* SRv6 LocalSID FIB DPO */
101 static u8 *
102 format_srv6_un_dpo (u8 * s, va_list * args)
103 {
104   index_t index = va_arg (*args, index_t);
105   CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
106
107   return (format (s, "SR: shift-and-forward 16b:[%u]", index));
108 }
109
110 void
111 srv6_un_dpo_lock (dpo_id_t * dpo)
112 {
113 }
114
115 void
116 srv6_un_dpo_unlock (dpo_id_t * dpo)
117 {
118 }
119
120 const static dpo_vft_t srv6_un_vft = {
121   .dv_lock = srv6_un_dpo_lock,
122   .dv_unlock = srv6_un_dpo_unlock,
123   .dv_format = format_srv6_un_dpo,
124 };
125
126 const static char *const srv6_un_ip6_nodes[] = {
127   "srv6-un-localsid",
128   NULL,
129 };
130
131 const static char *const *const srv6_un_nodes[DPO_PROTO_NUM] = {
132   [DPO_PROTO_IP6] = srv6_un_ip6_nodes,
133 };
134
135 /**********************/
136 static clib_error_t *
137 srv6_un_init (vlib_main_t * vm)
138 {
139   srv6_un_main_t *sm = &srv6_un_main;
140   int rv = 0;
141
142   sm->vlib_main = vm;
143   sm->vnet_main = vnet_get_main ();
144
145   /* Create DPO */
146   sm->srv6_un16_dpo_type =
147     dpo_register_new_type (&srv6_un_vft, srv6_un_nodes);
148
149   /* Register SRv6 LocalSID */
150   rv = sr_localsid_register_function (vm,
151                                       function_name,
152                                       keyword_str,
153                                       def_str,
154                                       params_str,
155                                       prefix_length,
156                                       &sm->srv6_un16_dpo_type,
157                                       format_srv6_un_localsid,
158                                       unformat_srv6_un_localsid,
159                                       srv6_un_localsid_creation_fn,
160                                       srv6_un_localsid_removal_fn);
161   if (rv < 0)
162     clib_error_return (0, "SRv6 LocalSID function could not be registered.");
163   else
164     sm->srv6_localsid_behavior_id = rv;
165
166   return 0;
167 }
168
169
170 /* *INDENT-OFF* */
171 VLIB_INIT_FUNCTION (srv6_un_init);
172
173 VLIB_PLUGIN_REGISTER () = {
174   .version = VPP_BUILD_VER,
175   .description = "Segment Routing Shift And Forward uN 16b",
176 };
177 /* *INDENT-ON* */
178
179 /*
180 * fd.io coding-style-patch-verification: ON
181 *
182 * Local Variables:
183 * eval: (c-set-style "gnu")
184 * End:
185 */