FD.io VPP  v17.04-9-g99c0734
Vector Packet Processing
api_helper_macros.h
Go to the documentation of this file.
1 /*
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 
20 #ifndef __api_helper_macros_h__
21 #define __api_helper_macros_h__
22 
23 #define f64_endian(a)
24 #define f64_print(a,b)
25 
26 #ifndef REPLY_MSG_ID_BASE
27 #define REPLY_MSG_ID_BASE 0
28 #endif
29 
30 #define REPLY_MACRO(t) \
31 do { \
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)); \
39  rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
40  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) \
47 do { \
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)); \
55  rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
56  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) \
63 do { \
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); \
71  rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
72  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) \
79 do { \
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  } \
100  rmp->_vl_msg_id = htons((t)+(REPLY_MSG_ID_BASE)); \
101  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 
110 static inline uword
112 {
113  return vnet_sw_interface_is_api_valid (vnet_get_main (), sw_if_index);
114 }
115 
116 #define VALIDATE_SW_IF_INDEX(mp) \
117  do { u32 __sw_if_index = ntohl(mp->sw_if_index); \
118  if (!vnet_sw_if_index_is_api_valid(__sw_if_index)) { \
119  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 \
125 do { \
126 bad_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); \
132  if (!vnet_sw_if_index_is_api_valid(__rx_sw_if_index)) { \
133  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 \
139 do { \
140 bad_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); \
146  if (!vnet_sw_if_index_is_api_valid(__tx_sw_if_index)) { \
147  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 \
153 do { \
154 bad_tx_sw_if_index: \
155  ; \
156 } while (0);
157 
158 #define pub_sub_handler(lca,UCA) \
159 static 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  \
193 reply: \
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) \
203 _(oam_events) \
204 _(bfd_events)
205 
206 /* WARNING: replicated in vpp/stats.h */
207 typedef struct
208 {
209  u32 client_index; /* in memclnt registration pool */
212 
213 struct _vl_api_ip4_arp_event;
214 struct _vl_api_ip6_nd_event;
215 
216 typedef struct
217 {
218 #define _(a) uword *a##_registration_hash; \
219  vpe_client_registration_t * a##_registrations;
221 #undef _
222  /* notifications happen really early in the game */
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 */
235 
237 
238 #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  */
vnet_main_t * vnet_main
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vlib_main_t * vlib_main
struct _vl_api_ip4_arp_event * arp_events
struct _vl_api_ip6_nd_event * nd_events
static uword vnet_sw_if_index_is_api_valid(u32 sw_if_index)
foreach_registration_hash u8 link_state_process_up
unsigned int u32
Definition: types.h:88
u64 uword
Definition: types.h:112
static uword vnet_sw_interface_is_api_valid(vnet_main_t *vnm, u32 sw_if_index)
unsigned char u8
Definition: types.h:56
vpe_api_main_t vpe_api_main
Definition: api.c:161
#define foreach_registration_hash