FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
af_packet_api.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * af_packet_api.c - af-packet 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 
23 #include <vnet/interface.h>
24 #include <vnet/api_errno.h>
26 
27 #include <vnet/vnet_msg_enum.h>
28 
29 #define vl_typedefs /* define message structures */
30 #include <vnet/vnet_all_api_h.h>
31 #undef vl_typedefs
32 
33 #define vl_endianfun /* define message structures */
34 #include <vnet/vnet_all_api_h.h>
35 #undef vl_endianfun
36 
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #define vl_printfun
40 #include <vnet/vnet_all_api_h.h>
41 #undef vl_printfun
42 
44 
45 #define foreach_vpe_api_msg \
46 _(AF_PACKET_CREATE, af_packet_create) \
47 _(AF_PACKET_DELETE, af_packet_delete) \
48 _(AF_PACKET_SET_L4_CKSUM_OFFLOAD, af_packet_set_l4_cksum_offload) \
49 _(AF_PACKET_DUMP, af_packet_dump)
50 
51 static void
53 {
56  int rv = 0;
57  u8 *host_if_name = NULL;
59 
60  host_if_name = format (0, "%s", mp->host_if_name);
61  vec_add1 (host_if_name, 0);
62 
63  rv = af_packet_create_if (vm, host_if_name,
64  mp->use_random_hw_addr ? 0 : mp->hw_addr,
65  &sw_if_index);
66 
67  vec_free (host_if_name);
68 
69  /* *INDENT-OFF* */
70  REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
71  ({
72  rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
73  }));
74  /* *INDENT-ON* */
75 }
76 
77 static void
79 {
81  vl_api_af_packet_delete_reply_t *rmp;
82  int rv = 0;
83  u8 *host_if_name = NULL;
84 
85  host_if_name = format (0, "%s", mp->host_if_name);
86  vec_add1 (host_if_name, 0);
87 
88  rv = af_packet_delete_if (vm, host_if_name);
89 
90  vec_free (host_if_name);
91 
92  REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
93 }
94 
95 static void
98 {
100  vl_api_af_packet_delete_reply_t *rmp;
101  int rv = 0;
102 
103  rv = af_packet_set_l4_cksum_offload (vm, ntohl (mp->sw_if_index), mp->set);
104  REPLY_MACRO (VL_API_AF_PACKET_SET_L4_CKSUM_OFFLOAD_REPLY);
105 }
106 
107 static void
109  vl_api_registration_t * reg,
110  af_packet_if_detail_t * af_packet_if, u32 context)
111 {
113  mp = vl_msg_api_alloc (sizeof (*mp));
114  clib_memset (mp, 0, sizeof (*mp));
115  mp->_vl_msg_id = htons (VL_API_AF_PACKET_DETAILS);
116  mp->sw_if_index = htonl (af_packet_if->sw_if_index);
117  clib_memcpy (mp->host_if_name, af_packet_if->host_if_name,
118  MIN (ARRAY_LEN (mp->host_if_name) - 1,
119  strlen ((const char *) af_packet_if->host_if_name)));
120 
121  mp->context = context;
122  vl_api_send_msg (reg, (u8 *) mp);
123 }
124 
125 
126 static void
128 {
129  int rv;
132  af_packet_if_detail_t *out_af_packet_ifs = NULL;
133  af_packet_if_detail_t *af_packet_if = NULL;
134 
136  if (!reg)
137  return;
138 
139  rv = af_packet_dump_ifs (&out_af_packet_ifs);
140  if (rv)
141  return;
142 
143  vec_foreach (af_packet_if, out_af_packet_ifs)
144  {
145  af_packet_send_details (am, reg, af_packet_if, mp->context);
146  }
147 
148  vec_free (out_af_packet_ifs);
149 }
150 
151 /*
152  * af_packet_api_hookup
153  * Add vpe's API message handlers to the table.
154  * vlib has already mapped shared memory and
155  * added the client registration handlers.
156  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
157  */
158 #define vl_msg_name_crc_list
159 #include <vnet/vnet_all_api_h.h>
160 #undef vl_msg_name_crc_list
161 
162 static void
164 {
165 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
166  foreach_vl_msg_name_crc_af_packet;
167 #undef _
168 }
169 
170 static clib_error_t *
172 {
174 
175 #define _(N,n) \
176  vl_msg_api_set_handlers(VL_API_##N, #n, \
177  vl_api_##n##_t_handler, \
178  vl_noop_handler, \
179  vl_api_##n##_t_endian, \
180  vl_api_##n##_t_print, \
181  sizeof(vl_api_##n##_t), 1);
183 #undef _
184 
185  /*
186  * Set up the (msg_name, crc, message-id) table
187  */
189 
190  return 0;
191 }
192 
194 
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */
vl_api_af_packet_delete_t::host_if_name
string host_if_name[64]
Definition: af_packet.api:59
vl_api_client_index_to_registration
static vl_api_registration_t * vl_api_client_index_to_registration(u32 index)
Definition: api.h:79
api.h
vl_api_af_packet_set_l4_cksum_offload_t_handler
static void vl_api_af_packet_set_l4_cksum_offload_t_handler(vl_api_af_packet_set_l4_cksum_offload_t *mp)
Definition: af_packet_api.c:97
REPLY_MACRO2
#define REPLY_MACRO2(t, body)
Definition: api_helper_macros.h:65
clib_memcpy
#define clib_memcpy(d, s, n)
Definition: string.h:197
vl_api_send_msg
static void vl_api_send_msg(vl_api_registration_t *rp, u8 *elem)
Definition: api.h:35
af_packet_if_detail_t::host_if_name
u8 host_if_name[64]
Definition: af_packet.h:27
vpe_api_main_t
Definition: api_helper_macros.h:414
setup_message_id_table
static void setup_message_id_table(api_main_t *am)
Definition: af_packet_api.c:163
af_packet_api_hookup
static clib_error_t * af_packet_api_hookup(vlib_main_t *vm)
Definition: af_packet_api.c:171
am
app_main_t * am
Definition: application.c:489
af_packet_create_if
int af_packet_create_if(vlib_main_t *vm, u8 *host_if_name, u8 *hw_addr_set, u32 *sw_if_index)
Definition: af_packet.c:235
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
vl_api_af_packet_set_l4_cksum_offload_t
Set l4 offload checksum calculation.
Definition: af_packet.api:66
vl_api_af_packet_create_t::use_random_hw_addr
bool use_random_hw_addr
Definition: af_packet.api:34
vl_api_af_packet_dump_t::client_index
u32 client_index
Definition: af_packet.api:78
vl_api_af_packet_delete_t
Delete host-interface.
Definition: af_packet.api:54
vl_api_af_packet_delete_t_handler
static void vl_api_af_packet_delete_t_handler(vl_api_af_packet_delete_t *mp)
Definition: af_packet_api.c:78
vl_api_af_packet_details_t
Reply for af_packet dump request.
Definition: af_packet.api:86
vl_api_af_packet_create_t::hw_addr
vl_api_mac_address_t hw_addr
Definition: af_packet.api:33
vl_api_af_packet_create_reply_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: af_packet.api:46
af_packet_send_details
static void af_packet_send_details(vpe_api_main_t *am, vl_api_registration_t *reg, af_packet_if_detail_t *af_packet_if, u32 context)
Definition: af_packet_api.c:108
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
af_packet_delete_if
int af_packet_delete_if(vlib_main_t *vm, u8 *host_if_name)
Definition: af_packet.c:441
foreach_vpe_api_msg
#define foreach_vpe_api_msg
Definition: af_packet_api.c:45
vnet_msg_enum.h
vl_api_af_packet_dump_t::context
u32 context
Definition: af_packet.api:79
vl_api_af_packet_details_t::host_if_name
string host_if_name[64]
Definition: af_packet.api:90
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
MIN
#define MIN(x, y)
Definition: node.h:31
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vl_api_af_packet_dump_t_handler
static void vl_api_af_packet_dump_t_handler(vl_api_af_packet_dump_t *mp)
Definition: af_packet_api.c:127
vlibapi_get_main
static api_main_t * vlibapi_get_main(void)
Definition: api_common.h:390
vl_api_af_packet_details_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: af_packet.api:89
interface.h
VLIB_API_INIT_FUNCTION
VLIB_API_INIT_FUNCTION(af_packet_api_hookup)
vl_api_af_packet_set_l4_cksum_offload_t::sw_if_index
vl_api_interface_index_t sw_if_index
Definition: af_packet.api:71
af_packet_set_l4_cksum_offload
int af_packet_set_l4_cksum_offload(vlib_main_t *vm, u32 sw_if_index, u8 set)
Definition: af_packet.c:501
vl_api_af_packet_create_t::host_if_name
string host_if_name[64]
Definition: af_packet.api:35
vl_api_af_packet_set_l4_cksum_offload_t::set
bool set
Definition: af_packet.api:72
api_main_t
API main structure, used by both vpp and binary API clients.
Definition: api_common.h:228
af_packet_dump_ifs
int af_packet_dump_ifs(af_packet_if_detail_t **out_af_packet_ifs)
Definition: af_packet.c:525
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
vl_api_af_packet_details_t::context
u32 context
Definition: af_packet.api:88
vl_api_af_packet_create_t
Create host-interface.
Definition: af_packet.api:28
format
description fragment has unexpected format
Definition: map.api:433
u32
unsigned int u32
Definition: types.h:88
af_packet_if_detail_t
Definition: af_packet.h:24
api_helper_macros.h
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
af_packet.h
vl_api_af_packet_create_t_handler
static void vl_api_af_packet_create_t_handler(vl_api_af_packet_create_t *mp)
Definition: af_packet_api.c:52
vl_api_af_packet_dump_t
Dump af_packet interfaces request.
Definition: af_packet.api:76
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
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
vl_api_af_packet_create_reply_t
Create host-interface response.
Definition: af_packet.api:42
vnet_all_api_h.h
context
u32 context
Definition: ip.api:780
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
api_errno.h
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
vpe_api_main
vpe_api_main_t vpe_api_main
Definition: interface_api.c:55
vl_msg_api_alloc
void * vl_msg_api_alloc(int nbytes)
Definition: memory_shared.c:199
af_packet_if_detail_t::sw_if_index
u32 sw_if_index
Definition: af_packet.h:26