api: refactor vlibmemory
[vpp.git] / src / vnet / udp / udp_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 #include <vnet/vnet.h>
17 #include <vlibmemory/api.h>
18
19 #include <vnet/udp/udp_encap.h>
20 #include <vnet/fib/fib_table.h>
21
22 #include <vnet/vnet_msg_enum.h>
23
24 #define vl_typedefs             /* define message structures */
25 #include <vnet/vnet_all_api_h.h>
26 #undef vl_typedefs
27
28 #define vl_endianfun            /* define message structures */
29 #include <vnet/vnet_all_api_h.h>
30 #undef vl_endianfun
31
32 /* instantiate all the print functions we know about */
33 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
34 #define vl_printfun
35 #include <vnet/vnet_all_api_h.h>
36 #undef vl_printfun
37
38 #include <vlibapi/api_helper_macros.h>
39
40
41 #define foreach_udp_api_msg                      \
42   _(UDP_ENCAP_ADD_DEL, udp_encap_add_del)        \
43 _(UDP_ENCAP_DUMP, udp_encap_dump)
44
45 static void
46 send_udp_encap_details (const udp_encap_t * ue, svm_queue_t * q, u32 context)
47 {
48   vl_api_udp_encap_details_t *mp;
49   fib_table_t *fib_table;
50
51   mp = vl_msg_api_alloc (sizeof (*mp));
52   memset (mp, 0, sizeof (*mp));
53   mp->_vl_msg_id = ntohs (VL_API_UDP_ENCAP_DETAILS);
54   mp->context = context;
55
56   mp->is_ip6 = (ue->ue_ip_proto == FIB_PROTOCOL_IP6);
57
58   if (FIB_PROTOCOL_IP4 == ue->ue_ip_proto)
59     {
60       clib_memcpy (mp->src_ip, &ue->ue_hdrs.ip4.ue_ip4.src_address, 4);
61       clib_memcpy (mp->dst_ip, &ue->ue_hdrs.ip4.ue_ip4.dst_address, 4);
62       mp->src_port = htons (ue->ue_hdrs.ip4.ue_udp.src_port);
63       mp->dst_port = htons (ue->ue_hdrs.ip4.ue_udp.dst_port);
64     }
65   else
66     {
67       clib_memcpy (mp->src_ip, &ue->ue_hdrs.ip6.ue_ip6.src_address, 16);
68       clib_memcpy (mp->dst_ip, &ue->ue_hdrs.ip6.ue_ip6.dst_address, 16);
69       mp->src_port = htons (ue->ue_hdrs.ip6.ue_udp.src_port);
70       mp->dst_port = htons (ue->ue_hdrs.ip6.ue_udp.dst_port);
71     }
72
73   fib_table = fib_table_get (ue->ue_fib_index, ue->ue_ip_proto);
74   mp->table_id = htonl (fib_table->ft_table_id);
75   mp->id = htonl (ue->ue_id);
76
77   vl_msg_api_send_shmem (q, (u8 *) & mp);
78 }
79
80 static void
81 vl_api_udp_encap_dump_t_handler (vl_api_udp_encap_dump_t * mp,
82                                  vlib_main_t * vm)
83 {
84   svm_queue_t *q;
85   udp_encap_t *ue;
86
87   q = vl_api_client_index_to_input_queue (mp->client_index);
88   if (q == 0)
89     return;
90
91   /* *INDENT-OFF* */
92   pool_foreach(ue, udp_encap_pool,
93   ({
94     send_udp_encap_details(ue, q, mp->context);
95   }));
96   /* *INDENT-ON* */
97 }
98
99 static void
100 vl_api_udp_encap_add_del_t_handler (vl_api_udp_encap_add_del_t * mp,
101                                     vlib_main_t * vm)
102 {
103   vl_api_udp_encap_add_del_reply_t *rmp;
104   ip46_address_t src_ip, dst_ip;
105   u32 fib_index, table_id, ue_id;
106   fib_protocol_t fproto;
107   int rv = 0;
108
109   ue_id = ntohl (mp->id);
110   table_id = ntohl (mp->table_id);
111   fproto = (mp->is_ip6 ? FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
112
113   fib_index = fib_table_find (fproto, table_id);
114
115   if (~0 == fib_index)
116     {
117       rv = VNET_API_ERROR_NO_SUCH_TABLE;
118       goto done;
119     }
120
121   if (FIB_PROTOCOL_IP4 == fproto)
122     {
123       clib_memcpy (&src_ip.ip4, mp->src_ip, 4);
124       clib_memcpy (&dst_ip.ip4, mp->dst_ip, 4);
125     }
126   else
127     {
128       clib_memcpy (&src_ip.ip6, mp->src_ip, 16);
129       clib_memcpy (&dst_ip.ip6, mp->dst_ip, 16);
130     }
131
132   if (mp->is_add)
133     {
134       udp_encap_add_and_lock (ue_id, fproto, fib_index,
135                               &src_ip, &dst_ip,
136                               ntohs (mp->src_port),
137                               ntohs (mp->dst_port), UDP_ENCAP_FIXUP_NONE);
138     }
139   else
140     {
141       udp_encap_unlock (ue_id);
142     }
143
144 done:
145   REPLY_MACRO (VL_API_UDP_ENCAP_ADD_DEL_REPLY);
146 }
147
148 #define vl_msg_name_crc_list
149 #include <vnet/udp/udp.api.h>
150 #undef vl_msg_name_crc_list
151
152 static void
153 setup_message_id_table (api_main_t * am)
154 {
155 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
156   foreach_vl_msg_name_crc_udp;
157 #undef _
158 }
159
160 static clib_error_t *
161 udp_api_hookup (vlib_main_t * vm)
162 {
163   api_main_t *am = &api_main;
164
165 #define _(N,n)                                                  \
166     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
167                            vl_api_##n##_t_handler,              \
168                            vl_noop_handler,                     \
169                            vl_api_##n##_t_endian,               \
170                            vl_api_##n##_t_print,                \
171                            sizeof(vl_api_##n##_t), 1);
172   foreach_udp_api_msg;
173 #undef _
174
175   /*
176    * Set up the (msg_name, crc, message-id) table
177    */
178   setup_message_id_table (am);
179
180   return 0;
181 }
182
183 VLIB_API_INIT_FUNCTION (udp_api_hookup);
184
185 /*
186  * fd.io coding-style-patch-verification: ON
187  *
188  * Local Variables:
189  * eval: (c-set-style "gnu")
190  * End:
191  */