blob: 2e29d4d798359db0826ade0f9a98bc5a12867e85 [file] [log] [blame]
Dave Barachaff70772016-10-31 11:59:07 -04001/*
2 *------------------------------------------------------------------
3 * api_helper_macros.h - message handler helper macros
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
Dave Barachaff70772016-10-31 11:59:07 -040020#ifndef __api_helper_macros_h__
21#define __api_helper_macros_h__
22
23#define f64_endian(a)
24#define f64_print(a,b)
25
Eyal Bari0856b972017-03-15 14:54:19 +020026#ifndef REPLY_MSG_ID_BASE
27#define REPLY_MSG_ID_BASE 0
28#endif
29
Dave Barachaff70772016-10-31 11:59:07 -040030#define REPLY_MACRO(t) \
31do { \
32 unix_shared_memory_queue_t * q; \
33 rv = vl_msg_api_pd_handler (mp, rv); \
34 q = vl_api_client_index_to_input_queue (mp->client_index); \
35 if (!q) \
36 return; \
37 \
38 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
Eyal Barib614d082017-03-16 10:02:57 +020039 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
Dave Barachaff70772016-10-31 11:59:07 -040040 rmp->context = mp->context; \
41 rmp->retval = ntohl(rv); \
42 \
43 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
44} while(0);
45
46#define REPLY_MACRO2(t, body) \
47do { \
48 unix_shared_memory_queue_t * q; \
49 rv = vl_msg_api_pd_handler (mp, rv); \
50 q = vl_api_client_index_to_input_queue (mp->client_index); \
51 if (!q) \
52 return; \
53 \
54 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
Eyal Barib614d082017-03-16 10:02:57 +020055 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
Dave Barachaff70772016-10-31 11:59:07 -040056 rmp->context = mp->context; \
57 rmp->retval = ntohl(rv); \
58 do {body;} while (0); \
59 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
60} while(0);
61
62#define REPLY_MACRO3(t, n, body) \
63do { \
64 unix_shared_memory_queue_t * q; \
65 rv = vl_msg_api_pd_handler (mp, rv); \
66 q = vl_api_client_index_to_input_queue (mp->client_index); \
67 if (!q) \
68 return; \
69 \
70 rmp = vl_msg_api_alloc (sizeof (*rmp) + n); \
Eyal Barib614d082017-03-16 10:02:57 +020071 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
Dave Barachaff70772016-10-31 11:59:07 -040072 rmp->context = mp->context; \
73 rmp->retval = ntohl(rv); \
74 do {body;} while (0); \
75 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
76} while(0);
77
78#define REPLY_MACRO4(t, n, body) \
79do { \
80 unix_shared_memory_queue_t * q; \
81 u8 is_error = 0; \
82 rv = vl_msg_api_pd_handler (mp, rv); \
83 q = vl_api_client_index_to_input_queue (mp->client_index); \
84 if (!q) \
85 return; \
86 \
87 rmp = vl_msg_api_alloc_or_null (sizeof (*rmp) + n); \
88 if (!rmp) \
89 { \
90 /* if there isn't enough memory, try to allocate */ \
91 /* some at least for returning an error */ \
92 rmp = vl_msg_api_alloc (sizeof (*rmp)); \
93 if (!rmp) \
94 return; \
95 \
96 memset (rmp, 0, sizeof (*rmp)); \
97 rv = VNET_API_ERROR_TABLE_TOO_BIG; \
98 is_error = 1; \
99 } \
Eyal Barib614d082017-03-16 10:02:57 +0200100 rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
Dave Barachaff70772016-10-31 11:59:07 -0400101 rmp->context = mp->context; \
102 rmp->retval = ntohl(rv); \
103 if (!is_error) \
104 do {body;} while (0); \
105 vl_msg_api_send_shmem (q, (u8 *)&rmp); \
106} while(0);
107
108/* "trust, but verify" */
109
Eyal Bari1c82cd42017-03-14 14:39:51 +0200110static inline uword
111vnet_sw_if_index_is_api_valid (u32 sw_if_index)
112{
113 return vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index);
114}
115
Dave Barachaff70772016-10-31 11:59:07 -0400116#define VALIDATE_SW_IF_INDEX(mp) \
117 do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200118 if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400119 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
120 goto bad_sw_if_index; \
121 } \
122} while(0);
123
124#define BAD_SW_IF_INDEX_LABEL \
125do { \
126bad_sw_if_index: \
127 ; \
128} while (0);
129
130#define VALIDATE_RX_SW_IF_INDEX(mp) \
131 do { u32 __rx_sw_if_index = ntohl(mp->rx_sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200132 if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400133 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
134 goto bad_rx_sw_if_index; \
135 } \
136} while(0);
137
138#define BAD_RX_SW_IF_INDEX_LABEL \
139do { \
140bad_rx_sw_if_index: \
141 ; \
142} while (0);
143
144#define VALIDATE_TX_SW_IF_INDEX(mp) \
145 do { u32 __tx_sw_if_index = ntohl(mp->tx_sw_if_index); \
Eyal Bari1c82cd42017-03-14 14:39:51 +0200146 if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
Dave Barachaff70772016-10-31 11:59:07 -0400147 rv = VNET_API_ERROR_INVALID_SW_IF_INDEX; \
148 goto bad_tx_sw_if_index; \
149 } \
150} while(0);
151
152#define BAD_TX_SW_IF_INDEX_LABEL \
153do { \
154bad_tx_sw_if_index: \
155 ; \
156} while (0);
157
John Lo97934772017-05-18 22:26:47 -0400158#define VALIDATE_BD_ID(mp) \
159 do { u32 __rx_bd_id = ntohl(mp->bd_id); \
160 if (__rx_bd_id > L2_BD_ID_MAX) { \
161 rv = VNET_API_ERROR_BD_ID_EXCEED_MAX; \
162 goto bad_bd_id; \
163 } \
164} while(0);
165
166#define BAD_BD_ID_LABEL \
167do { \
168bad_bd_id: \
169 ; \
170} while (0);
171
Dave Barach6d963c22016-12-05 09:50:05 -0500172#define pub_sub_handler(lca,UCA) \
173static void vl_api_want_##lca##_t_handler ( \
174 vl_api_want_##lca##_t *mp) \
175{ \
176 vpe_api_main_t *vam = &vpe_api_main; \
177 vpe_client_registration_t *rp; \
178 vl_api_want_##lca##_reply_t *rmp; \
179 uword *p; \
180 i32 rv = 0; \
181 \
182 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
183 if (p) { \
184 if (mp->enable_disable) { \
185 clib_warning ("pid %d: already enabled...", mp->pid); \
186 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
187 goto reply; \
188 } else { \
189 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
190 pool_put (vam->lca##_registrations, rp); \
191 hash_unset (vam->lca##_registration_hash, \
192 mp->client_index); \
193 goto reply; \
194 } \
195 } \
196 if (mp->enable_disable == 0) { \
197 clib_warning ("pid %d: already disabled...", mp->pid); \
198 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
199 goto reply; \
200 } \
201 pool_get (vam->lca##_registrations, rp); \
202 rp->client_index = mp->client_index; \
203 rp->client_pid = mp->pid; \
204 hash_set (vam->lca##_registration_hash, rp->client_index, \
205 rp - vam->lca##_registrations); \
206 \
207reply: \
208 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
209}
210
211#define foreach_registration_hash \
212_(interface_events) \
213_(to_netconf_server) \
214_(from_netconf_server) \
215_(to_netconf_client) \
216_(from_netconf_client) \
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200217_(oam_events) \
218_(bfd_events)
Dave Barach6d963c22016-12-05 09:50:05 -0500219
220/* WARNING: replicated in vpp/stats.h */
221typedef struct
222{
223 u32 client_index; /* in memclnt registration pool */
224 u32 client_pid;
225} vpe_client_registration_t;
226
227struct _vl_api_ip4_arp_event;
228struct _vl_api_ip6_nd_event;
229
230typedef struct
231{
232#define _(a) uword *a##_registration_hash; \
233 vpe_client_registration_t * a##_registrations;
234 foreach_registration_hash
235#undef _
236 /* notifications happen really early in the game */
237 u8 link_state_process_up;
238
239 /* ip4 arp event registration pool */
240 struct _vl_api_ip4_arp_event *arp_events;
241
242 /* ip6 nd event registration pool */
243 struct _vl_api_ip6_nd_event *nd_events;
244
245 /* convenience */
246 vlib_main_t *vlib_main;
247 vnet_main_t *vnet_main;
248} vpe_api_main_t;
249
250extern vpe_api_main_t vpe_api_main;
251
Dave Barachaff70772016-10-31 11:59:07 -0400252#endif /* __api_helper_macros_h__ */
253
254/*
255 * fd.io coding-style-patch-verification: ON
256 *
257 * Local Variables:
258 * eval: (c-set-style "gnu")
259 * End:
260 */