From 22e9cfd760be613f33a4135e9247729b64619cc6 Mon Sep 17 00:00:00 2001 From: Mohsin Kazmi Date: Tue, 23 Jul 2019 11:54:48 +0200 Subject: pg: add GSO support Type: feature Change-Id: I72676495a85fbecc946aa266a75234cce70c3a5e Signed-off-by: Mohsin Kazmi --- src/vat/api_format.c | 17 ++++- src/vnet/pg/cli.c | 18 +++-- src/vnet/pg/input.c | 73 ++++++++++++++++++- src/vnet/pg/pg.api | 6 +- src/vnet/pg/pg.h | 5 +- src/vnet/pg/pg_api.c | 4 +- src/vnet/pg/stream.c | 14 +++- src/vpp/api/custom_dump.c | 2 + test/framework.py | 4 +- test/test_gso.py | 175 ++++++++++++++++++++++++++++++++++++++++++++++ test/vpp_pg_interface.py | 18 ++++- 11 files changed, 319 insertions(+), 17 deletions(-) create mode 100644 test/test_gso.py diff --git a/src/vat/api_format.c b/src/vat/api_format.c index 69a57ab3d33..a1cd9fee527 100644 --- a/src/vat/api_format.c +++ b/src/vat/api_format.c @@ -19749,12 +19749,24 @@ api_pg_create_interface (vat_main_t * vam) unformat_input_t *input = vam->input; vl_api_pg_create_interface_t *mp; - u32 if_id = ~0; + u32 if_id = ~0, gso_size = 0; + u8 gso_enabled = 0; int ret; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "if_id %d", &if_id)) ; + else if (unformat (input, "gso-enabled")) + { + gso_enabled = 1; + if (unformat (input, "gso-size %u", &gso_size)) + ; + else + { + errmsg ("missing gso-size"); + return -99; + } + } else break; } @@ -19768,6 +19780,7 @@ api_pg_create_interface (vat_main_t * vam) M (PG_CREATE_INTERFACE, mp); mp->context = 0; mp->interface_id = ntohl (if_id); + mp->gso_enabled = gso_enabled; S (mp); W (ret); @@ -22324,7 +22337,7 @@ _(ipfix_classify_table_dump, "") \ _(sw_interface_span_enable_disable, "[l2] [src | src_sw_if_index ] [disable | [[dst | dst_sw_if_index ] [both|rx|tx]]]") \ _(sw_interface_span_dump, "[l2]") \ _(get_next_index, "node-name next-node-name ") \ -_(pg_create_interface, "if_id ") \ +_(pg_create_interface, "if_id [gso-enabled gso-size ]") \ _(pg_capture, "if_id pcap count [disable]") \ _(pg_enable_disable, "[stream ] disable") \ _(ip_source_and_port_range_check_add_del, \ diff --git a/src/vnet/pg/cli.c b/src/vnet/pg/cli.c index 8f82ac1c1f7..a8451d74e1e 100644 --- a/src/vnet/pg/cli.c +++ b/src/vnet/pg/cli.c @@ -649,7 +649,7 @@ create_pg_if_cmd_fn (vlib_main_t * vm, { pg_main_t *pg = &pg_main; unformat_input_t _line_input, *line_input = &_line_input; - u32 if_id; + u32 if_id, gso_enabled = 0, gso_size = 0; clib_error_t *error = NULL; if (!unformat_user (input, unformat_line_input, line_input)) @@ -659,7 +659,17 @@ create_pg_if_cmd_fn (vlib_main_t * vm, { if (unformat (line_input, "interface pg%u", &if_id)) ; - + else if (unformat (line_input, "gso-enabled")) + { + gso_enabled = 1; + if (unformat (line_input, "gso-size %u", &gso_size)) + ; + else + { + error = clib_error_create ("gso enabled but gso size missing"); + goto done; + } + } else { error = clib_error_create ("unknown input `%U'", @@ -668,7 +678,7 @@ create_pg_if_cmd_fn (vlib_main_t * vm, } } - pg_interface_add_or_get (pg, if_id); + pg_interface_add_or_get (pg, if_id, gso_enabled, gso_size); done: unformat_free (line_input); @@ -679,7 +689,7 @@ done: /* *INDENT-OFF* */ VLIB_CLI_COMMAND (create_pg_if_cmd, static) = { .path = "create packet-generator", - .short_help = "create packet-generator interface ", + .short_help = "create packet-generator interface [gso-enabled gso-size ]", .function = create_pg_if_cmd_fn, }; /* *INDENT-ON* */ diff --git a/src/vnet/pg/input.c b/src/vnet/pg/input.c index 2d850e939b0..151624fe00f 100644 --- a/src/vnet/pg/input.c +++ b/src/vnet/pg/input.c @@ -50,6 +50,9 @@ #include #include #include +#include +#include +#include #include static int @@ -1523,6 +1526,70 @@ pg_input_trace (pg_main_t * pg, } } +static_always_inline void +fill_gso_buffer_flags (vlib_main_t * vm, u32 * buffers, u32 n_buffers, + u32 packet_data_size) +{ + + for (int i = 0; i < n_buffers; i++) + { + vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[i]); + u8 l4_proto = 0; + u8 l4_hdr_sz = 0; + + ethernet_header_t *eh = (ethernet_header_t *) b0->data; + u16 ethertype = clib_net_to_host_u16 (eh->type); + u16 l2hdr_sz = sizeof (ethernet_header_t); + + vnet_buffer (b0)->l2_hdr_offset = 0; + vnet_buffer (b0)->l3_hdr_offset = l2hdr_sz; + if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP4)) + { + ip4_header_t *ip4 = (ip4_header_t *) (b0->data + l2hdr_sz); + vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + ip4_header_bytes (ip4); + l4_proto = ip4->protocol; + b0->flags |= + (VNET_BUFFER_F_IS_IP4 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID + | VNET_BUFFER_F_L3_HDR_OFFSET_VALID | + VNET_BUFFER_F_L4_HDR_OFFSET_VALID); + b0->flags |= VNET_BUFFER_F_OFFLOAD_IP_CKSUM; + } + else if (PREDICT_TRUE (ethertype == ETHERNET_TYPE_IP6)) + { + ip6_header_t *ip6 = (ip6_header_t *) (b0->data + l2hdr_sz); + /* FIXME IPv6 EH traversal */ + vnet_buffer (b0)->l4_hdr_offset = l2hdr_sz + sizeof (ip6_header_t); + l4_proto = ip6->protocol; + b0->flags |= + (VNET_BUFFER_F_IS_IP6 | VNET_BUFFER_F_L2_HDR_OFFSET_VALID + | VNET_BUFFER_F_L3_HDR_OFFSET_VALID | + VNET_BUFFER_F_L4_HDR_OFFSET_VALID); + b0->flags |= VNET_BUFFER_F_OFFLOAD_IP_CKSUM; + } + if (l4_proto == IP_PROTOCOL_TCP) + { + b0->flags |= VNET_BUFFER_F_OFFLOAD_TCP_CKSUM; + tcp_header_t *tcp = (tcp_header_t *) (b0->data + + vnet_buffer + (b0)->l4_hdr_offset); + l4_hdr_sz = tcp_header_bytes (tcp); + tcp->checksum = 0; + vnet_buffer2 (b0)->gso_l4_hdr_sz = l4_hdr_sz; + vnet_buffer2 (b0)->gso_size = packet_data_size; + b0->flags |= VNET_BUFFER_F_GSO; + } + else if (l4_proto == IP_PROTOCOL_UDP) + { + b0->flags |= VNET_BUFFER_F_OFFLOAD_UDP_CKSUM; + udp_header_t *udp = (udp_header_t *) (b0->data + + vnet_buffer + (b0)->l4_hdr_offset); + vnet_buffer2 (b0)->gso_l4_hdr_sz = sizeof (*udp); + udp->checksum = 0; + } + } +} + static uword pg_generate_packets (vlib_node_runtime_t * node, pg_main_t * pg, @@ -1538,6 +1605,7 @@ pg_generate_packets (vlib_node_runtime_t * node, u8 feature_arc_index = fm->device_input_feature_arc_index; cm = &fm->feature_config_mains[feature_arc_index]; u32 current_config_index = ~(u32) 0; + pg_interface_t *pi = pool_elt_at_index (pg->interfaces, s->pg_if_index); int i; bi0 = s->buffer_indices; @@ -1564,14 +1632,12 @@ pg_generate_packets (vlib_node_runtime_t * node, vlib_next_frame_t *nf; vlib_frame_t *f; ethernet_input_frame_t *ef; - pg_interface_t *pi; vlib_get_new_next_frame (vm, node, next_index, to_next, n_left); nf = vlib_node_runtime_get_next_frame (vm, node, next_index); f = vlib_get_frame (vm, nf->frame); f->flags = ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX; ef = vlib_frame_scalar_args (f); - pi = pool_elt_at_index (pg->interfaces, s->pg_if_index); ef->sw_if_index = pi->sw_if_index; ef->hw_if_index = pi->hw_if_index; vlib_frame_no_append (f); @@ -1615,6 +1681,9 @@ pg_generate_packets (vlib_node_runtime_t * node, vnet_buffer (b)->feature_arc_index = feature_arc_index; } + if (pi->gso_enabled) + fill_gso_buffer_flags (vm, to_next, n_this_frame, pi->gso_size); + n_trace = vlib_get_trace_count (vm, node); if (n_trace > 0) { diff --git a/src/vnet/pg/pg.api b/src/vnet/pg/pg.api index 8f986cbc5ed..6a421e45d7d 100644 --- a/src/vnet/pg/pg.api +++ b/src/vnet/pg/pg.api @@ -18,18 +18,22 @@ This file defines packet-generator interface APIs. */ -option version = "1.0.0"; +option version = "1.1.0"; /** \brief PacketGenerator create interface request @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request @param interface_id - interface index + @param enable_gso - enable gso on this interface + @param gso_size - gso size on this interface */ define pg_create_interface { u32 client_index; u32 context; u32 interface_id; + u8 gso_enabled; + u32 gso_size; }; /** \brief PacketGenerator create interface response diff --git a/src/vnet/pg/pg.h b/src/vnet/pg/pg.h index c0e000fe7b2..aef7a5bbf71 100644 --- a/src/vnet/pg/pg.h +++ b/src/vnet/pg/pg.h @@ -299,6 +299,8 @@ typedef struct /* Identifies stream for this interface. */ u32 id; + u8 gso_enabled; + u32 gso_size; pcap_main_t pcap_main; u8 *pcap_file_name; } pg_interface_t; @@ -351,7 +353,8 @@ void pg_stream_enable_disable (pg_main_t * pg, pg_stream_t * s, int is_enable); /* Find/create free packet-generator interface index. */ -u32 pg_interface_add_or_get (pg_main_t * pg, uword stream_index); +u32 pg_interface_add_or_get (pg_main_t * pg, uword stream_index, + u8 gso_enabled, u32 gso_size); always_inline pg_node_t * pg_get_node (uword node_index) diff --git a/src/vnet/pg/pg_api.c b/src/vnet/pg/pg_api.c index 7775343a6f5..f4b9604bfd4 100644 --- a/src/vnet/pg/pg_api.c +++ b/src/vnet/pg/pg_api.c @@ -53,7 +53,9 @@ vl_api_pg_create_interface_t_handler (vl_api_pg_create_interface_t * mp) int rv = 0; pg_main_t *pg = &pg_main; - u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id)); + u32 pg_if_id = pg_interface_add_or_get (pg, ntohl (mp->interface_id), + mp->gso_enabled, + ntohl (mp->gso_size)); pg_interface_t *pi = pool_elt_at_index (pg->interfaces, pg_if_id); /* *INDENT-OFF* */ diff --git a/src/vnet/pg/stream.c b/src/vnet/pg/stream.c index 64fe7c859ae..c4a57ea702d 100644 --- a/src/vnet/pg/stream.c +++ b/src/vnet/pg/stream.c @@ -179,7 +179,8 @@ pg_eth_flag_change (vnet_main_t * vnm, vnet_hw_interface_t * hi, u32 flags) } u32 -pg_interface_add_or_get (pg_main_t * pg, uword if_id) +pg_interface_add_or_get (pg_main_t * pg, uword if_id, u8 gso_enabled, + u32 gso_size) { vnet_main_t *vnm = vnet_get_main (); vlib_main_t *vm = vlib_get_main (); @@ -213,6 +214,13 @@ pg_interface_add_or_get (pg_main_t * pg, uword if_id) ethernet_register_interface (vnm, pg_dev_class.index, i, hw_addr, &pi->hw_if_index, pg_eth_flag_change); hi = vnet_get_hw_interface (vnm, pi->hw_if_index); + if (gso_enabled) + { + hi->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO; + vnm->interface_main.gso_interface_count++; + pi->gso_enabled = 1; + pi->gso_size = gso_size; + } pi->sw_if_index = hi->sw_if_index; hash_set (pg->if_index_by_if_id, if_id, i); @@ -445,7 +453,9 @@ pg_stream_add (pg_main_t * pg, pg_stream_t * s_init) } /* Find an interface to use. */ - s->pg_if_index = pg_interface_add_or_get (pg, s->if_id); + s->pg_if_index = + pg_interface_add_or_get (pg, s->if_id, 0 /* gso_enabled */ , + 0 /* gso_size */ ); if (s->sw_if_index[VLIB_RX] == ~0) { diff --git a/src/vpp/api/custom_dump.c b/src/vpp/api/custom_dump.c index 11ff41aa86c..e53d46259dc 100644 --- a/src/vpp/api/custom_dump.c +++ b/src/vpp/api/custom_dump.c @@ -2761,6 +2761,8 @@ static void *vl_api_pg_create_interface_t_print s = format (0, "SCRIPT: pg_create_interface "); s = format (0, "if_id %d", ntohl (mp->interface_id)); + s = format (0, "gso-enabled %u", mp->gso_enabled); + s = format (0, "gso-size %u", ntohl (mp->gso_size)); FINISH; } diff --git a/test/framework.py b/test/framework.py index 59c451d475c..e4d0f9d13ab 100644 --- a/test/framework.py +++ b/test/framework.py @@ -762,7 +762,7 @@ class VppTestCase(unittest.TestCase): cls._captures = [] @classmethod - def create_pg_interfaces(cls, interfaces): + def create_pg_interfaces(cls, interfaces, gso=0, gso_size=0): """ Create packet-generator interfaces. @@ -772,7 +772,7 @@ class VppTestCase(unittest.TestCase): """ result = [] for i in interfaces: - intf = VppPGInterface(cls, i) + intf = VppPGInterface(cls, i, gso, gso_size) setattr(cls, intf.name, intf) result.append(intf) cls.pg_interfaces = result diff --git a/test/test_gso.py b/test/test_gso.py new file mode 100644 index 00000000000..43d9e92380d --- /dev/null +++ b/test/test_gso.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python +"""GSO functional tests""" + +# +# Add tests for: +# - GSO +# - Verify that sending Jumbo frame without GSO enabled correctly +# - Verify that sending Jumbo frame with GSO enabled correctly +# - Verify that sending Jumbo frame with GSO enabled only on ingress interface +# +import unittest + +from scapy.packet import Raw +from scapy.layers.inet6 import IPv6, Ether, IP, UDP, ICMPv6PacketTooBig +from scapy.layers.inet import TCP, ICMP +from scapy.layers.vxlan import VXLAN +from scapy.data import ETH_P_IP, ETH_P_IPV6, ETH_P_ARP + +from framework import VppTestCase, VppTestRunner +from vpp_object import VppObject +from vpp_interface import VppInterface +from vpp_ip import DpoProto +from vpp_ip_route import VppIpRoute, VppRoutePath, FibPathProto +from socket import AF_INET, AF_INET6, inet_pton +from util import reassemble4 + + +""" Test_gso is a subclass of VPPTestCase classes. + GSO tests. +""" + + +class TestGSO(VppTestCase): + """ GSO Test Case """ + + def __init__(self, *args): + VppTestCase.__init__(self, *args) + + @classmethod + def setUpClass(self): + super(TestGSO, self).setUpClass() + + @classmethod + def tearDownClass(self): + super(TestGSO, self).tearDownClass() + + def setUp(self): + super(TestGSO, self).setUp() + + def tearDown(self): + super(TestGSO, self).tearDown() + if not self.vpp_dead: + for i in self.pg_interfaces: + i.unconfig_ip4() + i.unconfig_ip6() + i.admin_down() + + def test_gso(self): + """ GSO test """ + # + # Send jumbo frame with gso disabled and DF bit is set + # + self.create_pg_interfaces(range(2)) + for i in self.pg_interfaces: + i.admin_up() + i.config_ip4() + i.config_ip6() + i.disable_ipv6_ra() + i.resolve_arp() + i.resolve_ndp() + + p4 = (Ether(src=self.pg0.remote_mac, dst=self.pg0.local_mac) / + IP(src=self.pg0.remote_ip4, dst=self.pg1.remote_ip4, + flags='DF') / + TCP(sport=1234, dport=1234) / + Raw('\xa5' * 65200)) + + rxs = self.send_and_expect(self.pg0, [p4], self.pg0) + + for rx in rxs: + self.assertEqual(rx[Ether].src, self.pg0.local_mac) + self.assertEqual(rx[Ether].dst, self.pg0.remote_mac) + self.assertEqual(rx[IP].src, self.pg0.local_ip4) + self.assertEqual(rx[IP].dst, self.pg0.remote_ip4) + self.assertEqual(rx[ICMP].type, 3) # "dest-unreach" + self.assertEqual(rx[ICMP].code, 4) # "fragmentation-needed" + + # + # Send jumbo frame with gso enabled and DF bit is set + # input and output interfaces support GSO + # + self.create_pg_interfaces(range(2, 4), 1, 1460) + for i in self.pg_interfaces: + i.admin_up() + i.config_ip4() + i.config_ip6() + i.disable_ipv6_ra() + i.resolve_arp() + i.resolve_ndp() + + p41 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) / + IP(src=self.pg2.remote_ip4, dst=self.pg3.remote_ip4, + flags='DF') / + TCP(sport=1234, dport=1234) / + Raw('\xa5' * 65200)) + + rxs = self.send_and_expect(self.pg2, [p41], self.pg3) + + for rx in rxs: + self.assertEqual(rx[Ether].src, self.pg3.local_mac) + self.assertEqual(rx[Ether].dst, self.pg3.remote_mac) + self.assertEqual(rx[IP].src, self.pg2.remote_ip4) + self.assertEqual(rx[IP].dst, self.pg3.remote_ip4) + self.assertEqual(rx[IP].len, 65240) # 65200 + 20 (IP) + 20 (TCP) + self.assertEqual(rx[TCP].sport, 1234) + self.assertEqual(rx[TCP].dport, 1234) + + # + # Send jumbo frame with gso enabled only on input interface + # and DF bit is set. GSO packet will be chunked into gso_size + # data payload + # + self.create_pg_interfaces(range(4, 5)) + for i in self.pg_interfaces: + i.admin_up() + i.config_ip4() + i.config_ip6() + i.disable_ipv6_ra() + i.resolve_arp() + i.resolve_ndp() + + p42 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) / + IP(src=self.pg2.remote_ip4, dst=self.pg4.remote_ip4, + flags='DF') / + TCP(sport=1234, dport=1234) / + Raw('\xa5' * 65200)) + + rxs = self.send_and_expect(self.pg2, [p42], self.pg4, 45) + size = 0 + for rx in rxs: + self.assertEqual(rx[Ether].src, self.pg4.local_mac) + self.assertEqual(rx[Ether].dst, self.pg4.remote_mac) + self.assertEqual(rx[IP].src, self.pg2.remote_ip4) + self.assertEqual(rx[IP].dst, self.pg4.remote_ip4) + self.assertEqual(rx[TCP].sport, 1234) + self.assertEqual(rx[TCP].dport, 1234) + + size = rxs[44][TCP].seq + rxs[44][IP].len - 20 - 20 + self.assertEqual(size, 65200) + + # + # Send jumbo frame with gso enabled only on input interface + # and DF bit is unset. GSO packet will be fragmented. + # + self.vapi.sw_interface_set_mtu(self.pg1.sw_if_index, [576, 0, 0, 0]) + + p43 = (Ether(src=self.pg2.remote_mac, dst=self.pg2.local_mac) / + IP(src=self.pg2.remote_ip4, dst=self.pg1.remote_ip4) / + TCP(sport=1234, dport=1234) / + Raw('\xa5' * 65200)) + + rxs = self.send_and_expect(self.pg2, [p43], self.pg1, 119) + size = 0 + for rx in rxs: + rx.show() + self.assertEqual(rx[Ether].src, self.pg1.local_mac) + self.assertEqual(rx[Ether].dst, self.pg1.remote_mac) + self.assertEqual(rx[IP].src, self.pg2.remote_ip4) + self.assertEqual(rx[IP].dst, self.pg1.remote_ip4) + size += rx[IP].len - 20 + size -= 20 # TCP header + self.assertEqual(size, 65200) + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) diff --git a/test/vpp_pg_interface.py b/test/vpp_pg_interface.py index bd4ddaff58a..e6dae66feec 100755 --- a/test/vpp_pg_interface.py +++ b/test/vpp_pg_interface.py @@ -45,6 +45,18 @@ class VppPGInterface(VppInterface): """packet-generator interface index assigned by VPP""" return self._pg_index + @property + def gso_enabled(self): + """gso enabled on packet-generator interface""" + if self._gso_enabled == 0: + return "gso-disabled" + return "gso-enabled" + + @property + def gso_size(self): + """gso size on packet-generator interface""" + return self._gso_size + @property def out_path(self): """pcap file path - captured packets""" @@ -86,17 +98,19 @@ class VppPGInterface(VppInterface): self._out_history_counter += 1 return v - def __init__(self, test, pg_index): + def __init__(self, test, pg_index, gso, gso_size): """ Create VPP packet-generator interface """ super(VppPGInterface, self).__init__(test) - r = test.vapi.pg_create_interface(pg_index) + r = test.vapi.pg_create_interface(pg_index, gso, gso_size) self.set_sw_if_index(r.sw_if_index) self._in_history_counter = 0 self._out_history_counter = 0 self._out_assert_counter = 0 self._pg_index = pg_index + self._gso_enabled = gso + self._gso_size = gso_size self._out_file = "pg%u_out.pcap" % self.pg_index self._out_path = self.test.tempdir + "/" + self._out_file self._in_file = "pg%u_in.pcap" % self.pg_index -- cgit 1.2.3-korg