1import mmap
2import pytest
3
4from atf_python.ktest import BaseKernelTest
5from atf_python.sys.netlink.attrs import NlAttrU32
6
7
8M_NOWAIT = 1
9M_WAITOK = 2
10NS_WRITER_TYPE_MBUF = 0
11NS_WRITER_TYPE_BUF = 1
12NS_WRITER_TYPE_LBUF = 1
13
14MHLEN = 160
15MCLBYTES = 2048  # XXX: may differ on some archs?
16MJUMPAGESIZE = mmap.PAGESIZE
17MJUM9BYTES = 9 * 1024
18MJUM16BYTES = 16 * 1024
19
20
21class TestNetlinkMessageWriter(BaseKernelTest):
22    KTEST_MODULE_NAME = "ktest_netlink_message_writer"
23
24    @pytest.mark.parametrize(
25        "malloc_flags",
26        [
27            pytest.param(M_NOWAIT, id="NOWAIT"),
28            pytest.param(M_WAITOK, id="WAITOK"),
29        ],
30    )
31    @pytest.mark.parametrize(
32        "writer_type",
33        [
34            pytest.param(NS_WRITER_TYPE_MBUF, id="MBUF"),
35            pytest.param(NS_WRITER_TYPE_BUF, id="BUF"),
36        ],
37    )
38    @pytest.mark.parametrize(
39        "sz",
40        [
41            pytest.param([160, 160], id="MHLEN"),
42            pytest.param([MCLBYTES, MCLBYTES], id="MCLBYTES"),
43        ],
44    )
45    def test_mbuf_writer_allocation(self, sz, writer_type, malloc_flags):
46        """override to parametrize"""
47
48        test_meta = [
49            NlAttrU32(1, sz[0]),  # size
50            NlAttrU32(2, sz[1]),  # expected_avail
51            NlAttrU32(4, writer_type),
52            NlAttrU32(5, malloc_flags),
53        ]
54        self.runtest(test_meta)
55
56    @pytest.mark.parametrize(
57        "malloc_flags",
58        [
59            pytest.param(M_NOWAIT, id="NOWAIT"),
60            pytest.param(M_WAITOK, id="WAITOK"),
61        ],
62    )
63    @pytest.mark.parametrize(
64        "sz",
65        [
66            pytest.param([160, 160, 1], id="MHLEN"),
67            pytest.param([MCLBYTES, MCLBYTES, 1], id="MCLBYTES"),
68            pytest.param([MCLBYTES + 1, MCLBYTES + 1, 2], id="MCLBYTES_MHLEN"),
69            pytest.param([MCLBYTES + 256, MCLBYTES * 2, 2], id="MCLBYTESx2"),
70        ],
71    )
72    def test_mbuf_chain_allocation(self, sz, malloc_flags):
73        test_meta = [
74            NlAttrU32(1, sz[0]),  # size
75            NlAttrU32(2, sz[1]),  # expected_avail
76            NlAttrU32(3, sz[2]),  # expected_count
77            NlAttrU32(5, malloc_flags),
78        ]
79        self.runtest(test_meta)
80