a32f1b76fe0ea8cde8017e9039f0d8c83a569f1c
[vpp.git] / src / plugins / pppoe / pppoe_test.c
1 /*
2  * Copyright (c) 2017 Intel 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 #include <vat/vat.h>
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
19
20 #include <vppinfra/error.h>
21 #include <pppoe/pppoe.h>
22
23 #define __plugin_msg_base pppoe_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
25
26
27 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
28 {
29   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
30   ip46_type_t type = va_arg (*args, ip46_type_t);
31   if ((type != IP46_TYPE_IP6) &&
32       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
33     ip46_address_mask_ip4(ip46);
34     return 1;
35   } else if ((type != IP46_TYPE_IP4) &&
36       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
37     return 1;
38   }
39   return 0;
40 }
41 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
42 {
43   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
44   u8 *len = va_arg (*args, u8 *);
45   ip46_type_t type = va_arg (*args, ip46_type_t);
46
47   u32 l;
48   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
49     if (l > 32)
50       return 0;
51     *len = l + 96;
52     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
53   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
54     if (l > 128)
55       return 0;
56     *len = l;
57   } else {
58     return 0;
59   }
60   return 1;
61 }
62 /////////////////////////
63
64 #define vl_msg_id(n,h) n,
65 typedef enum {
66 #include <pppoe/pppoe.api.h>
67     /* We'll want to know how many messages IDs we need... */
68     VL_MSG_FIRST_AVAILABLE,
69 } vl_msg_id_t;
70 #undef vl_msg_id
71
72 /* define message structures */
73 #define vl_typedefs
74 #include <pppoe/pppoe.api.h>
75 #undef vl_typedefs
76
77 /* declare message handlers for each api */
78
79 #define vl_endianfun             /* define message structures */
80 #include <pppoe/pppoe.api.h>
81 #undef vl_endianfun
82
83 /* instantiate all the print functions we know about */
84 #define vl_print(handle, ...)
85 #define vl_printfun
86 #include <pppoe/pppoe.api.h>
87 #undef vl_printfun
88
89 /* Get the API version number. */
90 #define vl_api_version(n,v) static u32 api_version=(v);
91 #include <pppoe/pppoe.api.h>
92 #undef vl_api_version
93
94 typedef struct {
95     /* API message ID base */
96     u16 msg_id_base;
97     vat_main_t *vat_main;
98 } pppoe_test_main_t;
99
100 pppoe_test_main_t pppoe_test_main;
101
102 static void vl_api_pppoe_add_del_session_reply_t_handler
103   (vl_api_pppoe_add_del_session_reply_t * mp)
104 {
105   vat_main_t *vam = &vat_main;
106   i32 retval = ntohl (mp->retval);
107   if (vam->async_mode)
108     {
109       vam->async_errors += (retval < 0);
110     }
111   else
112     {
113       vam->retval = retval;
114       vam->sw_if_index = ntohl (mp->sw_if_index);
115       vam->result_ready = 1;
116     }
117 }
118
119
120 /*
121  * Table of message reply handlers, must include boilerplate handlers
122  * we just generated
123  */
124 #define foreach_vpe_api_reply_msg                               \
125   _(PPPOE_ADD_DEL_SESSION_REPLY, pppoe_add_del_session_reply)               \
126   _(PPPOE_SESSION_DETAILS, pppoe_session_details)
127
128
129 static int
130 api_pppoe_add_del_session (vat_main_t * vam)
131 {
132   unformat_input_t *line_input = vam->input;
133   vl_api_pppoe_add_del_session_t *mp;
134   u16 session_id = 0;
135   ip46_address_t client_ip;
136   u8 is_add = 1;
137   u8 client_ip_set = 0;
138   u8 ipv4_set = 0;
139   u8 ipv6_set = 0;
140   u32 decap_vrf_id = 0;
141   u8 client_mac[6] = { 0 };
142   u8 client_mac_set = 0;
143   int ret;
144
145   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
146   memset (&client_ip, 0, sizeof client_ip);
147
148   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
149     {
150       if (unformat (line_input, "del"))
151         {
152           is_add = 0;
153         }
154       else if (unformat (line_input, "session_id %d", &session_id))
155         ;
156       else if (unformat (line_input, "client-ip %U",
157                          unformat_ip4_address, &client_ip.ip4))
158         {
159           client_ip_set = 1;
160           ipv4_set = 1;
161         }
162       else if (unformat (line_input, "client-ip %U",
163                          unformat_ip6_address, &client_ip.ip6))
164         {
165           client_ip_set = 1;
166           ipv6_set = 1;
167         }
168       else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
169         ;
170       else if (unformat (line_input, "client-mac %U", unformat_ethernet_address, client_mac))
171         client_mac_set = 1;
172       else
173         {
174           return -99;
175         }
176     }
177
178   if (client_ip_set == 0)
179     {
180       errmsg ("session client_ip address not specified");
181       return -99;
182     }
183
184   if (ipv4_set && ipv6_set)
185     {
186       errmsg ("both IPv4 and IPv6 addresses specified");
187       return -99;
188     }
189
190   if (client_mac_set == 0)
191     {
192       errmsg("session client mac not specified");
193       return -99;
194     }
195
196   M (PPPOE_ADD_DEL_SESSION, mp);
197
198   if (ipv6_set)
199     {
200       clib_memcpy (mp->client_ip, &client_ip.ip6, sizeof (client_ip.ip6));
201     }
202   else
203     {
204       clib_memcpy (mp->client_ip, &client_ip.ip4, sizeof (client_ip.ip4));
205     }
206
207   mp->decap_vrf_id = ntohl (decap_vrf_id);
208   mp->session_id = ntohl (session_id);
209   mp->is_add = is_add;
210   mp->is_ipv6 = ipv6_set;
211   memcpy (mp->client_mac, client_mac, 6);
212
213   S (mp);
214   W (ret);
215   return ret;
216 }
217
218 static void vl_api_pppoe_session_details_t_handler
219   (vl_api_pppoe_session_details_t * mp)
220 {
221   vat_main_t *vam = &vat_main;
222   ip46_address_t client_ip = to_ip46 (mp->is_ipv6, mp->client_ip);
223
224   print (vam->ofp, "%11d%14d%24U%14d%14d%30U%30U",
225        ntohl (mp->sw_if_index), ntohl (mp->session_id),
226        format_ip46_address, &client_ip, IP46_TYPE_ANY,
227        ntohl (mp->encap_if_index), ntohl (mp->decap_vrf_id),
228        format_ethernet_address, mp->local_mac,
229        format_ethernet_address, mp->client_mac);
230 }
231
232 static int
233 api_pppoe_session_dump (vat_main_t * vam)
234 {
235   unformat_input_t *i = vam->input;
236   vl_api_pppoe_session_dump_t *mp;
237   u32 sw_if_index;
238   u8 sw_if_index_set = 0;
239   int ret;
240
241   /* Parse args required to build the message */
242   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
243     {
244       if (unformat (i, "sw_if_index %d", &sw_if_index))
245       sw_if_index_set = 1;
246       else
247       break;
248     }
249
250   if (sw_if_index_set == 0)
251     {
252       sw_if_index = ~0;
253     }
254
255   if (!vam->json_output)
256     {
257       print (vam->ofp, "%11s%24s%14s%14s%14s",
258            "sw_if_index", "client_ip", "session_id",
259            "encap_if_index", "decap_fib_index",
260            "local-mac", "client-mac");
261     }
262
263   /* Get list of pppoe-session interfaces */
264   M (PPPOE_SESSION_DUMP, mp);
265
266   mp->sw_if_index = htonl (sw_if_index);
267
268   S (mp);
269
270   W (ret);
271   return ret;
272 }
273
274 /*
275  * List of messages that the api test plugin sends,
276  * and that the data plane plugin processes
277  */
278 #define foreach_vpe_api_msg                                            \
279 _(pppoe_add_del_session,                                                 \
280   " client-addr <client-addr> session-id <nn>"                            \
281   " [encap-if-index <nn>] [decap-next [ip4|ip6|node <name>]]"             \
282   " local-mac <local-mac> client-mac <client-mac> [del]") \
283 _(pppoe_session_dump, "[<intfc> | sw_if_index <nn>]")                    \
284
285 static void
286 pppoe_vat_api_hookup (vat_main_t *vam)
287 {
288   pppoe_test_main_t * pem = &pppoe_test_main;
289   /* Hook up handlers for replies from the data plane plug-in */
290 #define _(N,n)                                                  \
291   vl_msg_api_set_handlers((VL_API_##N + pem->msg_id_base),       \
292                           #n,                                   \
293                           vl_api_##n##_t_handler,               \
294                           vl_noop_handler,                      \
295                           vl_api_##n##_t_endian,                \
296                           vl_api_##n##_t_print,                 \
297                           sizeof(vl_api_##n##_t), 1);
298   foreach_vpe_api_reply_msg;
299 #undef _
300
301   /* API messages we can send */
302 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
303   foreach_vpe_api_msg;
304 #undef _
305
306   /* Help strings */
307 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
308   foreach_vpe_api_msg;
309 #undef _
310 }
311
312 clib_error_t * vat_plugin_register (vat_main_t *vam)
313 {
314   pppoe_test_main_t * pem = &pppoe_test_main;
315
316   u8 * name;
317
318   pem->vat_main = vam;
319
320   /* Ask the vpp engine for the first assigned message-id */
321   name = format (0, "pppoe_%08x%c", api_version, 0);
322   pem->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
323
324   if (pem->msg_id_base != (u16) ~0)
325     pppoe_vat_api_hookup (vam);
326
327   vec_free(name);
328
329   return 0;
330 }