FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
punt_api.c
Go to the documentation of this file.
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 #include <vnet/ip/ip_types_api.h>
24 
25 #include <vnet/format_fns.h>
26 #include <vnet/ip/punt.api_enum.h>
27 #include <vnet/ip/punt.api_types.h>
28 
29 #define REPLY_MSG_ID_BASE punt_main.msg_id_base
31 
32 static int
33 vl_api_punt_type_decode (vl_api_punt_type_t in, punt_type_t * out)
34 {
35  in = clib_net_to_host_u32 (in);
36 
37  switch (in)
38  {
39 #define _(v, s) \
40  case PUNT_API_TYPE_##v: \
41  *out = PUNT_TYPE_##v; \
42  return (0);
44 #undef _
45  }
46 
47  return (-1);
48 }
49 
50 static vl_api_punt_type_t
52 {
53  vl_api_punt_type_t pt = PUNT_API_TYPE_L4;
54 
55  switch (in)
56  {
57 #define _(v, s) \
58  case PUNT_TYPE_##v: \
59  pt = PUNT_API_TYPE_##v; \
60  break;
62 #undef _
63  }
64 
65  return (clib_host_to_net_u32 (pt));
66 }
67 
68 static int
69 vl_api_punt_l4_decode (const vl_api_punt_l4_t * in, punt_l4_t * out)
70 {
71  int rv;
72 
73  rv = ip_address_family_decode (in->af, &out->af);
74  if (rv < 0)
75  return (rv);
76  rv = ip_proto_decode (in->protocol, &out->protocol);
77  if (rv < 0)
78  return (rv);
79  out->port = clib_net_to_host_u16 (in->port);
80 
81  return (rv);
82 }
83 
84 static int
85 vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
86  punt_ip_proto_t * out)
87 {
88  int rv;
89 
90  rv = ip_address_family_decode (in->af, &out->af);
91  if (rv < 0)
92  return (rv);
93  rv = ip_proto_decode (in->protocol, &out->protocol);
94 
95  return (rv);
96 }
97 
98 static int
99 vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
100  punt_exception_t * out)
101 {
102  int rv;
103 
104  out->reason = clib_net_to_host_u32 (in->id);
106 
107  return (rv);
108 }
109 
110 static int
111 vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
112 {
113  int rv;
114 
115  rv = vl_api_punt_type_decode (in->type, &out->type);
116 
117  if (rv)
118  return (rv);
119 
120  switch (out->type)
121  {
122  case PUNT_TYPE_L4:
123  return (vl_api_punt_l4_decode (&in->punt.l4, &out->punt.l4));
124  case PUNT_TYPE_EXCEPTION:
125  return (vl_api_punt_exception_decode (&in->punt.exception,
126  &out->punt.exception));
127  case PUNT_TYPE_IP_PROTO:
128  return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
129  &out->punt.ip_proto));
130  }
131 
132  return (-1);
133 }
134 
135 static void
136 vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
137 {
138  out->af = ip_address_family_encode (in->af);
139  out->protocol = ip_proto_encode (in->protocol);
140  out->port = clib_net_to_host_u16 (in->port);
141 }
142 
143 static void
145  vl_api_punt_ip_proto_t * out)
146 {
147  out->af = ip_address_family_encode (in->af);
148  out->protocol = ip_proto_encode (in->protocol);
149 }
150 
151 static void
153  vl_api_punt_exception_t * out)
154 {
155  out->id = clib_host_to_net_u32 (in->reason);
156 }
157 
158 static void
159 vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
160 {
161  out->type = vl_api_punt_type_encode (in->type);
162 
163  switch (in->type)
164  {
165  case PUNT_TYPE_L4:
166  vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
167  break;
168  case PUNT_TYPE_IP_PROTO:
169  vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
170  break;
171  case PUNT_TYPE_EXCEPTION:
173  &out->punt.exception);
174  break;
175  }
176 }
177 
178 static void
180 {
181  vl_api_set_punt_reply_t *rmp;
184  punt_reg_t pr;
185  int rv;
186 
187  rv = vl_api_punt_decode (&mp->punt, &pr);
188 
189  if (rv)
190  goto out;
191 
192  error = vnet_punt_add_del (vm, &pr, mp->is_add);
193  if (error)
194  {
195  rv = -1;
197  }
198 
199 out:
200  REPLY_MACRO (VL_API_SET_PUNT_REPLY);
201 }
202 
203 static void
205 {
209  punt_reg_t pr;
210  int rv;
211 
212  rv = vl_api_punt_decode (&mp->punt, &pr);
213 
214  if (rv)
215  return;
216 
218  &pr, (char *) mp->pathname);
219  if (error)
220  {
221  rv = -1;
223  }
224 
225  char *p = vnet_punt_get_server_pathname ();
226 
227  /* *INDENT-OFF* */
228  REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
229  ({
230  memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
231  }));
232  /* *INDENT-ON* */
233 }
234 
236 {
240 
241 static walk_rc_t
243 {
244  punt_socket_send_ctx_t *ctx = args;
246 
247  mp = vl_msg_api_alloc (sizeof (*mp));
248  if (!mp)
249  return (WALK_STOP);
250 
251  clib_memset (mp, 0, sizeof (*mp));
252  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_PUNT_SOCKET_DETAILS);
253  mp->context = ctx->context;
254  vl_api_punt_encode (&pc->reg, &mp->punt);
255  memcpy (mp->pathname, pc->caddr.sun_path, sizeof (pc->caddr.sun_path));
256 
257  vl_api_send_msg (ctx->reg, (u8 *) mp);
258 
259  return (WALK_CONTINUE);
260 }
261 
262 static void
264 {
266  punt_type_t pt;
267 
268  if (0 != vl_api_punt_type_decode (mp->type, &pt))
269  return;
270 
272  if (!reg)
273  return;
274 
276  .reg = reg,
277  .context = mp->context,
278  };
279 
281 }
282 
283 static void
285 {
286  vl_api_punt_socket_deregister_reply_t *rmp;
289  punt_reg_t pr;
290  int rv;
291 
292  rv = vl_api_punt_decode (&mp->punt, &pr);
293 
294  if (rv)
295  goto out;
296 
297  error = vnet_punt_socket_del (vm, &pr);
298  if (error)
299  {
300  rv = -1;
302  }
303 
304 out:
305  REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
306 }
307 
309 {
314 
315 static int
317 {
320 
321  if (ctx->name)
322  {
323  /* user requested a specific punt-reason */
324  if (vec_cmp (name, ctx->name))
325  /* not the reasonn we're lookgin for */
326  return 1;
327  }
328 
329  mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
330  if (!mp)
331  return (0);
332 
333  clib_memset (mp, 0, sizeof (*mp));
334  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_PUNT_REASON_DETAILS);
335 
336  mp->context = ctx->context;
337  mp->reason.id = clib_host_to_net_u32 (id);
338  vl_api_vec_to_api_string (name, &mp->reason.name);
339 
340  vl_api_send_msg (ctx->reg, (u8 *) mp);
341 
342  return (1);
343 }
344 
345 static void
347 {
349 
351  if (!reg)
352  return;
353 
355  .reg = reg,
356  .context = mp->context,
357  .name = vl_api_from_api_to_new_vec (mp, &mp->reason.name),
358  };
359 
361 
362  vec_free (ctx.name);
363 }
364 
365 #include <vnet/ip/punt.api.c>
366 
367 static clib_error_t *
369 {
370  /*
371  * Set up the (msg_name, crc, message-id) table
372  */
374 
375  return 0;
376 }
377 
379 
380 
381 /*
382  * fd.io coding-style-patch-verification: ON
383  *
384  * Local Variables:
385  * eval: (c-set-style "gnu")
386  * End:
387  */
vlib_punt_reason_t
enum vlib_punt_reason_t_ vlib_punt_reason_t
The 'syatem' defined punt reasons.
punt_api_hookup
static clib_error_t * punt_api_hookup(vlib_main_t *vm)
Definition: punt_api.c:368
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
vl_api_punt_exception_decode
static int vl_api_punt_exception_decode(const vl_api_punt_exception_t *in, punt_exception_t *out)
Definition: punt_api.c:99
punt_reason_dump_walk_ctx_t_::context
u32 context
Definition: punt_api.c:311
foreach_punt_type
@ foreach_punt_type
Definition: punt.h:67
api.h
ntohs
#define ntohs(x)
Definition: af_xdp.bpf.c:29
punt_client_t
Definition: punt.h:133
WALK_CONTINUE
@ WALK_CONTINUE
Definition: interface_funcs.h:174
vl_api_punt_socket_dump_t::type
vl_api_punt_type_t type
Definition: punt.api:118
REPLY_MSG_ID_BASE
#define REPLY_MSG_ID_BASE
Definition: punt_api.c:29
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
punt_reg_t_
A registration, by a client, to direct punted traffic to a given node.
Definition: punt.h:96
punt_socket_send_ctx_t
struct punt_socket_send_ctx_t_ punt_socket_send_ctx_t
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
vl_api_punt_reason_details_t::reason
vl_api_punt_reason_t reason
Definition: punt.api:154
name
string name[64]
Definition: fib.api:25
vl_api_punt_decode
static int vl_api_punt_decode(const vl_api_punt_t *in, punt_reg_t *out)
Definition: punt_api.c:111
vl_api_punt_socket_dump_t::context
u32 context
Definition: punt.api:117
punt_l4_t_::af
ip_address_family_t af
Definition: punt.h:73
vl_api_punt_socket_details_t::punt
vl_api_punt_t punt
Definition: punt.api:124
punt_ip_proto_t_::protocol
ip_protocol_t protocol
Definition: punt.h:81
vl_api_punt_socket_register_t
Punt traffic to the host via socket.
Definition: punt.api:99
vl_api_punt_socket_deregister_t
Definition: punt.api:128
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
punt_reason_walk
void punt_reason_walk(punt_reason_walk_cb_t cb, void *ctx)
Definition: punt.c:457
ip_proto_encode
vl_api_ip_proto_t ip_proto_encode(ip_protocol_t ipp)
Definition: ip_types_api.c:69
clib_error_report
#define clib_error_report(e)
Definition: error.h:113
vl_api_punt_socket_register_reply_t::pathname
string pathname[108]
Definition: punt.api:111
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(punt_api_hookup)
vnet_punt_get_server_pathname
char * vnet_punt_get_server_pathname(void)
Definition: punt.c:41
vec_cmp
#define vec_cmp(v1, v2)
Compare two vectors (only applicable to vectors of signed numbers).
Definition: vec.h:1033
error
Definition: cJSON.c:88
punt_ip_proto_t_
Definition: punt.h:78
vl_api_punt_exception_encode
static void vl_api_punt_exception_encode(const punt_exception_t *in, vl_api_punt_exception_t *out)
Definition: punt_api.c:152
vl_api_punt_socket_register_t_handler
static void vl_api_punt_socket_register_t_handler(vl_api_punt_socket_register_t *mp)
Definition: punt_api.c:204
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
vl_api_punt_reason_details_t
Definition: punt.api:151
vl_api_punt_type_encode
static vl_api_punt_type_t vl_api_punt_type_encode(punt_type_t in)
Definition: punt_api.c:51
ip_address_family_decode
int ip_address_family_decode(vl_api_address_family_t af, ip_address_family_t *out)
Conversion functions to/from (decode/encode) API types to VPP internal types.
Definition: ip_types_api.c:20
punt_reason_dump_walk_cb
static int punt_reason_dump_walk_cb(vlib_punt_reason_t id, const u8 *name, void *args)
Definition: punt_api.c:316
punt_union_t_::ip_proto
punt_ip_proto_t ip_proto
Definition: punt.h:93
punt_union_t_::exception
punt_exception_t exception
Definition: punt.h:91
punt_client_t::reg
punt_reg_t reg
Definition: punt.h:135
vl_api_punt_socket_register_t::pathname
string pathname[108]
Definition: punt.api:104
vl_api_registration_
An API client registration, only in vpp/vlib.
Definition: api_common.h:47
REPLY_MACRO
#define REPLY_MACRO(t)
Definition: api_helper_macros.h:30
vl_api_punt_reason_dump_t::reason
vl_api_punt_reason_t reason
Definition: punt.api:148
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: sr_mpls_api.c:174
vl_api_set_punt_t::is_add
bool is_add
Definition: punt.api:89
punt_ip_proto_t_::af
ip_address_family_t af
Definition: punt.h:80
punt_socket_send_ctx_t_::reg
vl_api_registration_t * reg
Definition: punt_api.c:237
punt_socket_send_ctx_t_::context
u32 context
Definition: punt_api.c:238
punt_client_walk
void punt_client_walk(punt_type_t pt, punt_client_walk_cb_t cb, void *ctx)
Definition: punt.c:691
vl_api_punt_reason_dump_t
Dump all or one of the exception punt reasons.
Definition: punt.api:144
vl_api_punt_socket_deregister_t_handler
static void vl_api_punt_socket_deregister_t_handler(vl_api_punt_socket_deregister_t *mp)
Definition: punt_api.c:284
punt_reason_dump_walk_ctx_t_::name
u8 * name
Definition: punt_api.c:312
vnet_punt_socket_del
clib_error_t * vnet_punt_socket_del(vlib_main_t *vm, const punt_reg_t *pr)
Definition: punt.c:329
vl_api_punt_socket_details_t::context
u32 context
Definition: punt.api:123
punt_exception_t_
Definition: punt.h:84
vl_api_punt_socket_dump_t
Definition: punt.api:114
vl_api_punt_reason_details_t::context
u32 context
Definition: punt.api:153
punt_l4_t_::port
u16 port
Definition: punt.h:75
vl_api_punt_socket_dump_t_handler
static void vl_api_punt_socket_dump_t_handler(vl_api_punt_socket_dump_t *mp)
Definition: punt_api.c:263
punt_reg_t_::punt
punt_union_t punt
Definition: punt.h:99
ip_address_family_encode
vl_api_address_family_t ip_address_family_encode(ip_address_family_t af)
Definition: ip_types_api.c:36
vl_api_punt_socket_send_details
static walk_rc_t vl_api_punt_socket_send_details(const punt_client_t *pc, void *args)
Definition: punt_api.c:242
punt_l4_t_
Definition: punt.h:71
vl_api_punt_l4_encode
static void vl_api_punt_l4_encode(const punt_l4_t *in, vl_api_punt_l4_t *out)
Definition: punt_api.c:136
vl_api_punt_ip_proto_encode
static void vl_api_punt_ip_proto_encode(const punt_ip_proto_t *in, vl_api_punt_ip_proto_t *out)
Definition: punt_api.c:144
vl_api_set_punt_t::punt
vl_api_punt_t punt
Definition: punt.api:90
vl_api_punt_socket_dump_t::client_index
u32 client_index
Definition: punt.api:116
vl_api_punt_reason_dump_t_handler
static void vl_api_punt_reason_dump_t_handler(vl_api_punt_reason_dump_t *mp)
Definition: punt_api.c:346
punt_client_t::caddr
struct sockaddr_un caddr
Definition: punt.h:136
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vl_api_punt_type_decode
static int vl_api_punt_type_decode(vl_api_punt_type_t in, punt_type_t *out)
Definition: punt_api.c:33
vl_api_punt_l4_decode
static int vl_api_punt_l4_decode(const vl_api_punt_l4_t *in, punt_l4_t *out)
Definition: punt_api.c:69
punt.h
Definitions for punt infrastructure.
vnet_punt_add_del
clib_error_t * vnet_punt_add_del(vlib_main_t *vm, const punt_reg_t *pr, bool is_add)
Definition: punt.c:435
vl_api_set_punt_t_handler
static void vl_api_set_punt_t_handler(vl_api_set_punt_t *mp)
Definition: punt_api.c:179
vl_api_vec_to_api_string
int vl_api_vec_to_api_string(const u8 *vec, vl_api_string_t *str)
Definition: api_shared.c:1175
vl_api_punt_reason_dump_t::context
u32 context
Definition: punt.api:147
format_fns.h
vl_api_punt_reason_dump_t::client_index
u32 client_index
Definition: punt.api:146
vl_api_punt_socket_deregister_t::punt
vl_api_punt_t punt
Definition: punt.api:131
punt_l4_t_::protocol
ip_protocol_t protocol
Definition: punt.h:74
u32
unsigned int u32
Definition: types.h:88
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
api_helper_macros.h
punt_exception_t_::reason
vlib_punt_reason_t reason
Definition: punt.h:86
vl_api_punt_socket_details_t
Definition: punt.api:121
punt_type_t
enum punt_type_t_ punt_type_t
vl_api_from_api_to_new_vec
u8 * vl_api_from_api_to_new_vec(void *mp, vl_api_string_t *astr)
Definition: api_shared.c:1202
vl_api_punt_socket_register_t::header_version
u32 header_version
Definition: punt.api:102
vnet_punt_socket_add
clib_error_t * vnet_punt_socket_add(vlib_main_t *vm, u32 header_version, const punt_reg_t *pr, char *client_pathname)
Definition: punt.c:289
vl_api_set_punt_t
Punt traffic to the host.
Definition: punt.api:86
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
punt_reason_dump_walk_ctx_t
struct punt_reason_dump_walk_ctx_t_ punt_reason_dump_walk_ctx_t
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
ip_proto_decode
int ip_proto_decode(vl_api_ip_proto_t ipp, ip_protocol_t *out)
Definition: ip_types_api.c:51
punt_socket_send_ctx_t_
Definition: punt_api.c:235
vl_api_punt_encode
static void vl_api_punt_encode(const punt_reg_t *in, vl_api_punt_t *out)
Definition: punt_api.c:159
punt_union_t_::l4
punt_l4_t l4
Definition: punt.h:92
rv
int __clib_unused rv
Definition: application.c:491
vlib_punt_reason_validate
int vlib_punt_reason_validate(vlib_punt_reason_t reason)
Validate that a punt reason is assigned.
Definition: punt.c:410
vl_api_punt_ip_proto_decode
static int vl_api_punt_ip_proto_decode(const vl_api_punt_ip_proto_t *in, punt_ip_proto_t *out)
Definition: punt_api.c:85
vnet.h
punt_reg_t_::type
punt_type_t type
Definition: punt.h:98
ip_types_api.h
walk_rc_t
enum walk_rc_t_ walk_rc_t
Walk return code.
vl_api_punt_socket_register_t::punt
vl_api_punt_t punt
Definition: punt.api:103
WALK_STOP
@ WALK_STOP
Definition: interface_funcs.h:173
PUNT_API_TYPE_L4
@ PUNT_API_TYPE_L4
Definition: punt.api:24
vl_api_punt_socket_register_reply_t
Definition: punt.api:107
vl_api_punt_socket_details_t::pathname
string pathname[108]
Definition: punt.api:125
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199
punt_reason_dump_walk_ctx_t_
Definition: punt_api.c:308
punt_reason_dump_walk_ctx_t_::reg
vl_api_registration_t * reg
Definition: punt_api.c:310