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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlibapi/api.h>
18 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21 #include <pppoe/pppoe.h>
23 #define __plugin_msg_base pppoe_test_main.msg_id_base
24 #include <vlibapi/vat_helper_macros.h>
27 uword unformat_ip46_address (unformat_input_t * input, va_list * args)
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);
35 } else if ((type != IP46_TYPE_IP4) &&
36 unformat(input, "%U", unformat_ip6_address, &ip46->ip6)) {
41 uword unformat_ip46_prefix (unformat_input_t * input, va_list * args)
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);
48 if ((type != IP46_TYPE_IP6) && unformat(input, "%U/%u", unformat_ip4_address, &ip46->ip4, &l)) {
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)) {
62 /////////////////////////
64 #define vl_msg_id(n,h) n,
66 #include <pppoe/pppoe.api.h>
67 /* We'll want to know how many messages IDs we need... */
68 VL_MSG_FIRST_AVAILABLE,
72 /* define message structures */
74 #include <pppoe/pppoe.api.h>
77 /* declare message handlers for each api */
79 #define vl_endianfun /* define message structures */
80 #include <pppoe/pppoe.api.h>
83 /* instantiate all the print functions we know about */
84 #define vl_print(handle, ...)
86 #include <pppoe/pppoe.api.h>
89 /* Get the API version number. */
90 #define vl_api_version(n,v) static u32 api_version=(v);
91 #include <pppoe/pppoe.api.h>
95 /* API message ID base */
100 pppoe_test_main_t pppoe_test_main;
102 static void vl_api_pppoe_add_del_session_reply_t_handler
103 (vl_api_pppoe_add_del_session_reply_t * mp)
105 vat_main_t *vam = &vat_main;
106 i32 retval = ntohl (mp->retval);
109 vam->async_errors += (retval < 0);
113 vam->retval = retval;
114 vam->sw_if_index = ntohl (mp->sw_if_index);
115 vam->result_ready = 1;
121 * Table of message reply handlers, must include boilerplate handlers
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)
130 api_pppoe_add_del_session (vat_main_t * vam)
132 unformat_input_t *line_input = vam->input;
133 vl_api_pppoe_add_del_session_t *mp;
135 ip46_address_t client_ip;
137 u8 client_ip_set = 0;
140 u32 decap_vrf_id = 0;
141 u8 client_mac[6] = { 0 };
142 u8 client_mac_set = 0;
145 /* Can't "universally zero init" (={0}) due to GCC bug 53119 */
146 memset (&client_ip, 0, sizeof client_ip);
148 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
150 if (unformat (line_input, "del"))
154 else if (unformat (line_input, "session_id %d", &session_id))
156 else if (unformat (line_input, "client-ip %U",
157 unformat_ip4_address, &client_ip.ip4))
162 else if (unformat (line_input, "client-ip %U",
163 unformat_ip6_address, &client_ip.ip6))
168 else if (unformat (line_input, "decap-vrf-id %d", &decap_vrf_id))
170 else if (unformat (line_input, "client-mac %U", unformat_ethernet_address, client_mac))
178 if (client_ip_set == 0)
180 errmsg ("session client_ip address not specified");
184 if (ipv4_set && ipv6_set)
186 errmsg ("both IPv4 and IPv6 addresses specified");
190 if (client_mac_set == 0)
192 errmsg("session client mac not specified");
196 M (PPPOE_ADD_DEL_SESSION, mp);
200 clib_memcpy (mp->client_ip, &client_ip.ip6, sizeof (client_ip.ip6));
204 clib_memcpy (mp->client_ip, &client_ip.ip4, sizeof (client_ip.ip4));
207 mp->decap_vrf_id = ntohl (decap_vrf_id);
208 mp->session_id = ntohl (session_id);
210 mp->is_ipv6 = ipv6_set;
211 memcpy (mp->client_mac, client_mac, 6);
218 static void vl_api_pppoe_session_details_t_handler
219 (vl_api_pppoe_session_details_t * mp)
221 vat_main_t *vam = &vat_main;
222 ip46_address_t client_ip = to_ip46 (mp->is_ipv6, mp->client_ip);
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);
233 api_pppoe_session_dump (vat_main_t * vam)
235 unformat_input_t *i = vam->input;
236 vl_api_pppoe_session_dump_t *mp;
238 u8 sw_if_index_set = 0;
241 /* Parse args required to build the message */
242 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
244 if (unformat (i, "sw_if_index %d", &sw_if_index))
250 if (sw_if_index_set == 0)
255 if (!vam->json_output)
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");
263 /* Get list of pppoe-session interfaces */
264 M (PPPOE_SESSION_DUMP, mp);
266 mp->sw_if_index = htonl (sw_if_index);
275 * List of messages that the api test plugin sends,
276 * and that the data plane plugin processes
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>]") \
286 pppoe_vat_api_hookup (vat_main_t *vam)
288 pppoe_test_main_t * pem = &pppoe_test_main;
289 /* Hook up handlers for replies from the data plane plug-in */
291 vl_msg_api_set_handlers((VL_API_##N + pem->msg_id_base), \
293 vl_api_##n##_t_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;
301 /* API messages we can send */
302 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
307 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
312 clib_error_t * vat_plugin_register (vat_main_t *vam)
314 pppoe_test_main_t * pem = &pppoe_test_main;
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);
324 if (pem->msg_id_base != (u16) ~0)
325 pppoe_vat_api_hookup (vam);