ip: Fix crash in ip address add on sub-int without exact-match
[vpp.git] / src / vnet / interface_funcs.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * interface_funcs.h: VNET interfaces/sub-interfaces exported functions
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_vnet_interface_funcs_h
41 #define included_vnet_interface_funcs_h
42
43 always_inline vnet_hw_interface_t *
44 vnet_get_hw_interface (vnet_main_t * vnm, u32 hw_if_index)
45 {
46   return pool_elt_at_index (vnm->interface_main.hw_interfaces, hw_if_index);
47 }
48
49 always_inline vnet_hw_interface_t *
50 vnet_get_hw_interface_or_null (vnet_main_t * vnm, u32 hw_if_index)
51 {
52   if (!pool_is_free_index (vnm->interface_main.hw_interfaces, hw_if_index))
53     return pool_elt_at_index (vnm->interface_main.hw_interfaces, hw_if_index);
54   return (NULL);
55 }
56
57 always_inline vnet_sw_interface_t *
58 vnet_get_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
59 {
60   return pool_elt_at_index (vnm->interface_main.sw_interfaces, sw_if_index);
61 }
62
63 always_inline vnet_sw_interface_t *
64 vnet_get_sw_interface_or_null (vnet_main_t * vnm, u32 sw_if_index)
65 {
66   if (!pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index))
67     return pool_elt_at_index (vnm->interface_main.sw_interfaces, sw_if_index);
68   return (NULL);
69 }
70
71 always_inline vnet_sw_interface_t *
72 vnet_get_hw_sw_interface (vnet_main_t * vnm, u32 hw_if_index)
73 {
74   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
75   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, hw->sw_if_index);
76   ASSERT (sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE);
77   return sw;
78 }
79
80 always_inline vnet_sw_interface_t *
81 vnet_get_sup_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
82 {
83   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
84   if (sw->type == VNET_SW_INTERFACE_TYPE_SUB ||
85       sw->type == VNET_SW_INTERFACE_TYPE_PIPE ||
86       sw->type == VNET_SW_INTERFACE_TYPE_P2P)
87     sw = vnet_get_sw_interface (vnm, sw->sup_sw_if_index);
88   return sw;
89 }
90
91 always_inline vnet_hw_interface_t *
92 vnet_get_sup_hw_interface (vnet_main_t * vnm, u32 sw_if_index)
93 {
94   vnet_sw_interface_t *sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
95   ASSERT ((sw->type == VNET_SW_INTERFACE_TYPE_HARDWARE) ||
96           (sw->type == VNET_SW_INTERFACE_TYPE_PIPE));
97   return vnet_get_hw_interface (vnm, sw->hw_if_index);
98 }
99
100 always_inline vnet_hw_interface_t *
101 vnet_get_sup_hw_interface_api_visible_or_null (vnet_main_t * vnm,
102                                                u32 sw_if_index)
103 {
104   vnet_sw_interface_t *si;
105   if (PREDICT_FALSE (pool_is_free_index (vnm->interface_main.sw_interfaces,
106                                          sw_if_index)))
107     return NULL;
108   si = vnet_get_sup_sw_interface (vnm, sw_if_index);
109   if (PREDICT_FALSE (si->flags & VNET_SW_INTERFACE_FLAG_HIDDEN))
110     return NULL;
111   ASSERT ((si->type == VNET_SW_INTERFACE_TYPE_HARDWARE) ||
112           (si->type == VNET_SW_INTERFACE_TYPE_PIPE));
113   return vnet_get_hw_interface (vnm, si->hw_if_index);
114 }
115
116 always_inline vnet_hw_interface_class_t *
117 vnet_get_hw_interface_class (vnet_main_t * vnm, u32 hw_class_index)
118 {
119   return vec_elt_at_index (vnm->interface_main.hw_interface_classes,
120                            hw_class_index);
121 }
122
123 always_inline vnet_device_class_t *
124 vnet_get_device_class (vnet_main_t * vnm, u32 dev_class_index)
125 {
126   return vec_elt_at_index (vnm->interface_main.device_classes,
127                            dev_class_index);
128 }
129
130 static inline u8 *
131 vnet_get_sw_interface_tag (vnet_main_t * vnm, u32 sw_if_index)
132 {
133   uword *p;
134   p = hash_get (vnm->interface_tag_by_sw_if_index, sw_if_index);
135   if (p)
136     return ((u8 *) p[0]);
137   return 0;
138 }
139
140 static inline void
141 vnet_set_sw_interface_tag (vnet_main_t * vnm, u8 * tag, u32 sw_if_index)
142 {
143   uword *p;
144   p = hash_get (vnm->interface_tag_by_sw_if_index, sw_if_index);
145   if (p)
146     {
147       u8 *oldtag = (u8 *) p[0];
148       hash_unset (vnm->interface_tag_by_sw_if_index, sw_if_index);
149       vec_free (oldtag);
150     }
151
152   hash_set (vnm->interface_tag_by_sw_if_index, sw_if_index, tag);
153 }
154
155 static inline void
156 vnet_clear_sw_interface_tag (vnet_main_t * vnm, u32 sw_if_index)
157 {
158   uword *p;
159   p = hash_get (vnm->interface_tag_by_sw_if_index, sw_if_index);
160   if (p)
161     {
162       u8 *oldtag = (u8 *) p[0];
163       hash_unset (vnm->interface_tag_by_sw_if_index, sw_if_index);
164       vec_free (oldtag);
165     }
166 }
167
168 /**
169  * Walk return code
170  */
171 typedef enum walk_rc_t_
172 {
173   WALK_STOP,
174   WALK_CONTINUE,
175 } walk_rc_t;
176
177 /**
178  * Call back walk type for walking SW indices on a HW interface
179  */
180 typedef walk_rc_t (*vnet_hw_sw_interface_walk_t) (vnet_main_t * vnm,
181                                                   u32 sw_if_index, void *ctx);
182
183 /**
184  * @brief
185  * Walk the SW interfaces on a HW interface - this is the super
186  * interface and any sub-interfaces.
187  */
188 void vnet_hw_interface_walk_sw (vnet_main_t * vnm,
189                                 u32 hw_if_index,
190                                 vnet_hw_sw_interface_walk_t fn, void *ctx);
191
192 /**
193  * Call back walk type for walking SW indices on a HW interface
194  */
195 typedef walk_rc_t (*vnet_sw_interface_walk_t) (vnet_main_t * vnm,
196                                                vnet_sw_interface_t * si,
197                                                void *ctx);
198
199 /**
200  * @brief
201  * Walk all the SW interfaces in the system.
202  */
203 void vnet_sw_interface_walk (vnet_main_t * vnm,
204                              vnet_sw_interface_walk_t fn, void *ctx);
205
206
207 /**
208  * Call back walk type for walking all HW indices
209  */
210 typedef walk_rc_t (*vnet_hw_interface_walk_t) (vnet_main_t * vnm,
211                                                u32 hw_if_index, void *ctx);
212
213 /**
214  * @brief
215  * Walk all the HW interface
216  */
217 void vnet_hw_interface_walk (vnet_main_t * vnm,
218                              vnet_hw_interface_walk_t fn, void *ctx);
219
220 /* Register a hardware interface instance. */
221 u32 vnet_register_interface (vnet_main_t * vnm,
222                              u32 dev_class_index,
223                              u32 dev_instance,
224                              u32 hw_class_index, u32 hw_instance);
225
226 /**
227  * Set interface output node - for interface registered without its output/tx
228  * nodes created because its VNET_DEVICE_CLASS did not specify any tx_function.
229  * This is typically the case for tunnel interfaces.
230  */
231 void vnet_set_interface_output_node (vnet_main_t * vnm,
232                                      u32 hw_if_index, u32 node_index);
233
234 /* Creates a software interface given template. */
235 clib_error_t *vnet_create_sw_interface (vnet_main_t * vnm,
236                                         vnet_sw_interface_t * template,
237                                         u32 * sw_if_index);
238
239 void vnet_delete_hw_interface (vnet_main_t * vnm, u32 hw_if_index);
240 void vnet_delete_sw_interface (vnet_main_t * vnm, u32 sw_if_index);
241 int vnet_sw_interface_is_p2p (vnet_main_t * vnm, u32 sw_if_index);
242 int vnet_sw_interface_is_nbma (vnet_main_t * vnm, u32 sw_if_index);
243
244 always_inline vnet_sw_interface_flags_t
245 vnet_sw_interface_get_flags (vnet_main_t * vnm, u32 sw_if_index)
246 {
247   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
248   return sw->flags;
249 }
250
251 always_inline uword
252 vnet_sw_interface_is_valid (vnet_main_t * vnm, u32 sw_if_index)
253 {
254   return !pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index);
255 }
256
257 always_inline uword
258 vnet_hw_interface_is_valid (vnet_main_t * vnm, u32 hw_if_index)
259 {
260   return !pool_is_free_index (vnm->interface_main.hw_interfaces, hw_if_index);
261 }
262
263
264 always_inline uword
265 vnet_sw_interface_is_admin_up (vnet_main_t * vnm, u32 sw_if_index)
266 {
267   return (vnet_sw_interface_get_flags (vnm, sw_if_index) &
268           VNET_SW_INTERFACE_FLAG_ADMIN_UP) != 0;
269 }
270
271 always_inline uword
272 vnet_swif_is_api_visible (vnet_sw_interface_t * si)
273 {
274   return !(si->flags & VNET_SW_INTERFACE_FLAG_HIDDEN);
275 }
276
277 always_inline uword
278 vnet_sw_interface_is_api_visible (vnet_main_t * vnm, u32 sw_if_index)
279 {
280   vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
281   return vnet_swif_is_api_visible (si);
282 }
283
284 always_inline uword
285 vnet_sw_interface_is_api_valid (vnet_main_t * vnm, u32 sw_if_index)
286 {
287   return !pool_is_free_index (vnm->interface_main.sw_interfaces, sw_if_index)
288     && vnet_sw_interface_is_api_visible (vnm, sw_if_index);
289 }
290
291 always_inline const u8 *
292 vnet_sw_interface_get_hw_address (vnet_main_t * vnm, u32 sw_if_index)
293 {
294   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
295   return hw->hw_address;
296 }
297
298 always_inline uword
299 vnet_hw_interface_get_flags (vnet_main_t * vnm, u32 hw_if_index)
300 {
301   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
302   return hw->flags;
303 }
304
305 always_inline u32
306 vnet_hw_interface_get_mtu (vnet_main_t * vnm, u32 hw_if_index)
307 {
308   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
309   return hw->max_packet_bytes;
310 }
311
312 always_inline u32
313 vnet_sw_interface_get_mtu (vnet_main_t * vnm, u32 sw_if_index, vnet_mtu_t af)
314 {
315   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
316   u32 mtu;
317   mtu = sw->mtu[af] > 0 ? sw->mtu[af] : sw->mtu[VNET_MTU_L3];
318   if (mtu == 0)
319     return 9000;                /* $$$ Deal with interface-types not setting MTU */
320   return mtu;
321 }
322
323 always_inline uword
324 vnet_hw_interface_is_link_up (vnet_main_t * vnm, u32 hw_if_index)
325 {
326   return ((vnet_hw_interface_get_flags (vnm, hw_if_index) &
327            VNET_HW_INTERFACE_FLAG_LINK_UP) != 0);
328 }
329
330 always_inline uword
331 vnet_sw_interface_is_link_up (vnet_main_t * vnm, u32 sw_if_index)
332 {
333   vnet_sw_interface_t *sw = vnet_get_sup_sw_interface (vnm, sw_if_index);
334
335   return (vnet_hw_interface_is_link_up (vnm, sw->hw_if_index));
336 }
337
338 always_inline uword
339 vnet_sw_interface_is_up (vnet_main_t * vnm, u32 sw_if_index)
340 {
341   return (vnet_sw_interface_is_admin_up (vnm, sw_if_index) &&
342           vnet_sw_interface_is_link_up (vnm, sw_if_index));
343 }
344
345 always_inline uword
346 vnet_sw_interface_is_sub (vnet_main_t *vnm, u32 sw_if_index)
347 {
348   vnet_sw_interface_t *sw = vnet_get_sw_interface (vnm, sw_if_index);
349
350   return (sw->sw_if_index != sw->sup_sw_if_index);
351 }
352
353 clib_error_t *vnet_sw_interface_supports_addressing (vnet_main_t *vnm,
354                                                      u32 sw_if_index);
355
356 always_inline vlib_frame_t *
357 vnet_get_frame_to_sw_interface (vnet_main_t * vnm, u32 sw_if_index)
358 {
359   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
360   return vlib_get_frame_to_node (vlib_get_main (), hw->output_node_index);
361 }
362
363 always_inline void
364 vnet_put_frame_to_sw_interface (vnet_main_t * vnm, u32 sw_if_index,
365                                 vlib_frame_t * f)
366 {
367   vnet_hw_interface_t *hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
368   return vlib_put_frame_to_node (vlib_get_main (), hw->output_node_index, f);
369 }
370
371 always_inline void
372 vnet_hw_interface_set_link_speed (vnet_main_t * vnm, u32 hw_if_index,
373                                   u32 link_speed)
374 {
375   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
376   hw->link_speed = link_speed;
377 }
378
379 /* Change interface flags (e.g. up, down, enable, disable). */
380 clib_error_t *vnet_hw_interface_set_flags (vnet_main_t * vnm, u32 hw_if_index,
381                                            vnet_hw_interface_flags_t flags);
382
383 /* Change interface flags (e.g. up, down, enable, disable). */
384 clib_error_t *vnet_sw_interface_set_flags (vnet_main_t * vnm, u32 sw_if_index,
385                                            vnet_sw_interface_flags_t flags);
386
387 void vnet_sw_interface_admin_up (vnet_main_t * vnm, u32 sw_if_index);
388 void vnet_sw_interface_admin_down (vnet_main_t * vnm, u32 sw_if_index);
389
390 /* Change interface class. */
391 clib_error_t *vnet_hw_interface_set_class (vnet_main_t * vnm, u32 hw_if_index,
392                                            u32 new_hw_class_index);
393
394 /* Redirect rx pkts to node */
395 int vnet_hw_interface_rx_redirect_to_node (vnet_main_t * vnm, u32 hw_if_index,
396                                            u32 node_index);
397
398 void vnet_hw_interface_init_for_class (vnet_main_t * vnm, u32 hw_if_index,
399                                        u32 hw_class_index, u32 hw_instance);
400
401 /* Rename interface */
402 clib_error_t *vnet_rename_interface (vnet_main_t * vnm, u32 hw_if_index,
403                                      char *new_name);
404
405 /* Add/delete secondary interface mac address*/
406 clib_error_t *vnet_hw_interface_add_del_mac_address (vnet_main_t * vnm,
407                                                      u32 hw_if_index,
408                                                      const u8 * mac_address,
409                                                      u8 is_add);
410
411 /* Change interface mac address*/
412 clib_error_t *vnet_hw_interface_change_mac_address (vnet_main_t * vnm,
413                                                     u32 hw_if_index,
414                                                     const u8 * mac_address);
415
416 /* Change rx-mode */
417 clib_error_t *set_hw_interface_change_rx_mode (vnet_main_t * vnm,
418                                                u32 hw_if_index,
419                                                u8 queue_id_valid,
420                                                u32 queue_id,
421                                                vnet_hw_if_rx_mode mode);
422
423 /* Set rx-placement on the interface */
424 clib_error_t *set_hw_interface_rx_placement (u32 hw_if_index, u32 queue_id,
425                                              u32 thread_index, u8 is_main);
426
427 /* Set the MTU on the HW interface */
428 void vnet_hw_interface_set_mtu (vnet_main_t * vnm, u32 hw_if_index, u32 mtu);
429
430 /* Set the MTU on the SW interface */
431 void vnet_sw_interface_set_mtu (vnet_main_t * vnm, u32 sw_if_index, u32 mtu);
432 void vnet_sw_interface_set_protocol_mtu (vnet_main_t * vnm, u32 sw_if_index,
433                                          u32 mtu[]);
434
435 /* update the unnumbered state of an interface */
436 int vnet_sw_interface_update_unnumbered (u32 sw_if_index, u32 ip_sw_if_index,
437                                          u8 enable);
438
439 int vnet_sw_interface_stats_collect_enable_disable (u32 sw_if_index,
440                                                     u8 enable);
441 void vnet_sw_interface_ip_directed_broadcast (vnet_main_t * vnm,
442                                               u32 sw_if_index, u8 enable);
443
444 /* set interface rss queues */
445 clib_error_t *vnet_hw_interface_set_rss_queues (vnet_main_t * vnm,
446                                                 vnet_hw_interface_t * hi,
447                                                 clib_bitmap_t * bitmap);
448
449 void vnet_hw_if_update_runtime_data (vnet_main_t *vnm, u32 hw_if_index);
450
451 /* Formats sw/hw interface. */
452 format_function_t format_vnet_hw_interface;
453 format_function_t format_vnet_hw_if_rx_mode;
454 format_function_t format_vnet_hw_if_index_name;
455 format_function_t format_vnet_sw_interface;
456 format_function_t format_vnet_sw_interface_name;
457 format_function_t format_vnet_sw_interface_name_override;
458 format_function_t format_vnet_sw_if_index_name;
459 format_function_t format_vnet_sw_interface_flags;
460
461 /* Parses sw/hw interface name -> index. */
462 unformat_function_t unformat_vnet_sw_interface;
463 unformat_function_t unformat_vnet_hw_interface;
464 unformat_function_t unformat_vnet_buffer_flags;
465 unformat_function_t unformat_vnet_buffer_offload_flags;
466
467 /* Parses interface flags (up, down, enable, disable, etc.) */
468 unformat_function_t unformat_vnet_hw_interface_flags;
469 unformat_function_t unformat_vnet_sw_interface_flags;
470
471 /* VLAN tag-rewrite */
472 format_function_t format_vtr;
473
474 /* Node runtime for interface output function. */
475 typedef struct
476 {
477   u32 hw_if_index;
478   u32 sw_if_index;
479   u32 dev_instance;
480   u32 is_deleted;
481 } vnet_interface_output_runtime_t;
482
483 /* Interface output function. */
484 word vnet_sw_interface_compare (vnet_main_t * vnm, uword sw_if_index0,
485                                 uword sw_if_index1);
486 word vnet_hw_interface_compare (vnet_main_t * vnm, uword hw_if_index0,
487                                 uword hw_if_index1);
488
489 typedef enum
490 {
491   VNET_INTERFACE_OUTPUT_NEXT_DROP,
492   VNET_INTERFACE_OUTPUT_NEXT_TX,
493 } vnet_interface_output_next_t;
494
495 typedef enum
496 {
497   VNET_INTERFACE_TX_NEXT_DROP,
498   VNET_INTERFACE_TX_N_NEXT,
499 } vnet_interface_tx_next_t;
500
501 #define VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT VNET_INTERFACE_TX_N_NEXT
502 #define VNET_SIMULATED_ETHERNET_TX_NEXT_L2_INPUT (VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT + 1)
503
504 typedef enum
505 {
506   VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DOWN,
507   VNET_INTERFACE_OUTPUT_ERROR_INTERFACE_DELETED,
508 } vnet_interface_output_error_t;
509
510 /* Format for interface output traces. */
511 u8 *format_vnet_interface_output_trace (u8 * s, va_list * va);
512
513 serialize_function_t serialize_vnet_interface_state,
514   unserialize_vnet_interface_state;
515
516 /**
517  * @brief Add buffer (vlib_buffer_t) to the trace
518  *
519  * @param *pm - pcap_main_t
520  * @param *vm - vlib_main_t
521  * @param buffer_index - u32
522  * @param n_bytes_in_trace - u32
523  *
524  */
525 static inline void
526 pcap_add_buffer (pcap_main_t *pm, struct vlib_main_t *vm, u32 buffer_index,
527                  u32 n_bytes_in_trace)
528 {
529   vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index);
530   u32 n = vlib_buffer_length_in_chain (vm, b);
531   i32 n_left = clib_min (n_bytes_in_trace, n);
532   f64 time_now = vlib_time_now (vm);
533   void *d;
534
535   if (PREDICT_TRUE (pm->n_packets_captured < pm->n_packets_to_capture))
536     {
537       clib_spinlock_lock_if_init (&pm->lock);
538       d = pcap_add_packet (pm, time_now, n_left, n);
539       while (1)
540         {
541           u32 copy_length = clib_min ((u32) n_left, b->current_length);
542           clib_memcpy_fast (d, b->data + b->current_data, copy_length);
543           n_left -= b->current_length;
544           if (n_left <= 0)
545             break;
546           d += b->current_length;
547           ASSERT (b->flags & VLIB_BUFFER_NEXT_PRESENT);
548           b = vlib_get_buffer (vm, b->next_buffer);
549         }
550       clib_spinlock_unlock_if_init (&pm->lock);
551     }
552 }
553 #endif /* included_vnet_interface_funcs_h */
554
555 /*
556  * fd.io coding-style-patch-verification: ON
557  *
558  * Local Variables:
559  * eval: (c-set-style "gnu")
560  * End:
561  */