55ab838b167188504e3e67f198bc34bc9af07939
[vpp.git] / src / vnet / ip / punt_api.c
1 /*
2  *------------------------------------------------------------------
3  * punt_api.c - Punt api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/ip/punt.h>
23
24 #include <vnet/vnet_msg_enum.h>
25
26 #define vl_typedefs             /* define message structures */
27 #include <vnet/vnet_all_api_h.h>
28 #undef vl_typedefs
29
30 #define vl_endianfun            /* define message structures */
31 #include <vnet/vnet_all_api_h.h>
32 #undef vl_endianfun
33
34 /* instantiate all the print functions we know about */
35 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
36 #define vl_printfun
37 #include <vnet/vnet_all_api_h.h>
38 #undef vl_printfun
39
40 #include <vlibapi/api_helper_macros.h>
41
42 #define foreach_punt_api_msg                                            \
43 _(PUNT, punt)                                                           \
44 _(PUNT_SOCKET_REGISTER, punt_socket_register)                           \
45 _(PUNT_SOCKET_DEREGISTER, punt_socket_deregister)
46
47 static void
48 vl_api_punt_t_handler (vl_api_punt_t * mp)
49 {
50   vl_api_punt_reply_t *rmp;
51   vlib_main_t *vm = vlib_get_main ();
52   int rv = 0;
53   clib_error_t *error;
54
55   error = vnet_punt_add_del (vm, mp->ipv, mp->l4_protocol,
56                              ntohs (mp->l4_port), mp->is_add);
57   if (error)
58     {
59       rv = -1;
60       clib_error_report (error);
61     }
62
63   REPLY_MACRO (VL_API_PUNT_REPLY);
64 }
65
66 static void
67 vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
68 {
69   vl_api_punt_socket_register_reply_t *rmp;
70   vlib_main_t *vm = vlib_get_main ();
71   int rv = 0;
72   clib_error_t *error;
73   vl_api_registration_t *reg;
74
75   error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
76                                 mp->is_ip4, mp->l4_protocol,
77                                 ntohs (mp->l4_port), (char *) mp->pathname);
78   if (error)
79     {
80       rv = -1;
81       clib_error_report (error);
82     }
83
84   reg = vl_api_client_index_to_registration (mp->client_index);
85   if (!reg)
86     return;
87
88   rmp = vl_msg_api_alloc (sizeof (*rmp));
89   rmp->_vl_msg_id = htons (VL_API_PUNT_SOCKET_REGISTER_REPLY);
90   rmp->context = mp->context;
91   rmp->retval = htonl (rv);
92   char *p = vnet_punt_get_server_pathname ();
93   /* Abstract pathnames start with \0 */
94   memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
95   vl_api_send_msg (reg, (u8 *) rmp);
96 }
97
98 static void
99 vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
100 {
101   vl_api_punt_socket_deregister_reply_t *rmp;
102   vlib_main_t *vm = vlib_get_main ();
103   int rv = 0;
104   clib_error_t *error;
105   vl_api_registration_t *reg;
106
107   error = vnet_punt_socket_del (vm, mp->is_ip4, mp->l4_protocol,
108                                 ntohs (mp->l4_port));
109   if (error)
110     {
111       rv = -1;
112       clib_error_report (error);
113     }
114
115   reg = vl_api_client_index_to_registration (mp->client_index);
116   if (!reg)
117     return;
118
119   rmp = vl_msg_api_alloc (sizeof (*rmp));
120   rmp->_vl_msg_id = htons (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
121   rmp->context = mp->context;
122   rmp->retval = htonl (rv);
123   vl_api_send_msg (reg, (u8 *) rmp);
124 }
125
126 #define vl_msg_name_crc_list
127 #include <vnet/ip/punt.api.h>
128 #undef vl_msg_name_crc_list
129
130 static void
131 setup_message_id_table (api_main_t * am)
132 {
133 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
134   foreach_vl_msg_name_crc_punt;
135 #undef _
136 }
137
138 static clib_error_t *
139 punt_api_hookup (vlib_main_t * vm)
140 {
141   api_main_t *am = &api_main;
142
143 #define _(N,n)                                                  \
144     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
145                            vl_api_##n##_t_handler,              \
146                            vl_noop_handler,                     \
147                            vl_api_##n##_t_endian,               \
148                            vl_api_##n##_t_print,                \
149                            sizeof(vl_api_##n##_t), 1);
150   foreach_punt_api_msg;
151 #undef _
152
153   /*
154    * Set up the (msg_name, crc, message-id) table
155    */
156   setup_message_id_table (am);
157
158   return 0;
159 }
160
161 VLIB_API_INIT_FUNCTION (punt_api_hookup);
162
163
164 /*
165  * fd.io coding-style-patch-verification: ON
166  *
167  * Local Variables:
168  * eval: (c-set-style "gnu")
169  * End:
170  */