| 1 |
# SPDX-License-Identifier: (GPL-2.0 OR Linux-OpenIB) |
| 2 |
# Copyright (c) 2019 Mellanox Technologies, Inc. All rights reserved. See COPYING file |
| 3 |
# Copyright 2020 Amazon.com, Inc. or its affiliates. All rights reserved. |
| 4 |
""" |
| 5 |
Test module for pyverbs' cq module. |
| 6 |
|
| 7 |
""" |
| 8 |
import unittest |
| 9 |
import errno |
| 10 |
import time |
| 11 |
|
| 12 |
from tests.base import PyverbsAPITestCase, RDMATestCase, UDResources |
| 13 |
from pyverbs.pyverbs_error import PyverbsRDMAError |
| 14 |
from pyverbs.base import PyverbsRDMAErrno |
| 15 |
from pyverbs.cq import CompChannel, CQ |
| 16 |
import tests.irdma_base as irdma |
| 17 |
from pyverbs.qp import QPCap |
| 18 |
import pyverbs.device as d |
| 19 |
import tests.utils as u |
| 20 |
|
| 21 |
|
| 22 |
class CQUDResources(UDResources): |
| 23 |
def __init__(self, dev_name, ib_port, gid_index, cq_depth=None): |
| 24 |
self.cq_depth = cq_depth |
| 25 |
super().__init__(dev_name, ib_port, gid_index) |
| 26 |
|
| 27 |
def create_cq(self): |
| 28 |
""" |
| 29 |
Initializes self.cq with a CQ of depth <num_msgs> - defined by each |
| 30 |
test. |
| 31 |
:return: None |
| 32 |
""" |
| 33 |
cq_depth = self.cq_depth if self.cq_depth is not None else self.num_msgs |
| 34 |
self.cq = CQ(self.ctx, cq_depth, None, None, 0) |
| 35 |
|
| 36 |
def create_qp_cap(self): |
| 37 |
return QPCap(max_recv_wr=self.num_msgs, max_send_wr=10) |
| 38 |
|
| 39 |
|
| 40 |
class CQAPITest(PyverbsAPITestCase): |
| 41 |
""" |
| 42 |
Test the API of the CQ class. |
| 43 |
""" |
| 44 |
def setUp(self): |
| 45 |
super().setUp() |
| 46 |
|
| 47 |
def test_create_cq(self): |
| 48 |
for cq_size in [1, self.attr.max_cqe/2, self.attr.max_cqe]: |
| 49 |
for comp_vector in range(0, min(2, self.ctx.num_comp_vectors)): |
| 50 |
try: |
| 51 |
cq = CQ(self.ctx, cq_size, None, None, comp_vector) |
| 52 |
cq.close() |
| 53 |
except PyverbsRDMAError as ex: |
| 54 |
cq_attr = f'cq_size={cq_size}, comp_vector={comp_vector}' |
| 55 |
raise PyverbsRDMAErrno(f'Failed to create a CQ with {cq_attr}') |
| 56 |
|
| 57 |
# Create CQ with Max value of comp_vector. |
| 58 |
max_cqs_comp_vector = self.ctx.num_comp_vectors - 1 |
| 59 |
cq = CQ(self.ctx, self.ctx.num_comp_vectors, None, None, max_cqs_comp_vector) |
| 60 |
|
| 61 |
|
| 62 |
def test_create_cq_with_comp_channel(self): |
| 63 |
for cq_size in [1, self.attr.max_cqe/2, self.attr.max_cqe]: |
| 64 |
try: |
| 65 |
cc = CompChannel(self.ctx) |
| 66 |
CQ(self.ctx, cq_size, None, cc, 0) |
| 67 |
cc.close() |
| 68 |
except PyverbsRDMAError as ex: |
| 69 |
if ex.error_code == errno.EOPNOTSUPP: |
| 70 |
raise unittest.SkipTest(f'CQ with completion channel is not supported') |
| 71 |
|
| 72 |
def test_create_cq_bad_flow(self): |
| 73 |
""" |
| 74 |
Test ibv_create_cq() with wrong comp_vector / number of cqes |
| 75 |
""" |
| 76 |
with self.assertRaises(PyverbsRDMAError) as ex: |
| 77 |
CQ(self.ctx, self.attr.max_cqe + 1, None, None, 0) |
| 78 |
self.assertEqual(ex.exception.error_code, errno.EINVAL) |
| 79 |
|
| 80 |
with self.assertRaises(PyverbsRDMAError) as ex: |
| 81 |
CQ(self.ctx, 100, None, None, self.ctx.num_comp_vectors + 1) |
| 82 |
self.assertEqual(ex.exception.error_code, errno.EINVAL) |
| 83 |
|
| 84 |
|
| 85 |
class CQTest(RDMATestCase): |
| 86 |
""" |
| 87 |
Test various functionalities of the CQ class. |
| 88 |
""" |
| 89 |
def setUp(self): |
| 90 |
super().setUp() |
| 91 |
self.iters = 10 |
| 92 |
self.server = None |
| 93 |
self.client = None |
| 94 |
|
| 95 |
def test_resize_cq(self): |
| 96 |
""" |
| 97 |
Test resize CQ, start with specific value and then increase and decrease |
| 98 |
the CQ size. The test also check bad flow of decrease the CQ size when |
| 99 |
there are more completions on it than the new value. |
| 100 |
""" |
| 101 |
self.create_players(CQUDResources, cq_depth=3) |
| 102 |
# Decrease the CQ size. |
| 103 |
new_cq_size = 1 |
| 104 |
try: |
| 105 |
self.client.cq.resize(new_cq_size) |
| 106 |
except PyverbsRDMAError as ex: |
| 107 |
if ex.error_code == errno.EOPNOTSUPP: |
| 108 |
raise unittest.SkipTest('Resize CQ is not supported') |
| 109 |
raise ex |
| 110 |
self.assertTrue(self.client.cq.cqe >= new_cq_size, |
| 111 |
f'The actual CQ size ({self.client.cq.cqe}) is less ' |
| 112 |
'than guaranteed ({new_cq_size})') |
| 113 |
|
| 114 |
# Increase the CQ size. |
| 115 |
new_cq_size = 7 |
| 116 |
post_send_num = new_cq_size - 1 |
| 117 |
self.client.cq.resize(new_cq_size) |
| 118 |
self.assertTrue(self.client.cq.cqe >= new_cq_size, |
| 119 |
f'The actual CQ size ({self.client.cq.cqe}) is less ' |
| 120 |
'than guaranteed ({new_cq_size})') |
| 121 |
|
| 122 |
# Fill the CQ entries except one for avoid cq_overrun warnings. |
| 123 |
send_wr, _ = u.get_send_elements(self.client, False) |
| 124 |
ah_client = u.get_global_ah(self.client, self.gid_index, self.ib_port) |
| 125 |
for i in range(post_send_num): |
| 126 |
u.send(self.client, send_wr, ah=ah_client) |
| 127 |
|
| 128 |
# Give a chance for some sends to be processed completely. |
| 129 |
time.sleep(1) |
| 130 |
# Decrease the CQ size to less than the CQ unpolled entries. |
| 131 |
new_cq_size = 1 |
| 132 |
try: |
| 133 |
self.client.cq.resize(new_cq_size) |
| 134 |
except PyverbsRDMAError as ex: |
| 135 |
self.assertEqual(ex.error_code, errno.EINVAL) |
| 136 |
finally: |
| 137 |
for i in range(post_send_num): |
| 138 |
u.poll_cq(self.client.cq) |