8cb8cc962240b9fdf3257e06c396b5d3cdb22ee9
[vpp.git] / src / plugins / ioam / udp-ping / udp_ping_api.c
1 /*
2  * Copyright (c) 2017 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  * udp_ping_api.c - UDP Ping related APIs to create
18  *             and maintain ping flows
19  *------------------------------------------------------------------
20  */
21
22 #include <vnet/vnet.h>
23 #include <vnet/ip/ip.h>
24 #include <ioam/udp-ping/udp_ping.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28 #include <vlibsocket/api.h>
29
30 /* define message IDs */
31 #include <ioam/udp-ping/udp_ping_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <ioam/udp-ping/udp_ping_all_api_h.h>
36 #undef vl_typedefs
37
38 /* define generated endian-swappers */
39 #define vl_endianfun
40 #include <ioam/udp-ping/udp_ping_all_api_h.h>
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
45 #define vl_printfun
46 #include <ioam/udp-ping/udp_ping_all_api_h.h>
47 #undef vl_printfun
48
49 /* Get the API version number */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <ioam/udp-ping/udp_ping_all_api_h.h>
52 #undef vl_api_version
53
54 /*
55  * A handy macro to set up a message reply.
56  * Assumes that the following variables are available:
57  * mp - pointer to request message
58  * rmp - pointer to reply message type
59  * rv - return value
60  */
61
62 #define REPLY_MACRO(t)                                          \
63     do {                                                            \
64         unix_shared_memory_queue_t * q =                            \
65         vl_api_client_index_to_input_queue (mp->client_index);      \
66         if (!q)                                                     \
67         return;                                                 \
68         \
69         rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
70         rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
71         rmp->context = mp->context;                                 \
72         rmp->retval = ntohl(rv);                                    \
73         \
74         vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
75     } while(0);
76
77 #define REPLY_MACRO2(t, body)                                   \
78     do {                                                            \
79         unix_shared_memory_queue_t * q;                             \
80         rv = vl_msg_api_pd_handler (mp, rv);                        \
81         q = vl_api_client_index_to_input_queue (mp->client_index);  \
82         if (!q)                                                     \
83         return;                                                 \
84         \
85         rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
86         rmp->_vl_msg_id = ntohs((t));                               \
87         rmp->context = mp->context;                                 \
88         rmp->retval = ntohl(rv);                                    \
89         do {body;} while (0);                                       \
90         vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
91     } while(0);
92
93 /* List of message types that this module understands */
94
95 #define foreach_udp_ping_api_msg                                      \
96     _(UDP_PING_ADD_DEL_REQ, udp_ping_add_del_req)                                     \
97     _(UDP_PING_EXPORT_REQ, udp_ping_export_req)                                     \
98
99 static void vl_api_udp_ping_add_del_req_t_handler
100   (vl_api_udp_ping_add_del_req_t * mp)
101 {
102   ip46_address_t dst, src;
103   int rv = 0;
104   udp_ping_main_t *sm = &udp_ping_main;
105   vl_api_udp_ping_add_del_reply_t *rmp;
106
107   if (mp->is_ipv4)
108     {
109       rv = -1;                  //Not supported
110       goto ERROROUT;
111     }
112
113   clib_memcpy ((void *) &src.ip6, (void *) mp->src_ip_address,
114                sizeof (ip6_address_t));
115   clib_memcpy ((void *) &dst.ip6, (void *) mp->dst_ip_address,
116                sizeof (ip6_address_t));
117
118   ip46_udp_ping_set_flow (src, dst,
119                           ntohs (mp->start_src_port),
120                           ntohs (mp->end_src_port),
121                           ntohs (mp->start_dst_port),
122                           ntohs (mp->end_dst_port),
123                           ntohs (mp->interval), mp->fault_det, mp->dis);
124   rv = 0;                       //FIXME
125
126 ERROROUT:
127   REPLY_MACRO (VL_API_UDP_PING_ADD_DEL_REPLY);
128 }
129
130 static void vl_api_udp_ping_export_req_t_handler
131   (vl_api_udp_ping_export_req_t * mp)
132 {
133   udp_ping_main_t *sm = &udp_ping_main;
134   int rv = 0;
135   vl_api_udp_ping_export_reply_t *rmp;
136
137   (void) udp_ping_flow_create (!mp->enable);
138   rv = 0;                       //FIXME
139
140   REPLY_MACRO (VL_API_UDP_PING_EXPORT_REPLY);
141 }
142
143 /* Set up the API message handling tables */
144 static clib_error_t *
145 udp_ping_api_hookup (vlib_main_t * vm)
146 {
147   udp_ping_main_t *sm = &udp_ping_main;
148 #define _(N,n)                                                  \
149     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
150                             #n,                                 \
151                             vl_api_##n##_t_handler,              \
152                             vl_noop_handler,                     \
153                             vl_api_##n##_t_endian,               \
154                             vl_api_##n##_t_print,                \
155                             sizeof(vl_api_##n##_t), 1);
156   foreach_udp_ping_api_msg;
157 #undef _
158
159   return 0;
160 }
161
162 static clib_error_t *
163 udp_ping_api_init (vlib_main_t * vm)
164 {
165   udp_ping_main_t *sm = &udp_ping_main;
166   clib_error_t *error = 0;
167   u8 *name;
168
169   name = format (0, "udp_ping_%08x%c", api_version, 0);
170
171   /* Ask for a correctly-sized block of API message decode slots */
172   sm->msg_id_base = vl_msg_api_get_msg_ids
173     ((char *) name, VL_MSG_FIRST_AVAILABLE);
174
175   error = udp_ping_api_hookup (vm);
176
177   vec_free (name);
178
179   return error;
180 }
181
182 VLIB_INIT_FUNCTION (udp_ping_api_init);
183
184 /*
185  * fd.io coding-style-patch-verification: ON
186  *
187  * Local Variables:
188  * eval: (c-set-style "gnu")
189  * End:
190  */