Improve MTU handling 83/10483/2
authorNeale Ranns <neale.ranns@cisco.com>
Fri, 9 Feb 2018 14:05:16 +0000 (06:05 -0800)
committerDamjan Marion <dmarion.lists@gmail.com>
Mon, 12 Feb 2018 11:23:33 +0000 (11:23 +0000)
- setting MTU on an interface updates the L3 max bytes too
- value cached in the adjacency is also updated
- MTU exceeded generates ICMP to sender

Change-Id: I343ec71d8e903b529594c4bd0543f04bc7f370b3
Signed-off-by: Neale Ranns <neale.ranns@cisco.com>
src/vnet/adj/adj.c
src/vnet/adj/adj.h
src/vnet/adj/rewrite.c
src/vnet/adj/rewrite.h
src/vnet/interface.c
src/vnet/interface_api.c
src/vnet/interface_cli.c
src/vnet/interface_funcs.h
src/vnet/ip/ip4_forward.c
test/test_ip4.py
test/vpp_papi_provider.py

index 5f7fe74..d28d519 100644 (file)
@@ -338,6 +338,43 @@ adj_feature_update (u32 sw_if_index,
     adj_walk (sw_if_index, adj_feature_update_walk_cb, &ctx);
 }
 
+static adj_walk_rc_t
+adj_mtu_update_walk_cb (adj_index_t ai,
+                        void *arg)
+{
+    ip_adjacency_t *adj;
+
+    adj = adj_get(ai);
+
+    vnet_rewrite_update_mtu (vnet_get_main(),
+                             &adj->rewrite_header);
+
+    return (ADJ_WALK_RC_CONTINUE);
+}
+
+static void
+adj_sw_mtu_update (vnet_main_t * vnm,
+                   u32 sw_if_index,
+                   void *ctx)
+{
+    /*
+     * Walk all the adjacencies on the interface to update the cached MTU
+     */
+    adj_walk (sw_if_index, adj_mtu_update_walk_cb, NULL);
+}
+
+void
+adj_mtu_update (u32 hw_if_index)
+{
+    /*
+     * Walk all the SW interfaces on the HW interface to update the cached MTU
+     */
+    vnet_hw_interface_walk_sw(vnet_get_main(),
+                              hw_if_index,
+                              adj_sw_mtu_update,
+                              NULL);
+}
+
 /**
  * @brief Walk the Adjacencies on a given interface
  */
index fe77d16..bcf6c04 100644 (file)
@@ -344,6 +344,12 @@ extern const u8* adj_get_rewrite (adj_index_t ai);
  */
 extern void adj_feature_update (u32 sw_if_index, u8 arc_index, u8 is_enable);
 
+/**
+ * @brief Notify the adjacency subsystem that the MTU settings for
+ * an HW interface have changed
+ */
+extern void adj_mtu_update (u32 hw_if_index);
+
 /**
  * @brief
  * The global adjacnecy pool. Exposed for fast/inline data-plane access
index 9150f2c..c21495a 100644 (file)
@@ -77,15 +77,14 @@ format_vnet_rewrite (u8 * s, va_list * args)
       vnet_sw_interface_t *si;
       si = vnet_get_sw_interface_safe (vnm, rw->sw_if_index);
       if (NULL != si)
-       s = format (s, "%U: ", format_vnet_sw_interface_name, vnm, si);
+       s = format (s, "%U:", format_vnet_sw_interface_name, vnm, si);
       else
        s = format (s, "DELETED:%d", rw->sw_if_index);
     }
 
   /* Format rewrite string. */
   if (rw->data_bytes > 0)
