d4d041ffb3c086aa5adc997fb14b24537c9b5373
[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   vlib_main_t *vm = vlib_get_main ();
40   vl_api_af_packet_create_reply_t *rmp;
41   int rv = 0;
42   u8 *host_if_name = NULL;
43   u32 sw_if_index;
44
45   host_if_name = format (0, "%s", mp->host_if_name);
46   vec_add1 (host_if_name, 0);
47
48   rv = af_packet_create_if (vm, host_if_name,
49                             mp->use_random_hw_addr ? 0 : mp->hw_addr,
50                             &sw_if_index);
51
52   vec_free (host_if_name);
53
54   /* *INDENT-OFF* */
55   REPLY_MACRO2(VL_API_AF_PACKET_CREATE_REPLY,
56   ({
57     rmp->sw_if_index = clib_host_to_net_u32(sw_if_index);
58   }));
59   /* *INDENT-ON* */
60 }
61
62 static void
63 vl_api_af_packet_delete_t_handler (vl_api_af_packet_delete_t * mp)
64 {
65   vlib_main_t *vm = vlib_get_main ();
66   vl_api_af_packet_delete_reply_t *rmp;
67   int rv = 0;
68   u8 *host_if_name = NULL;
69
70   host_if_name = format (0, "%s", mp->host_if_name);
71   vec_add1 (host_if_name, 0);
72
73   rv = af_packet_delete_if (vm, host_if_name);
74
75   vec_free (host_if_name);
76
77   REPLY_MACRO (VL_API_AF_PACKET_DELETE_REPLY);
78 }
79
80 static void
81   vl_api_af_packet_set_l4_cksum_offload_t_handler
82   (vl_api_af_packet_set_l4_cksum_offload_t * mp)
83 {
84   vlib_main_t *vm = vlib_get_main ();
85   vl_api_af_packet_delete_reply_t *rmp;
86   int rv = 0;
87
88   rv = af_packet_set_l4_cksum_offload (vm, ntohl (mp->sw_if_index), mp->set);
89   REPLY_MACRO (VL_API_AF_PACKET_SET_L4_CKSUM_OFFLOAD_REPLY);
90 }
91
92 static void
93 af_packet_send_details (vpe_api_main_t * am,
94                         vl_api_registration_t * reg,
95                         af_packet_if_detail_t * af_packet_if, u32 context)
96 {
97   vl_api_af_packet_details_t *mp;
98   mp = vl_msg_api_alloc (sizeof (*mp));
99   clib_memset (mp, 0, sizeof (*mp));
100   mp->_vl_msg_id = htons (REPLY_MSG_ID_BASE + VL_API_AF_PACKET_DETAILS);
101   mp->sw_if_index = htonl (af_packet_if->sw_if_index);
102   clib_memcpy (mp->host_if_name, af_packet_if->host_if_name,
103                MIN (ARRAY_LEN (mp->host_if_name) - 1,
104                     strlen ((const char *) af_packet_if->host_if_name)));
105
106   mp->context = context;
107   vl_api_send_msg (reg, (u8 *) mp);
108 }
109
110
111 static void
112 vl_api_af_packet_dump_t_handler (vl_api_af_packet_dump_t * mp)
113 {
114   int rv;
115   vpe_api_main_t *am = &vpe_api_main;
116   vl_api_registration_t *reg;
117   af_packet_if_detail_t *out_af_packet_ifs = NULL;
118   af_packet_if_detail_t *af_packet_if = NULL;
119
120   reg = vl_api_client_index_to_registration (mp->client_index);
121   if (!reg)
122     return;
123
124   rv = af_packet_dump_ifs (&out_af_packet_ifs);
125   if (rv)
126     return;
127
128   vec_foreach (af_packet_if, out_af_packet_ifs)
129   {
130     af_packet_send_details (am, reg, af_packet_if, mp->context);
131   }
132
133   vec_free (out_af_packet_ifs);
134 }
135
136 #include <vnet/devices/af_packet/af_packet.api.c>
137 static clib_error_t *
138 af_packet_api_hookup (vlib_main_t * vm)
139 {
140   /*
141    * Set up the (msg_name, crc, message-id) table
142    */
143   REPLY_MSG_ID_BASE = setup_message_id_table ();
144
145   return 0;
146 }
147
148 VLIB_API_INIT_FUNCTION (af_packet_api_hookup);
149
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "gnu")
155  * End:
156  */