d991a2e685251d3247ad50b59a21f14e07934d42
[vpp.git] / src / vnet / devices / af_packet / af_packet_api.c
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>
25 #include <vnet/devices/af_packet/af_packet.h>
26
27 #include <vnet/format_fns.h>
28 #include <vnet/devices/af_packet/af_packet.api_enum.h>
29 #include <vnet/devices/af_packet/af_packet.api_types.h>
30
31 #define REPLY_MSG_ID_BASE msg_id_base
32 #include <vlibapi/api_helper_macros.h>
33
34 static u16 msg_id_base;
35
36 static void
37 vl_api_af_packet_create_t_handler (vl_api_af_packet_create_t * mp)
38 {
39   af_packet_create_if_arg_t _arg, *arg = &_arg;
40   vl_api_af_packet_create_reply_t *rmp;
41   int rv = 0;
42
43   clib_memset (arg, 0, sizeof (*arg));
44
45   arg->host_if_name = format (0, "%s", mp->host_if_name);
46   vec_add1 (arg->host_if_name, 0);
47
48   arg->hw_addr = mp->use_random_hw_addr ? 0 : mp->hw_addr;
49   arg->mode = AF_PACKET_IF_MODE_ETHERNET;
50   // Default flags
51   arg->flags = AF_PACKET_IF_FLAGS_QDISC_BYPASS | AF_PACKET_IF_FLAGS_CKSUM_GSO;
52   rv = af_packet_create_if (arg);
53
54   vec_free (arg->host_if_name);
55
56   REPLY_MACRO2 (VL_API_AF_PACKET_CREATE_REPLY, ({
57                   rmp->sw_if_index = clib_host_to_net_u32 (arg->sw_if_index);
58                 }));
59 }
60
61 static void
62 vl_api_af_packet_create_v2_t_handler (vl_api_af_packet_create_v2_t *mp)
63 {
64   af_packet_create_if_arg_t _arg, *arg = &_arg;
65   vl_api_af_packet_create_v2_reply_t *rmp;
66   int rv = 0;
67
68   clib_memset (arg, 0, sizeof (*arg));
69
70   arg->host_if_name = format (0, "%s", mp->host_if_name);
71   vec_add1 (arg->host_if_name, 0);
72
73   // Default number of rx/tx queue(s)
74   arg->num_rxqs = 1;
75   arg->num_txqs = 1;
76   arg->rx_frame_size = clib_net_to_host_u32 (mp->rx_frame_size);
77   arg->tx_frame_size = clib_net_to_host_u32 (mp->tx_frame_size);
78   arg->rx_frames_per_block = clib_net_to_host_u32 (mp->rx_frames_per_block);
79   arg->tx_frames_per_block = clib_net_to_host_u32 (mp->tx_frames_per_block);
80   arg->hw_addr = mp->use_random_hw_addr ? 0 : mp->hw_addr;
81   arg->mode = AF_PACKET_IF_MODE_ETHERNET;
82   // Default flags
83   arg->flags = AF_PACKET_IF_FLAGS_QDISC_BYPASS | AF_PACKET_IF_FLAGS_CKSUM_GSO;
84
85   if (mp->num_rx_queues > 1)
86     arg->num_rxqs = clib_net_to_host_u16 (mp->num_rx_queues);
87
88   rv = af_packet_create_if (arg);
89
90   vec_free (arg->host_if_name);
91   REPLY_MACRO2 (VL_API_AF_PACKET_CREATE_V2_REPLY, ({
92                   rmp->sw_if_index = clib_host_to_net_u32 (arg->sw_if_index);
93                 }));
94 }
95
96 static void
97 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
98 {
99   vl_api_af_packet_delete_reply_t *rmp;
100   int rv = 0;
101   u8 *host_if_name = NULL;
102
103   host_if_name = format (0, "%s", mp->host_if_name);
104   vec_add1 (host_if_name, 0);
105
106   rv = af_packet_delete_if (host_if_name);
107
108   vec_free (host_if_name);
109
110   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
111 }
112
113 static void
114   vl_api_af_packet_set_l4_cksum_offload_t_handler
115   (vl_api_af_packet_set_l4_cksum_offload_t * mp)
116 {
117   vl_api_af_packet_delete_reply_t *rmp;
118   int rv = 0;
119
120   rv = af_packet_set_l4_cksum_offload (ntohl (mp->sw_if_index), mp->set);
121   REPLY_MACRO (VL_API_AF_PACKET_SET_L4_CKSUM_OFFLOAD_REPLY);
122 }
123
124 static void
125 af_packet_send_details (vpe_api_main_t * am,
126                         vl_api_registration_t * reg,
127                         af_packet_if_detail_t * af_packet_if, u32 context)
128 {
129   vl_api_af_packet_details_t *mp;
130   mp = vl_msg_api_alloc (sizeof (*mp));
131   clib_memset (mp, 0, sizeof (*mp));
132   mp->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_AF_PACKET_DETAILS);
133   mp->sw_if_index = htonl (af_packet_if->sw_if_index);
134   clib_memcpy (mp->host_if_name, af_packet_if->host_if_name,
135                MIN (ARRAY_LEN (mp->host_if_name) - 1,
136                     strlen ((const char *) af_packet_if->host_if_name)));
137
138   mp->context = context;
139   vl_api_send_msg (reg, (u8 *) mp);
140 }
141
142
143 static void
144 vl_api_af_packet_dump_t_handler (vl_api_af_packet_dump_t * mp)
145 {
146   int rv;
147   vpe_api_main_t *am = &vpe_api_main;
148   vl_api_registration_t *reg;
149   af_packet_if_detail_t *out_af_packet_ifs = NULL;
150   af_packet_if_detail_t *af_packet_if = NULL;
151
152   reg = vl_api_client_index_to_registration (mp->client_index);
153   if (!reg)
154     return;
155
156   rv = af_packet_dump_ifs (&out_af_packet_ifs);
157   if (rv)
158     return;
159
160   vec_foreach (af_packet_if, out_af_packet_ifs)
161   {
162     af_packet_send_details (am, reg, af_packet_if, mp->context);
163   }
164
165   vec_free (out_af_packet_ifs);
166 }
167
168 #include <vnet/devices/af_packet/af_packet.api.c>
169 static clib_error_t *
170 af_packet_api_hookup (vlib_main_t * vm)
171 {
172   /*
173    * Set up the (msg_name, crc, message-id) table
174    */
175   REPLY_MSG_ID_BASE = setup_message_id_table ();
176
177   return 0;
178 }
179
180 VLIB_API_INIT_FUNCTION (af_packet_api_hookup);
181
182 /*
183  * fd.io coding-style-patch-verification: ON
184  *
185  * Local Variables:
186  * eval: (c-set-style "gnu")
187  * End:
188  */