-
-    s = format (s, "%U",
+    s = format (s, " %U",
                format_hex_bytes,
                rw->data + max_data_bytes - rw->data_bytes, rw->data_bytes);
 
@@ -110,6 +109,13 @@ vnet_rewrite_init (vnet_main_t * vnm,
     vnet_sw_interface_get_mtu (vnm, sw_if_index, VLIB_TX);
 }
 
+void
+vnet_rewrite_update_mtu (vnet_main_t * vnm, vnet_rewrite_header_t * rw)
+{
+  rw->max_l3_packet_bytes =
+    vnet_sw_interface_get_mtu (vnm, rw->sw_if_index, VLIB_TX);
+}
+
 void
 vnet_rewrite_for_sw_interface (vnet_main_t * vnm,
                               vnet_link_t link_type,
index 1dea72f..005ac41 100644 (file)
@@ -328,6 +328,9 @@ void vnet_rewrite_init (struct vnet_main_t *vnm,
                        u32 this_node,
                        u32 next_node, vnet_rewrite_header_t * rw);
 
+void vnet_rewrite_update_mtu (struct vnet_main_t *vnm,
+                             vnet_rewrite_header_t * rw);
+
 u8 *vnet_build_rewrite_for_sw_interface (struct vnet_main_t *vnm,
                                         u32 sw_if_index,
                                         vnet_link_t packet_type,
index a34bb79..7516aec 100644 (file)
@@ -122,6 +122,22 @@ unserialize_vnet_sw_interface_set_flags (serialize_main_t * m, va_list * va)
      /* helper_flags no redistribution */ 0);
 }
 
+void
+vnet_hw_interface_set_mtu (vnet_main_t * vnm, u32 hw_if_index, u32 mtu)
+{
+  vnet_hw_interface_t *hi = vnet_get_hw_interface (vnm, hw_if_index);
+
+  if (hi->max_packet_bytes != mtu)
+    {
+      u16 l3_pad = hi->max_packet_bytes - hi->max_l3_packet_bytes[VLIB_TX];
+      hi->max_packet_bytes = mtu;
+      hi->max_l3_packet_bytes[VLIB_TX] =
+       hi->max_l3_packet_bytes[VLIB_RX] = mtu - l3_pad;
+      ethernet_set_flags (vnm, hw_if_index, ETHERNET_INTERFACE_FLAG_MTU);
+      adj_mtu_update (hw_if_index);
+    }
+}
+
 static void
 unserialize_vnet_hw_interface_set_flags (serialize_main_t * m, va_list * va)
 {
index 0541f31..ed116bc 100644 (file)
@@ -98,7 +98,6 @@ vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
 {
   vl_api_sw_interface_set_mtu_reply_t *rmp;
   vnet_main_t *vnm = vnet_get_main ();
-  u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
   u32 sw_if_index = ntohl (mp->sw_if_index);
   u16 mtu = ntohs (mp->mtu);
   ethernet_main_t *em = &ethernet_main;
@@ -134,11 +133,7 @@ vl_api_sw_interface_set_mtu_t_handler (vl_api_sw_interface_set_mtu_t * mp)
       goto bad_sw_if_index;
     }
 
-  if (hi->max_packet_bytes != mtu)
-    {
-      hi->max_packet_bytes = mtu;
-      ethernet_set_flags (vnm, si->hw_if_index, flags);
-    }
+  vnet_hw_interface_set_mtu (vnm, si->hw_if_index, mtu);
 
   BAD_SW_IF_INDEX_LABEL;
   REPLY_MACRO (VL_API_SW_INTERFACE_SET_MTU_REPLY);
index 5509e3f..8880d97 100644 (file)
@@ -1132,7 +1132,6 @@ mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   vnet_main_t *vnm = vnet_get_main ();
   u32 hw_if_index, mtu;
-  u32 flags = ETHERNET_INTERFACE_FLAG_MTU;
   ethernet_main_t *em = &ethernet_main;
 
   if (unformat (input, "%d %U", &mtu,
@@ -1153,11 +1152,7 @@ mtu_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
        return clib_error_return (0, "Invalid mtu (%d): must be <= (%d)", mtu,
                                  hi->max_supported_packet_bytes);
 
-      if (hi->max_packet_bytes != mtu)
-       {
-         hi->max_packet_bytes = mtu;
-         ethernet_set_flags (vnm, hw_if_index, flags);
-       }
+      vnet_hw_interface_set_mtu (vnm, hw_if_index, mtu);
     }
   else
     return clib_error_return (0, "unknown input `%U'",
index ac50e7c..5aecaff 100644 (file)
@@ -292,6 +292,9 @@ clib_error_t *set_hw_interface_change_rx_mode (vnet_main_t * vnm,
                                               vnet_hw_interface_rx_mode
                                               mode);
 
+/* Set the MTU on the HW interface */
+void vnet_hw_interface_set_mtu (vnet_main_t * vnm, u32 hw_if_index, u32 mtu);
+
 /* Formats sw/hw interface. */
 format_function_t format_vnet_hw_interface;
 format_function_t format_vnet_hw_interface_rx_mode;
index ae45106..b9875d7 100644 (file)
@@ -2109,16 +2109,26 @@ ip4_rewrite_inline (vlib_main_t * vm,
          vnet_buffer (p1)->ip.save_rewrite_length = rw_len1;
 
          /* Check MTU of outgoing interface. */
-         error0 =
-           (vlib_buffer_length_in_chain (vm, p0) >
-            adj0[0].
-            rewrite_header.max_l3_packet_bytes ? IP4_ERROR_MTU_EXCEEDED :
-            error0);
-         error1 =
-           (vlib_buffer_length_in_chain (vm, p1) >
-            adj1[0].
-            rewrite_header.max_l3_packet_bytes ? IP4_ERROR_MTU_EXCEEDED :
-            error1);
+         if (vlib_buffer_length_in_chain (vm, p0) >
+             adj0[0].rewrite_header.max_l3_packet_bytes)
+           {
+             error0 = IP4_ERROR_MTU_EXCEEDED;
+             next0 = IP4_REWRITE_NEXT_ICMP_ERROR;
+             icmp4_error_set_vnet_buffer
+               (p0, ICMP4_destination_unreachable,
+                ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
+                0);
+           }
+         if (vlib_buffer_length_in_chain (vm, p1) >
+             adj1[0].rewrite_header.max_l3_packet_bytes)
+           {
+             error1 = IP4_ERROR_MTU_EXCEEDED;
+             next1 = IP4_REWRITE_NEXT_ICMP_ERROR;
+             icmp4_error_set_vnet_buffer
+               (p1, ICMP4_destination_unreachable,
+                ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
+                0);
+           }
 
          if (is_mcast)
            {
@@ -2290,9 +2300,16 @@ ip4_rewrite_inline (vlib_main_t * vm,
               vlib_buffer_length_in_chain (vm, p0) + rw_len0);
 
          /* Check MTU of outgoing interface. */
-         error0 = (vlib_buffer_length_in_chain (vm, p0)
-                   > adj0[0].rewrite_header.max_l3_packet_bytes
-                   ? IP4_ERROR_MTU_EXCEEDED : error0);
+         if (vlib_buffer_length_in_chain (vm, p0) >
+             adj0[0].rewrite_header.max_l3_packet_bytes)
+           {
+             error0 = IP4_ERROR_MTU_EXCEEDED;
+             next0 = IP4_REWRITE_NEXT_ICMP_ERROR;
+             icmp4_error_set_vnet_buffer
+               (p0, ICMP4_destination_unreachable,
+                ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
+                0);
+           }
          if (is_mcast)
            {
              error0 = ((adj0[0].rewrite_header.sw_if_index ==
index 79d4a36..1a61125 100644 (file)
@@ -1287,6 +1287,32 @@ class TestIPInput(VppTestCase):
         self.assertEqual(icmp.src, self.pg0.remote_ip4)
         self.assertEqual(icmp.dst, self.pg1.remote_ip4)
 
+        #
+        # MTU exceeded
+        #
+        p_mtu = (Ether(src=self.pg0.remote_mac,
+                       dst=self.pg0.local_mac) /
+                 IP(src=self.pg0.remote_ip4,
+                    dst=self.pg1.remote_ip4,
+                    ttl=10) /
+                 UDP(sport=1234, dport=1234) /
+                 Raw('\xa5' * 2000))
+
+        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, 1500)
+
+        rx = self.send_and_expect(self.pg0, p_mtu * 65, self.pg0)
+        rx = rx[0]
+        icmp = rx[ICMP]
+
+        self.assertEqual(icmptypes[icmp.type], "dest-unreach")
+        self.assertEqual(icmpcodes[icmp.type][icmp.code],
+                         "fragmentation-needed")
+        self.assertEqual(icmp.src, self.pg0.remote_ip4)
+        self.assertEqual(icmp.dst, self.pg1.remote_ip4)
+
+        self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, 2500)
+        rx = self.send_and_expect(self.pg0, p_mtu * 65, self.pg1)
+
 
 if __name__ == '__main__':
     unittest.main(testRunner=VppTestRunner)
index 15a566c..b7c7653 100644 (file)
@@ -629,6 +629,16 @@ class VppPapiProvider(object):
                         {'sw_if_index': sw_if_index,
                          'admin_up_down': admin_up_down})
 
+    def sw_interface_set_mtu(self, sw_if_index, mtu):
+        """
+        :param sw_if_index:
+        :param mtu:
+
+        """
+        return self.api(self.papi.sw_interface_set_mtu,
+                        {'sw_if_index': sw_if_index,
+                         'mtu': mtu})
+
     def create_subif(self, sw_if_index, sub_id, outer_vlan, inner_vlan,
                      no_tags=0, one_tag=0, two_tags=0, dot1ad=0, exact_match=0,
                      default_sub=0, outer_vlan_id_any=0, inner_vlan_id_any=0):