pppoe: remove api boilerplate
[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 #include <pppoe/pppoe.api_enum.h>
65 #include <pppoe/pppoe.api_types.h>
66
67 typedef struct {
68     /* API message ID base */
69     u16 msg_id_base;
70     vat_main_t *vat_main;
71 } pppoe_test_main_t;
72
73 pppoe_test_main_t pppoe_test_main;
74
75 static void vl_api_pppoe_add_del_session_reply_t_handler
76   (vl_api_pppoe_add_del_session_reply_t * mp)
77 {
78   vat_main_t *vam = &vat_main;
79   i32 retval = ntohl (mp->retval);
80   if (vam->async_mode)
81     {
82       vam->async_errors += (retval < 0);
83     }
84   else
85     {
86       vam->retval = retval;
87       vam->sw_if_index = ntohl (mp->sw_if_index);
88       vam->result_ready = 1;
89     }
90 }
91
92 static int
93 api_pppoe_add_del_session (vat_main_t * vam)
94 {
95   unformat_input_t *line_input = vam->input;
96   vl_api_pppoe_add_del_session_t *mp;
97   u16 session_id = 0;
98   ip46_address_t client_ip;
99   u8 is_add = 1;
100   u8 client_ip_set = 0;
101   u8 ipv4_set = 0;
102   u8 ipv6_set = 0;
103   u32 decap_vrf_id = 0;
104   u8 client_mac[6] = { 0 };
105   u8 client_mac_set = 0;
106   int ret;
107
108   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
109   clib_memset (&client_ip, 0, sizeof client_ip);
110
111   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
112     {
113       if (unformat (line_input, "del"))
114         {
115           is_add = 0;
116         }
117       else if (unformat (line_input, "session_id %d", &session_id))
118         ;
119       else if (unformat (line_input, "client-ip %U",
120                          unformat_ip4_address, &client_ip.ip4))
121         {
122           client_ip_set = 1;
123           ipv4_set = 1;
124         }
125       else if (unformat (line_input, "client-ip %U",
126                          unformat_ip6_address, &client_ip.ip6))
127         {
128           client_ip_set = 1;
129           ipv6_set = 1;
130         }
131       else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
132         ;
133       else if (unformat (line_input, "client-mac %U", unformat_ethernet_address, client_mac))
134         client_mac_set = 1;
135       else
136         {
137           return -99;
138         }
139     }
140
141   if (client_ip_set == 0)
142     {
143       errmsg ("session client_ip address not specified");
144       return -99;
145     }
146
147   if (ipv4_set && ipv6_set)
148     {
149       errmsg ("both IPv4 and IPv6 addresses specified");
150       return -99;
151     }
152
153   if (client_mac_set == 0)
154     {
155       errmsg("session client mac not specified");
156       return -99;
157     }
158
159   M (PPPOE_ADD_DEL_SESSION, mp);
160
161   if (ipv6_set)
162     {
163       clib_memcpy (mp->client_ip, &client_ip.ip6, sizeof (client_ip.ip6));
164     }
165   else
166     {
167       clib_memcpy (mp->client_ip, &client_ip.ip4, sizeof (client_ip.ip4));
168     }
169
170   mp->decap_vrf_id = ntohl (decap_vrf_id);
171   mp->session_id = ntohl (session_id);
172   mp->is_add = is_add;
173   mp->is_ipv6 = ipv6_set;
174   memcpy (mp->client_mac, client_mac, 6);
175
176   S (mp);
177   W (ret);
178   return ret;
179 }
180
181 static void vl_api_pppoe_session_details_t_handler
182   (vl_api_pppoe_session_details_t * mp)
183 {
184   vat_main_t *vam = &vat_main;
185   ip46_address_t client_ip = to_ip46 (mp->is_ipv6, mp->client_ip);
186
187   print (vam->ofp, "%11d%14d%24U%14d%14d%30U%30U",
188        ntohl (mp->sw_if_index), ntohl (mp->session_id),
189        format_ip46_address, &client_ip, IP46_TYPE_ANY,
190        ntohl (mp->encap_if_index), ntohl (mp->decap_vrf_id),
191        format_ethernet_address, mp->local_mac,
192        format_ethernet_address, mp->client_mac);
193 }
194
195 static int
196 api_pppoe_session_dump (vat_main_t * vam)
197 {
198   unformat_input_t *i = vam->input;
199   vl_api_pppoe_session_dump_t *mp;
200   u32 sw_if_index;
201   u8 sw_if_index_set = 0;
202   int ret;
203
204   /* Parse args required to build the message */
205   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
206     {
207       if (unformat (i, "sw_if_index %d", &sw_if_index))
208       sw_if_index_set = 1;
209       else
210       break;
211     }
212
213   if (sw_if_index_set == 0)
214     {
215       sw_if_index = ~0;
216     }
217
218   if (!vam->json_output)
219     {
220       print (vam->ofp, "%11s%24s%14s%14s%14s",
221            "sw_if_index", "client_ip", "session_id",
222            "encap_if_index", "decap_fib_index",
223            "local-mac", "client-mac");
224     }
225
226   /* Get list of pppoe-session interfaces */
227   M (PPPOE_SESSION_DUMP, mp);
228
229   mp->sw_if_index = htonl (sw_if_index);
230
231   S (mp);
232
233   W (ret);
234   return ret;
235 }
236
237 #include <pppoe/pppoe.api_test.c>