summaryrefslogtreecommitdiffstats
path: root/src/network/netdev/ipoib.c
blob: 5dd9286d57f5407c909440035c5e15ddd6e74103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* SPDX-License-Identifier: LGPL-2.1-or-later */

#include <linux/if_arp.h>
#include <linux/if_link.h>

#include "ipoib.h"
#include "networkd-network.h"
#include "parse-util.h"
#include "string-table.h"

assert_cc((int) IP_OVER_INFINIBAND_MODE_DATAGRAM  == (int) IPOIB_MODE_DATAGRAM);
assert_cc((int) IP_OVER_INFINIBAND_MODE_CONNECTED == (int) IPOIB_MODE_CONNECTED);

static void netdev_ipoib_init(NetDev *netdev) {
        IPoIB *ipoib;

        assert(netdev);

        ipoib = IPOIB(netdev);

        assert(ipoib);

        ipoib->mode = _IP_OVER_INFINIBAND_MODE_INVALID;
        ipoib->umcast = -1;
}

static int netdev_ipoib_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
        IPoIB *ipoib;
        int r;

        assert(netdev);
        assert(link);
        assert(m);

        ipoib = IPOIB(netdev);

        assert(ipoib);

        if (ipoib->pkey > 0) {
                r = sd_netlink_message_append_u16(m, IFLA_IPOIB_PKEY, ipoib->pkey);
                if (r < 0)
                        return r;
        }

        if (ipoib->mode >= 0) {
                r = sd_netlink_message_append_u16(m, IFLA_IPOIB_MODE, ipoib->mode);
                if (r < 0)
                        return r;
        }

        if (ipoib->umcast >= 0) {
                r = sd_netlink_message_append_u16(m, IFLA_IPOIB_UMCAST, ipoib->umcast);
                if (r < 0)
                        return r;
        }

        return 0;
}

int ipoib_set_netlink_message(Link *link, sd_netlink_message *m) {
        int r;

        assert(link);
        assert(link->network);
        assert(m);

        r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK);
        if (r < 0)
                return r;

        r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
        if (r < 0)
                return r;

        r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind);
        if (r < 0)
                return r;

        if (link->network->ipoib_mode >= 0) {
                r = sd_netlink_message_append_u16(m, IFLA_IPOIB_MODE, link->network->ipoib_mode);
                if (r < 0)
                        return r;
        }

        if (link->network->ipoib_umcast >= 0) {
                r = sd_netlink_message_append_u16(m, IFLA_IPOIB_UMCAST, link->network->ipoib_umcast);
                if (r < 0)
                        return r;
        }

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return r;

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return r;

        return 0;
}

static const char * const ipoib_mode_table[_IP_OVER_INFINIBAND_MODE_MAX] = {
        [IP_OVER_INFINIBAND_MODE_DATAGRAM]  = "datagram",
        [IP_OVER_INFINIBAND_MODE_CONNECTED] = "connected",
};

DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(ipoib_mode, IPoIBMode);
DEFINE_CONFIG_PARSE_ENUM(config_parse_ipoib_mode, ipoib_mode, IPoIBMode, "Failed to parse IPoIB mode");

int config_parse_ipoib_pkey(
                const char *unit,
                const char *filename,
                unsigned line,
                const char *section,
                unsigned section_line,
                const char *lvalue,
                int ltype,
                const char *rvalue,
                void *data,
                void *userdata) {

        uint16_t u, *pkey = ASSERT_PTR(data);
        int r;

        assert(filename);
        assert(lvalue);
        assert(rvalue);

        if (isempty(rvalue)) {
                *pkey = 0; /* 0 means unset. */
                return 0;
        }

        r = safe_atou16(rvalue, &u);
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to parse IPoIB pkey '%s', ignoring assignment: %m",
                           rvalue);
                return 0;
        }
        if (IN_SET(u, 0, 0x8000)) {
                log_syntax(unit, LOG_WARNING, filename, line, 0,
                           "IPoIB pkey cannot be 0 nor 0x8000, ignoring assignment: %s",
                           rvalue);
                return 0;
        }

        *pkey = u;
        return 0;
}


const NetDevVTable ipoib_vtable = {
        .object_size = sizeof(IPoIB),
        .sections = NETDEV_COMMON_SECTIONS "IPoIB\0",
        .init = netdev_ipoib_init,
        .fill_message_create = netdev_ipoib_fill_message_create,
        .create_type = NETDEV_CREATE_STACKED,
        .iftype = ARPHRD_INFINIBAND,
        .generate_mac = true,
};