api: set interface MTU API (VPP-442)
[vpp.git] / vnet / vnet / interface_api.c
1 /*
2  *------------------------------------------------------------------
3  * interface_api.c - vnet interface 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/ethernet/ethernet.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
43 #include <vlibapi/api_helper_macros.h>
44
45 #define foreach_vpe_api_msg                             \
46 _(SW_INTERFACE_SET_FLAGS, sw_interface_set_flags)       \
47 _(SW_INTERFACE_SET_MTU, sw_interface_set_mtu)
48
49 static void
50 vl_api_sw_interface_set_flags_t_handler (vl_api_sw_interface_set_flags_t * mp)
51 {
52   vl_api_sw_interface_set_flags_reply_t *rmp;
53   vnet_main_t *vnm = vnet_get_main ();
54   int rv = 0;
55   clib_error_t *error;
56   u16 flags;
57
58   VALIDATE_SW_IF_INDEX (mp);
59
60   flags = mp->admin_up_down ? VNET_SW_INTERFACE_FLAG_ADMIN_UP : 0;
61
62   error = vnet_sw_interface_set_flags (vnm, ntohl (mp->sw_if_index), flags);
63   if (error)
64     {
65       rv = -1;
66       clib_error_report (error);
67     }
68
69   BAD_SW_IF_INDEX_LABEL;
70   REPLY_MACRO (VL_API_SW_INTERFACE_SET_FLAGS_REPLY);
71 }
72
73 static void
74 vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
75 {
76   vl_api_sw_interface_set_mtu_reply_t *rmp;
77   vnet_main_t *vnm = vnet_get_main ();
78   u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
79   u32 sw_if_index = ntohl (mp->sw_if_index);
80   u16 mtu = ntohs (mp->mtu);
81   ethernet_main_t *em = &ethernet_main;
82   int rv = 0;
83
84   VALIDATE_SW_IF_INDEX (mp);
85
86   vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, sw_if_index);
87   ethernet_interface_t *eif = ethernet_get_interface (em, sw_if_index);
88
89   if (!eif)
90     {
91       rv = VNET_API_ERROR_FEATURE_DISABLED;
92       goto bad_sw_if_index;
93     }
94
95   if (mtu < hi->min_supported_packet_bytes)
96     {
97       rv = VNET_API_ERROR_INVALID_VALUE;
98       goto bad_sw_if_index;
99     }
100
101   if (mtu > hi->max_supported_packet_bytes)
102     {
103       rv = VNET_API_ERROR_INVALID_VALUE;
104       goto bad_sw_if_index;
105     }
106
107   if (hi->max_packet_bytes != mtu)
108     {
109       hi->max_packet_bytes = mtu;
110       ethernet_set_flags (vnm, sw_if_index, flags);
111     }
112
113   BAD_SW_IF_INDEX_LABEL;
114   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
115 }
116
117
118 /*
119  * vpe_api_hookup
120  * Add vpe's API message handlers to the table.
121  * vlib has alread mapped shared memory and
122  * added the client registration handlers.
123  * See .../vlib-api/vlibmemory/memclnt_vlib.c:memclnt_process()
124  */
125 #define vl_msg_name_crc_list
126 #include <vnet/vnet_all_api_h.h>
127 #undef vl_msg_name_crc_list
128
129 static void
130 setup_message_id_table (api_main_t * am)
131 {
132 #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id);
133   foreach_vl_msg_name_crc_interface;
134 #undef _
135 }
136
137 static clib_error_t *
138 interface_api_hookup (vlib_main_t * vm)
139 {
140   api_main_t *am = &api_main;
141
142 #define _(N,n)                                                  \
143     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
144                            vl_api_##n##_t_handler,              \
145                            vl_noop_handler,                     \
146                            vl_api_##n##_t_endian,               \
147                            vl_api_##n##_t_print,                \
148                            sizeof(vl_api_##n##_t), 1);
149   foreach_vpe_api_msg;
150 #undef _
151
152   /*
153    * Set up the (msg_name, crc, message-id) table
154    */
155   setup_message_id_table (am);
156
157   return 0;
158 }
159
160 VLIB_API_INIT_FUNCTION (interface_api_hookup);
161
162 /*
163  * fd.io coding-style-patch-verification: ON
164  *
165  * Local Variables:
166  * eval: (c-set-style "gnu")
167  * End:
168  */