blob: a492c3f40e2d2d38619f386a39ef51b7ef110264 [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
Dave Barach6d963c22016-12-05 09:50:05 -0500158#define pub_sub_handler(lca,UCA) \
159static void vl_api_want_##lca##_t_handler ( \
160 vl_api_want_##lca##_t *mp) \
161{ \
162 vpe_api_main_t *vam = &vpe_api_main; \
163 vpe_client_registration_t *rp; \
164 vl_api_want_##lca##_reply_t *rmp; \
165 uword *p; \
166 i32 rv = 0; \
167 \
168 p = hash_get (vam->lca##_registration_hash, mp->client_index); \
169 if (p) { \
170 if (mp->enable_disable) { \
171 clib_warning ("pid %d: already enabled...", mp->pid); \
172 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
173 goto reply; \
174 } else { \
175 rp = pool_elt_at_index (vam->lca##_registrations, p[0]); \
176 pool_put (vam->lca##_registrations, rp); \
177 hash_unset (vam->lca##_registration_hash, \
178 mp->client_index); \
179 goto reply; \
180 } \
181 } \
182 if (mp->enable_disable == 0) { \
183 clib_warning ("pid %d: already disabled...", mp->pid); \
184 rv = VNET_API_ERROR_INVALID_REGISTRATION; \
185 goto reply; \
186 } \
187 pool_get (vam->lca##_registrations, rp); \
188 rp->client_index = mp->client_index; \
189 rp->client_pid = mp->pid; \
190 hash_set (vam->lca##_registration_hash, rp->client_index, \
191 rp - vam->lca##_registrations); \
192 \
193reply: \
194 REPLY_MACRO (VL_API_WANT_##UCA##_REPLY); \
195}
196
197#define foreach_registration_hash \
198_(interface_events) \
199_(to_netconf_server) \
200_(from_netconf_server) \
201_(to_netconf_client) \
202_(from_netconf_client) \
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200203_(oam_events) \
204_(bfd_events)
Dave Barach6d963c22016-12-05 09:50:05 -0500205
206/* WARNING: replicated in vpp/stats.h */
207typedef struct
208{
209 u32 client_index; /* in memclnt registration pool */
210 u32 client_pid;
211} vpe_client_registration_t;
212
213struct _vl_api_ip4_arp_event;
214struct _vl_api_ip6_nd_event;
215
216typedef struct
217{
218#define _(a) uword *a##_registration_hash; \
219 vpe_client_registration_t * a##_registrations;
220 foreach_registration_hash
221#undef _
222 /* notifications happen really early in the game */
223 u8 link_state_process_up;
224
225 /* ip4 arp event registration pool */
226 struct _vl_api_ip4_arp_event *arp_events;
227
228 /* ip6 nd event registration pool */
229 struct _vl_api_ip6_nd_event *nd_events;
230
231 /* convenience */
232 vlib_main_t *vlib_main;
233 vnet_main_t *vnet_main;
234} vpe_api_main_t;
235
236extern vpe_api_main_t vpe_api_main;
237
Dave Barachaff70772016-10-31 11:59:07 -0400238#endif /* __api_helper_macros_h__ */
239
240/*
241 * fd.io coding-style-patch-verification: ON
242 *
243 * Local Variables:
244 * eval: (c-set-style "gnu")
245 * End:
246 */