udp: fix csum computation when offload disabled
[vpp.git] / src / vnet / pg / pg_api.c
1 /*
2  *------------------------------------------------------------------
3  * pg_api.c - vnet pg 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/pg/pg.h>
23
24 #include <vnet/format_fns.h>
25 #include <vnet/pg/pg.api_enum.h>
26 #include <vnet/pg/pg.api_types.h>
27
28 #define REPLY_MSG_ID_BASE pg->msg_id_base
29 #include <vlibapi/api_helper_macros.h>
30
31 static void
32 vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp)
33 {
34   vl_api_pg_create_interface_reply_t *rmp;
35   int rv = 0;
36
37   pg_main_t *pg = &pg_main;
38   u32 pg_if_id =
39     pg_interface_add_or_get (pg, ntohl (mp->interface_id), mp->gso_enabled,
40                              ntohl (mp->gso_size), 0, PG_MODE_ETHERNET);
41   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
42
43   REPLY_MACRO2(VL_API_PG_CREATE_INTERFACE_REPLY,
44   ({
45     rmp->sw_if_index = ntohl(pi->sw_if_index);
46   }));
47 }
48
49 static void
50 vl_api_pg_create_interface_v2_t_handler (vl_api_pg_create_interface_v2_t *mp)
51 {
52   vl_api_pg_create_interface_v2_reply_t *rmp;
53   int rv = 0;
54
55   pg_main_t *pg = &pg_main;
56   u32 pg_if_id =
57     pg_interface_add_or_get (pg, ntohl (mp->interface_id), mp->gso_enabled,
58                              ntohl (mp->gso_size), 0, (u8) mp->mode);
59   pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id);
60
61   REPLY_MACRO2 (VL_API_PG_CREATE_INTERFACE_V2_REPLY,
62                 ({ rmp->sw_if_index = ntohl (pi->sw_if_index); }));
63 }
64
65 static void
66   vl_api_pg_interface_enable_disable_coalesce_t_handler
67   (vl_api_pg_interface_enable_disable_coalesce_t * mp)
68 {
69   vl_api_pg_interface_enable_disable_coalesce_reply_t *rmp;
70   int rv = 0;
71
72   VALIDATE_SW_IF_INDEX (mp);
73
74   u32 sw_if_index = ntohl (mp->sw_if_index);
75
76   pg_main_t *pg = &pg_main;
77   vnet_main_t *vnm = vnet_get_main ();
78   vnet_hw_interface_t *hw =
79     vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
80
81   if (hw)
82     {
83       pg_interface_t *pi =
84         pool_elt_at_index (pg->interfaces, hw->dev_instance);
85       if (pi->gso_enabled)
86         pg_interface_enable_disable_coalesce (pi, mp->coalesce_enabled,
87                                               hw->tx_node_index);
88       else
89         rv = VNET_API_ERROR_CANNOT_ENABLE_DISABLE_FEATURE;
90     }
91   else
92     {
93       rv = VNET_API_ERROR_NO_MATCHING_INTERFACE;
94     }
95
96   BAD_SW_IF_INDEX_LABEL;
97   REPLY_MACRO (VL_API_PG_INTERFACE_ENABLE_DISABLE_COALESCE_REPLY);
98 }
99
100 static void
101 vl_api_pg_capture_t_handler (vl_api_pg_capture_t * mp)
102 {
103   pg_main_t *pg = &pg_main;
104   vl_api_pg_capture_reply_t *rmp;
105   int rv = 0;
106
107   vnet_main_t *vnm = vnet_get_main ();
108   vnet_interface_main_t *im = &vnm->interface_main;
109   vnet_hw_interface_t *hi = 0;
110
111   u8 *intf_name = format (0, "pg%d", ntohl (mp->interface_id), 0);
112   vec_terminate_c_string (intf_name);
113   u32 hw_if_index = ~0;
114   uword *p = hash_get_mem (im->hw_interface_by_name, intf_name);
115   if (p)
116     hw_if_index = *p;
117   vec_free (intf_name);
118
119   if (hw_if_index != ~0)
120     {
121       pg_capture_args_t _a, *a = &_a;
122       char *pcap_file_name =
123         vl_api_from_api_to_new_c_string (&mp->pcap_file_name);
124
125       hi = vnet_get_sup_hw_interface (vnm, hw_if_index);
126       a->hw_if_index = hw_if_index;
127       a->dev_instance = hi->dev_instance;
128       a->is_enabled = mp->is_enabled;
129       a->pcap_file_name = pcap_file_name;
130       a->count = ntohl (mp->count);
131
132       clib_error_t *e = pg_capture (a);
133       if (e)
134         {
135           clib_error_report (e);
136           rv = VNET_API_ERROR_CANNOT_CREATE_PCAP_FILE;
137         }
138
139       vec_free (pcap_file_name);
140     }
141   REPLY_MACRO (VL_API_PG_CAPTURE_REPLY);
142 }
143
144 static void
145 vl_api_pg_enable_disable_t_handler (vl_api_pg_enable_disable_t * mp)
146 {
147   vl_api_pg_enable_disable_reply_t *rmp;
148   int rv = 0;
149
150   pg_main_t *pg = &pg_main;
151   u32 stream_index = ~0;
152
153   int is_enable = mp->is_enabled != 0;
154
155   if (vl_api_string_len (&mp->stream_name) > 0)
156     {
157       u8 *stream_name = vl_api_from_api_to_new_vec (mp, &mp->stream_name);
158       uword *p = hash_get_mem (pg->stream_index_by_name, stream_name);
159       if (p)
160         stream_index = *p;
161       vec_free (stream_name);
162     }
163
164   pg_enable_disable (stream_index, is_enable);
165
166   REPLY_MACRO (VL_API_PG_ENABLE_DISABLE_REPLY);
167 }
168
169 #include <vnet/pg/pg.api.c>
170 static clib_error_t *
171 pg_api_hookup (vlib_main_t * vm)
172 {
173   pg_main_t *pg = &pg_main;
174   /*
175    * Set up the (msg_name, crc, message-id) table
176    */
177   pg->msg_id_base = setup_message_id_table ();
178
179   return 0;
180 }
181
182 VLIB_API_INIT_FUNCTION (pg_api_hookup);
183
184 /*
185  * fd.io coding-style-patch-verification: ON
186  *
187  * Local Variables:
188  * eval: (c-set-style "gnu")
189  * End:
190  */