| 1 |
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) |
| 2 |
# Copyright (c) 2020 Nvidia All rights reserved. See COPYING file |
| 3 |
""" |
| 4 |
Test module for pyverbs' flow module. |
| 5 |
""" |
| 6 |
from tests.base import RDMATestCase, RawResources, PyverbsRDMAError |
| 7 |
from pyverbs.spec import EthSpec, Ipv4ExtSpec, Ipv6Spec, TcpUdpSpec |
| 8 |
from tests.utils import requires_cap_net_raw, PacketConsts, requires_eth |
| 9 |
from pyverbs.flow import FlowAttr, Flow |
| 10 |
from pyverbs.libibverbs_enums import ibv_flow_spec_type |
| 11 |
import tests.utils as u |
| 12 |
import unittest |
| 13 |
import socket |
| 14 |
import errno |
| 15 |
|
| 16 |
|
| 17 |
class FlowRes(RawResources): |
| 18 |
def __init__(self, dev_name, ib_port, gid_index): |
| 19 |
""" |
| 20 |
Initialize Flow resources based on Raw resources that include Raw QP. |
| 21 |
:param dev_name: Device name to be used |
| 22 |
:param ib_port: IB port of the device to use |
| 23 |
:param gid_index: Which GID index to use |
| 24 |
""" |
| 25 |
super().__init__(dev_name=dev_name, ib_port=ib_port, |
| 26 |
gid_index=gid_index) |
| 27 |
|
| 28 |
@requires_cap_net_raw() |
| 29 |
@requires_eth() |
| 30 |
def create_qps(self): |
| 31 |
super().create_qps() |
| 32 |
|
| 33 |
@staticmethod |
| 34 |
def create_eth_spec(ether_type=PacketConsts.ETHER_TYPE_IPV4): |
| 35 |
""" |
| 36 |
Creates ethernet spec that matches on ethertype, source and destination |
| 37 |
macs. |
| 38 |
:param ether_type: IPv4 or IPv6 |
| 39 |
:return: created ethernet spec |
| 40 |
""" |
| 41 |
eth_spec = EthSpec(ether_type=ether_type, dst_mac=PacketConsts.DST_MAC) |
| 42 |
eth_spec.src_mac = PacketConsts.SRC_MAC |
| 43 |
eth_spec.src_mac_mask = PacketConsts.SRC_MAC |
| 44 |
return eth_spec |
| 45 |
|
| 46 |
def create_ip_spec(self, ver=PacketConsts.IP_V4, |
| 47 |
next_hdr=socket.IPPROTO_UDP): |
| 48 |
""" |
| 49 |
Creates IPv4 or IPv6 spec that matches on source and destination ips. |
| 50 |
:param ver: IP version |
| 51 |
:param next_hdr: Next header type |
| 52 |
:return: created IPv4 or IPv6 spec |
| 53 |
""" |
| 54 |
if ver == PacketConsts.IP_V4: |
| 55 |
ip_spec = Ipv4ExtSpec(src_ip=PacketConsts.SRC_IP, |
| 56 |
dst_ip=PacketConsts.DST_IP, proto=next_hdr) |
| 57 |
else: |
| 58 |
ip_spec = Ipv6Spec(src_ip=PacketConsts.SRC_IP6, |
| 59 |
dst_ip=PacketConsts.DST_IP6, next_hdr=next_hdr) |
| 60 |
return ip_spec |
| 61 |
|
| 62 |
@staticmethod |
| 63 |
def create_tcp_udp_spec(spec_type): |
| 64 |
""" |
| 65 |
Creates TcpUdp spec that matches on ethertype, source and destination |
| 66 |
macs. |
| 67 |
:param spec_type: Spec type TCP or UDP |
| 68 |
:return: TCP or UDP spec |
| 69 |
""" |
| 70 |
spec = TcpUdpSpec(spec_type, src_port=PacketConsts.SRC_PORT, |
| 71 |
dst_port=PacketConsts.DST_PORT) |
| 72 |
return spec |
| 73 |
|
| 74 |
def create_flow(self, specs=None): |
| 75 |
""" |
| 76 |
Creates flow to match on provided specs. |
| 77 |
:param specs: list of specs to match on |
| 78 |
:return: created flow |
| 79 |
""" |
| 80 |
specs = [] if specs is None else specs |
| 81 |
flow_attr = FlowAttr(num_of_specs=len(specs), port=self.ib_port) |
| 82 |
for spec in specs: |
| 83 |
flow_attr.specs.append(spec) |
| 84 |
try: |
| 85 |
flow = self._create_flow(flow_attr) |
| 86 |
except PyverbsRDMAError as ex: |
| 87 |
if ex.error_code == errno.EOPNOTSUPP: |
| 88 |
raise unittest.SkipTest('Flow creation is not supported') |
| 89 |
raise ex |
| 90 |
return flow |
| 91 |
|
| 92 |
def _create_flow(self, flow_attr): |
| 93 |
return Flow(self.qp, flow_attr) |
| 94 |
|
| 95 |
|
| 96 |
class FlowTest(RDMATestCase): |
| 97 |
""" |
| 98 |
Test various functionalities of the Flow class. |
| 99 |
""" |
| 100 |
def setUp(self): |
| 101 |
super().setUp() |
| 102 |
self.iters = 10 |
| 103 |
self.server = None |
| 104 |
self.client = None |
| 105 |
|
| 106 |
def flow_traffic(self, specs, l3=PacketConsts.IP_V4, |
| 107 |
l4=PacketConsts.UDP_PROTO): |
| 108 |
""" |
| 109 |
Execute raw ethernet traffic with given specs flow. |
| 110 |
:param specs: list of specs |
| 111 |
:param l3: Packet layer 3 type: 4 for IPv4 or 6 for IPv6 |
| 112 |
:param l4: Packet layer 4 type: 'tcp' or 'udp' |
| 113 |
:return: None |
| 114 |
""" |
| 115 |
self.flow = self.server.create_flow(specs) |
| 116 |
u.raw_traffic(self.client, self.server, self.iters, l3, l4) |
| 117 |
|
| 118 |
def test_eth_spec_flow_traffic(self): |
| 119 |
self.create_players(FlowRes) |
| 120 |
self.flow_traffic([self.server.create_eth_spec()]) |
| 121 |
|
| 122 |
def test_ipv4_spec_flow_traffic(self): |
| 123 |
self.create_players(FlowRes) |
| 124 |
if self.is_eth_and_has_roce_hw_bug(): |
| 125 |
raise unittest.SkipTest(f'Device {self.dev_name} doesn\'t support Ipv4ExtSpec') |
| 126 |
self.flow_traffic([self.server.create_ip_spec()]) |
| 127 |
|
| 128 |
def test_ipv6_spec_flow_traffic(self): |
| 129 |
self.create_players(FlowRes) |
| 130 |
eth_spec = self.server.create_eth_spec(PacketConsts.ETHER_TYPE_IPV6) |
| 131 |
if self.is_eth_and_has_roce_hw_bug(): |
| 132 |
raise unittest.SkipTest(f'Device {self.dev_name} doesn\'t support Ipv6Spec') |
| 133 |
ip_spec = self.server.create_ip_spec(PacketConsts.IP_V6) |
| 134 |
self.flow_traffic([eth_spec, ip_spec], PacketConsts.IP_V6) |
| 135 |
|
| 136 |
def test_udp_spec_flow_traffic(self): |
| 137 |
self.create_players(FlowRes) |
| 138 |
eth_spec = self.server.create_eth_spec() |
| 139 |
if self.is_eth_and_has_roce_hw_bug(): |
| 140 |
raise unittest.SkipTest(f'Device {self.dev_name} doesn\'t support Ipv4ExtSpec') |
| 141 |
ip_spec = self.server.create_ip_spec() |
| 142 |
udp_spec = self.server.create_tcp_udp_spec(ibv_flow_spec_type.IBV_FLOW_SPEC_UDP) |
| 143 |
self.flow_traffic([eth_spec, ip_spec, udp_spec], PacketConsts.IP_V4, |
| 144 |
PacketConsts.UDP_PROTO) |
| 145 |
|
| 146 |
def test_tcp_spec_flow_traffic(self): |
| 147 |
self.create_players(FlowRes) |
| 148 |
eth_spec = self.server.create_eth_spec(PacketConsts.ETHER_TYPE_IPV6) |
| 149 |
if self.is_eth_and_has_roce_hw_bug(): |
| 150 |
raise unittest.SkipTest(f'Device {self.dev_name} doesn\'t support Ipv6Spec') |
| 151 |
ip_spec = self.server.create_ip_spec(PacketConsts.IP_V6, |
| 152 |
socket.IPPROTO_TCP) |
| 153 |
tcp_spec = self.server.create_tcp_udp_spec(ibv_flow_spec_type.IBV_FLOW_SPEC_TCP) |
| 154 |
self.flow_traffic([eth_spec, ip_spec, tcp_spec], PacketConsts.IP_V6, |
| 155 |
PacketConsts.TCP_PROTO) |