| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "netif-sriov.h" |
| 5 |
#include "networkd-forward.h" |
| 6 |
|
| 7 |
typedef int (*request_process_func_t)(Request *req, Link *link, void *userdata); |
| 8 |
typedef int (*request_netlink_handler_t)(sd_netlink *nl, sd_netlink_message *m, Request *req, Link *link, void *userdata); |
| 9 |
|
| 10 |
typedef enum RequestType { |
| 11 |
REQUEST_TYPE_ACTIVATE_LINK, |
| 12 |
REQUEST_TYPE_ADDRESS, |
| 13 |
REQUEST_TYPE_ADDRESS_LABEL, |
| 14 |
REQUEST_TYPE_BRIDGE_FDB, |
| 15 |
REQUEST_TYPE_BRIDGE_MDB, |
| 16 |
REQUEST_TYPE_DHCP_RELAY, |
| 17 |
REQUEST_TYPE_DHCP_SERVER, |
| 18 |
REQUEST_TYPE_DHCP4_CLIENT, |
| 19 |
REQUEST_TYPE_DHCP6_CLIENT, |
| 20 |
REQUEST_TYPE_IPV6_PROXY_NDP, |
| 21 |
REQUEST_TYPE_NDISC, |
| 22 |
REQUEST_TYPE_NEIGHBOR, |
| 23 |
REQUEST_TYPE_NETDEV_INDEPENDENT, |
| 24 |
REQUEST_TYPE_NETDEV_STACKED, |
| 25 |
REQUEST_TYPE_NEXTHOP, |
| 26 |
REQUEST_TYPE_RADV, |
| 27 |
REQUEST_TYPE_ROUTE, |
| 28 |
REQUEST_TYPE_ROUTING_POLICY_RULE, |
| 29 |
REQUEST_TYPE_SET_LINK_ADDRESS_GENERATION_MODE, /* Setting IPv6LL address generation mode. */ |
| 30 |
REQUEST_TYPE_SET_LINK_BOND, /* Setting bond configs. */ |
| 31 |
REQUEST_TYPE_SET_LINK_BRIDGE, /* Setting bridge configs. */ |
| 32 |
REQUEST_TYPE_SET_LINK_BRIDGE_VLAN, /* Setting bridge VLAN configs. */ |
| 33 |
REQUEST_TYPE_DEL_LINK_BRIDGE_VLAN, /* Removing bridge VLAN configs. */ |
| 34 |
REQUEST_TYPE_SET_LINK_CAN, /* Setting CAN interface configs. */ |
| 35 |
REQUEST_TYPE_SET_LINK_FLAGS, /* Setting IFF_NOARP or friends. */ |
| 36 |
REQUEST_TYPE_SET_LINK_GROUP, /* Setting interface group. */ |
| 37 |
REQUEST_TYPE_SET_LINK_IPOIB, /* Setting IPoIB configs. */ |
| 38 |
REQUEST_TYPE_SET_LINK_MAC, /* Setting MAC address. */ |
| 39 |
REQUEST_TYPE_SET_LINK_MASTER, /* Setting IFLA_MASTER. */ |
| 40 |
REQUEST_TYPE_SET_LINK_MTU, /* Setting MTU. */ |
| 41 |
_REQUEST_TYPE_SRIOV_BASE, |
| 42 |
REQUEST_TYPE_SRIOV_VF_MAC = _REQUEST_TYPE_SRIOV_BASE + SR_IOV_VF_MAC, |
| 43 |
REQUEST_TYPE_SRIOV_VF_SPOOFCHK = _REQUEST_TYPE_SRIOV_BASE + SR_IOV_VF_SPOOFCHK, |
| 44 |
REQUEST_TYPE_SRIOV_VF_RSS_QUERY_EN = _REQUEST_TYPE_SRIOV_BASE + SR_IOV_VF_RSS_QUERY_EN, |
| 45 |
REQUEST_TYPE_SRIOV_VF_TRUST = _REQUEST_TYPE_SRIOV_BASE + SR_IOV_VF_TRUST, |
| 46 |
REQUEST_TYPE_SRIOV_VF_LINK_STATE = _REQUEST_TYPE_SRIOV_BASE + SR_IOV_VF_LINK_STATE, |
| 47 |
REQUEST_TYPE_SRIOV_VF_VLAN_LIST = _REQUEST_TYPE_SRIOV_BASE + SR_IOV_VF_VLAN_LIST, |
| 48 |
REQUEST_TYPE_TC_CLASS, |
| 49 |
REQUEST_TYPE_TC_QDISC, |
| 50 |
REQUEST_TYPE_UP_DOWN, |
| 51 |
_REQUEST_TYPE_MAX, |
| 52 |
_REQUEST_TYPE_INVALID = -EINVAL, |
| 53 |
} RequestType; |
| 54 |
|
| 55 |
typedef struct Request { |
| 56 |
unsigned n_ref; |
| 57 |
|
| 58 |
Manager *manager; /* must be non-NULL */ |
| 59 |
Link *link; /* can be NULL */ |
| 60 |
|
| 61 |
RequestType type; |
| 62 |
|
| 63 |
/* Target object, e.g. Address, Route, NetDev, and so on. */ |
| 64 |
void *userdata; |
| 65 |
/* freeing userdata when the request is completed or failed. */ |
| 66 |
mfree_func_t free_func; |
| 67 |
|
| 68 |
/* hash and compare functions for userdata, used for dedup requests. */ |
| 69 |
hash_func_t hash_func; |
| 70 |
compare_func_t compare_func; |
| 71 |
|
| 72 |
/* Checks the request dependencies, and then processes this request, e.g. call address_configure(). |
| 73 |
* Return 1 when processed, 0 when its dependencies not resolved, and negative errno on failure. */ |
| 74 |
request_process_func_t process; |
| 75 |
|
| 76 |
/* incremented when requested, decremented when request is completed or failed. */ |
| 77 |
unsigned *counter; |
| 78 |
/* called in netlink handler, the 'counter' is decremented before this is called. |
| 79 |
* If this is specified, then the 'process' function must increment the reference of this |
| 80 |
* request, and pass this request to the netlink_call_async(), and set the destroy function |
| 81 |
* to the slot. */ |
| 82 |
request_netlink_handler_t netlink_handler; |
| 83 |
|
| 84 |
bool waiting_reply; |
| 85 |
} Request; |
| 86 |
|
| 87 |
DECLARE_TRIVIAL_REF_UNREF_FUNC(Request, request); |
| 88 |
DEFINE_TRIVIAL_CLEANUP_FUNC(Request*, request_unref); |
| 89 |
|
| 90 |
void request_detach(Request *req); |
| 91 |
|
| 92 |
int netdev_queue_request( |
| 93 |
NetDev *netdev, |
| 94 |
request_process_func_t process, |
| 95 |
Request **ret); |
| 96 |
|
| 97 |
int link_queue_request_full( |
| 98 |
Link *link, |
| 99 |
RequestType type, |
| 100 |
void *userdata, |
| 101 |
mfree_func_t free_func, |
| 102 |
hash_func_t hash_func, |
| 103 |
compare_func_t compare_func, |
| 104 |
request_process_func_t process, |
| 105 |
unsigned *counter, |
| 106 |
request_netlink_handler_t netlink_handler, |
| 107 |
Request **ret); |
| 108 |
|
| 109 |
int manager_queue_request_full( |
| 110 |
Manager *manager, |
| 111 |
RequestType type, |
| 112 |
void *userdata, |
| 113 |
mfree_func_t free_func, |
| 114 |
hash_func_t hash_func, |
| 115 |
compare_func_t compare_func, |
| 116 |
request_process_func_t process, |
| 117 |
unsigned *counter, |
| 118 |
request_netlink_handler_t netlink_handler, |
| 119 |
Request **ret); |
| 120 |
|
| 121 |
int link_requeue_request(Link *link, Request *req, void *userdata, Request **ret); |
| 122 |
|
| 123 |
static inline int link_queue_request( |
| 124 |
Link *link, |
| 125 |
RequestType type, |
| 126 |
request_process_func_t process, |
| 127 |
Request **ret) { |
| 128 |
|
| 129 |
return link_queue_request_full(link, type, NULL, NULL, NULL, NULL, |
| 130 |
process, NULL, NULL, ret); |
| 131 |
} |
| 132 |
|
| 133 |
#define link_queue_request_safe(link, type, userdata, free_func, hash_func, compare_func, process, counter, netlink_handler, ret) \ |
| 134 |
({ \ |
| 135 |
typeof(userdata) (*_f)(typeof(userdata)) = (free_func); \ |
| 136 |
void (*_h)(const typeof(*userdata)*, struct siphash*) = (hash_func); \ |
| 137 |
int (*_c)(const typeof(*userdata)*, const typeof(*userdata)*) = (compare_func); \ |
| 138 |
int (*_p)(Request*, Link*, typeof(userdata)) = (process); \ |
| 139 |
int (*_n)(sd_netlink*, sd_netlink_message*, Request*, Link*, typeof(userdata)) = (netlink_handler); \ |
| 140 |
\ |
| 141 |
link_queue_request_full(link, type, userdata, \ |
| 142 |
(mfree_func_t) _f, \ |
| 143 |
(hash_func_t) _h, \ |
| 144 |
(compare_func_t) _c, \ |
| 145 |
(request_process_func_t) _p, \ |
| 146 |
counter, \ |
| 147 |
(request_netlink_handler_t) _n, \ |
| 148 |
ret); \ |
| 149 |
}) |
| 150 |
|
| 151 |
int manager_process_requests(Manager *manager); |
| 152 |
int request_call_netlink_async(sd_netlink *nl, sd_netlink_message *m, Request *req); |
| 153 |
|
| 154 |
DECLARE_STRING_TABLE_LOOKUP_TO_STRING(request_type, RequestType); |
| 155 |
|
| 156 |
typedef struct RemoveRequest RemoveRequest; |
| 157 |
typedef int (*remove_request_netlink_handler_t)(sd_netlink *nl, sd_netlink_message *m, RemoveRequest *req); |
| 158 |
|
| 159 |
struct RemoveRequest { |
| 160 |
Manager *manager; |
| 161 |
Link *link; |
| 162 |
void *userdata; /* e.g. Address */ |
| 163 |
mfree_func_t unref_func; /* e.g. address_unref() */ |
| 164 |
sd_netlink *netlink; |
| 165 |
sd_netlink_message *message; |
| 166 |
remove_request_netlink_handler_t netlink_handler; |
| 167 |
}; |
| 168 |
|
| 169 |
int remove_request_add( |
| 170 |
Manager *manager, |
| 171 |
Link *link, |
| 172 |
void *userdata, /* This is unref()ed when the call failed. */ |
| 173 |
mfree_func_t unref_func, |
| 174 |
sd_netlink *netlink, |
| 175 |
sd_netlink_message *message, |
| 176 |
remove_request_netlink_handler_t netlink_handler); |
| 177 |
|
| 178 |
#define _remove_request_add(manager, link, data, name, nl, m, handler) \ |
| 179 |
({ \ |
| 180 |
typeof(*data) *_data = (data); \ |
| 181 |
int _r; \ |
| 182 |
\ |
| 183 |
_r = remove_request_add(manager, link, _data, \ |
| 184 |
(mfree_func_t) name##_unref, \ |
| 185 |
nl, m, handler); \ |
| 186 |
if (_r > 0) \ |
| 187 |
name##_ref(_data); \ |
| 188 |
_r; \ |
| 189 |
}) |
| 190 |
|
| 191 |
#define link_remove_request_add(link, data, name, nl, m, handler) \ |
| 192 |
({ \ |
| 193 |
Link *_link = (link); \ |
| 194 |
\ |
| 195 |
_remove_request_add(_link->manager, _link, data, name, \ |
| 196 |
nl, m, handler); \ |
| 197 |
}) |
| 198 |
|
| 199 |
#define manager_remove_request_add(manager, data, name, nl, m, handler) \ |
| 200 |
_remove_request_add(manager, NULL, data, name, nl, m, handler) |
| 201 |
|
| 202 |
int manager_process_remove_requests(Manager *manager); |