pppoe: make pppoe plugin work with dot1q subinterfaces
[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 #include <vnet/format_fns.h>
23
24 #include <vnet/ip/ip_types_api.h>
25
26 #define __plugin_msg_base pppoe_test_main.msg_id_base
27 #include <vlibapi/vat_helper_macros.h>
28
29
30 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
31 {
32   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
33   ip46_type_t type = va_arg (*args, ip46_type_t);
34   if ((type != IP46_TYPE_IP6) &&
35       unformat(input, "%U", unformat_ip4_address, &ip46->ip4)) {
36     ip46_address_mask_ip4(ip46);
37     return 1;
38   } else if ((type != IP46_TYPE_IP4) &&
39       unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
40     return 1;
41   }
42   return 0;
43 }
44 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
45 {
46   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
47   u8 *len = va_arg (*args, u8 *);
48   ip46_type_t type = va_arg (*args, ip46_type_t);
49
50   u32 l;
51   if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
52     if (l > 32)
53       return 0;
54     *len = l + 96;
55     ip46->pad[0] = ip46->pad[1] = ip46->pad[2] = 0;
56   } else if ((type != IP46_TYPE_IP4) && unformat(input, "%U/%u", unformat_ip6_address, &ip46->ip6, &l)) {
57     if (l > 128)
58       return 0;
59     *len = l;
60   } else {
61     return 0;
62   }
63   return 1;
64 }
65 /////////////////////////
66
67 #include <pppoe/pppoe.api_enum.h>
68 #include <pppoe/pppoe.api_types.h>
69
70 typedef struct {
71     /* API message ID base */
72     u16 msg_id_base;
73     vat_main_t *vat_main;
74 } pppoe_test_main_t;
75
76 pppoe_test_main_t pppoe_test_main;
77
78 static void vl_api_pppoe_add_del_session_reply_t_handler
79   (vl_api_pppoe_add_del_session_reply_t * mp)
80 {
81   vat_main_t *vam = &vat_main;
82   i32 retval = ntohl (mp->retval);
83   if (vam->async_mode)
84     {
85       vam->async_errors += (retval < 0);
86     }
87   else
88     {
89       vam->retval = retval;
90       vam->sw_if_index = ntohl (mp->sw_if_index);
91       vam->result_ready = 1;
92     }
93 }
94
95 static int
96 api_pppoe_add_del_session (vat_main_t * vam)
97 {
98   unformat_input_t *line_input = vam->input;
99   vl_api_pppoe_add_del_session_t *mp;
100   u16 session_id = 0;
101   ip46_address_t client_ip;
102   u8 is_add = 1;
103   u8 client_ip_set = 0;
104   u8 ipv4_set = 0;
105   u8 ipv6_set = 0;
106   u32 decap_vrf_id = 0;
107   u8 client_mac[6] = { 0 };
108   u8 client_mac_set = 0;
109   int ret;
110
111   /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
112   clib_memset (&client_ip, 0, sizeof client_ip);
113
114   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
115     {
116       if (unformat (line_input, "del"))
117         {
118           is_add = 0;
119         }
120       else if (unformat (line_input, "session_id %d", &session_id))
121         ;
122       else if (unformat (line_input, "client-ip %U",
123                          unformat_ip4_address, &client_ip.ip4))
124         {
125           client_ip_set = 1;
126           ipv4_set = 1;
127         }
128       else if (unformat (line_input, "client-ip %U",
129                          unformat_ip6_address, &client_ip.ip6))
130         {
131           client_ip_set = 1;
132           ipv6_set = 1;
133         }
134       else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
135         ;
136       else if (unformat (line_input, "client-mac %U", unformat_ethernet_address, client_mac))
137         client_mac_set = 1;
138       else
139         {
140           return -99;
141         }
142     }
143
144   if (client_ip_set == 0)
145     {
146       errmsg ("session client_ip address not specified");
147       return -99;
148     }
149
150   if (ipv4_set && ipv6_set)
151     {
152       errmsg ("both IPv4 and IPv6 addresses specified");
153       return -99;
154     }
155
156   if (client_mac_set == 0)
157     {
158       errmsg("session client mac not specified");
159       return -99;
160     }
161
162   M (PPPOE_ADD_DEL_SESSION, mp);
163
164
165   if (ipv6_set)
166     {
167       ip_address_encode(&client_ip, IP46_TYPE_IP6, &mp->client_ip);
168     }
169   else
170     {
171       ip_address_encode(&client_ip, IP46_TYPE_IP4, &mp->client_ip);
172     }
173
174   mp->decap_vrf_id = ntohl (decap_vrf_id);
175   mp->session_id = ntohl (session_id);
176   mp->is_add = is_add;
177   memcpy (mp->client_mac, client_mac, 6);
178
179   S (mp);
180   W (ret);
181   return ret;
182 }
183
184 static void vl_api_pppoe_session_details_t_handler
185   (vl_api_pppoe_session_details_t * mp)
186 {
187   vat_main_t *vam = &vat_main;
188   ip46_address_t client_ip;
189   ip_address_decode(&mp->client_ip, &client_ip);
190   print (vam->ofp, "%11d%14d%24U%14d%14d%30U%30U",
191        ntohl (mp->sw_if_index), ntohl (mp->session_id),
192        format_ip46_address, &client_ip, IP46_TYPE_ANY,
193        ntohl (mp->encap_if_index), ntohl (mp->decap_vrf_id),
194        format_ethernet_address, mp->local_mac,
195        format_ethernet_address, mp->client_mac);
196 }
197
198 static int
199 api_pppoe_session_dump (vat_main_t * vam)
200 {
201   unformat_input_t *i = vam->input;
202   vl_api_pppoe_session_dump_t *mp;
203   u32 sw_if_index;
204   u8 sw_if_index_set = 0;
205   int ret;
206
207   /* Parse args required to build the message */
208   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
209     {
210       if (unformat (i, "sw_if_index %d", &sw_if_index))
211       sw_if_index_set = 1;
212       else
213       break;
214     }
215
216   if (sw_if_index_set == 0)
217     {
218       sw_if_index = ~0;
219     }
220
221   if (!vam->json_output)
222     {
223       print (vam->ofp, "%11s%24s%14s%14s%14s",
224            "sw_if_index", "client_ip", "session_id",
225            "encap_if_index", "decap_fib_index",
226            "local-mac", "client-mac");
227     }
228
229   /* Get list of pppoe-session interfaces */
230   M (PPPOE_SESSION_DUMP, mp);
231
232   mp->sw_if_index = htonl (sw_if_index);
233
234   S (mp);
235
236   W (ret);
237   return ret;
238 }
239
240 static void vl_api_pppoe_add_del_cp_reply_t_handler
241   (vl_api_pppoe_add_del_session_reply_t * mp)
242 {
243   vat_main_t *vam = &vat_main;
244   i32 retval = ntohl (mp->retval);
245   if (vam->async_mode)
246     {
247       vam->async_errors += (retval < 0);
248     }
249   else
250     {
251       vam->retval = retval;
252       vam->result_ready = 1;
253     }
254 }
255
256 static int
257 api_pppoe_add_del_cp (vat_main_t * vam)
258 {
259   unformat_input_t *line_input = vam->input;
260   vl_api_pppoe_add_del_cp_t *mp;
261   u8 is_add = 1;
262   u32 sw_if_index = ~0;
263   int ret;
264
265   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
266     {
267       if (unformat (line_input, "del"))
268         {
269           is_add = 0;
270         }
271       else if (unformat (line_input, "cp-if-index %d", &sw_if_index))
272         ;
273     }
274
275   M (PPPOE_ADD_DEL_CP, mp);
276
277   mp->is_add = is_add;
278   mp->sw_if_index = sw_if_index;
279
280   S (mp);
281   W (ret);
282   return ret;
283 }
284
285 #include <pppoe/pppoe.api_test.c>