Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2017 6WIND S.A.
3 : : * Copyright 2017 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <errno.h>
7 : : #include <fcntl.h>
8 : : #include <inttypes.h>
9 : : #include <linux/sockios.h>
10 : : #include <linux/netlink.h>
11 : : #include <linux/rtnetlink.h>
12 : : #include <net/if.h>
13 : : #include <net/if_arp.h>
14 : : #include <netinet/ip.h>
15 : : #include <stdarg.h>
16 : : #include <stddef.h>
17 : : #include <stdlib.h>
18 : : #include <stdint.h>
19 : : #include <stdio.h>
20 : : #include <string.h>
21 : : #include <sys/ioctl.h>
22 : : #include <sys/queue.h>
23 : : #include <sys/socket.h>
24 : : #include <unistd.h>
25 : :
26 : : #include <rte_alarm.h>
27 : : #include <bus_driver.h>
28 : : #include <bus_vdev_driver.h>
29 : : #include <rte_common.h>
30 : : #include <dev_driver.h>
31 : : #include <rte_errno.h>
32 : : #include <rte_ethdev.h>
33 : : #include <rte_ether.h>
34 : : #include <rte_hypervisor.h>
35 : : #include <rte_kvargs.h>
36 : : #include <rte_log.h>
37 : : #include <rte_string_fns.h>
38 : :
39 : : #define VDEV_NETVSC_DRIVER net_vdev_netvsc
40 : : #define VDEV_NETVSC_DRIVER_NAME RTE_STR(VDEV_NETVSC_DRIVER)
41 : : #define VDEV_NETVSC_DRIVER_NAME_LEN 15
42 : : #define VDEV_NETVSC_ARG_IFACE "iface"
43 : : #define VDEV_NETVSC_ARG_MAC "mac"
44 : : #define VDEV_NETVSC_ARG_FORCE "force"
45 : : #define VDEV_NETVSC_ARG_IGNORE "ignore"
46 : : #define VDEV_NETVSC_PROBE_MS 1000
47 : :
48 : : #define NETVSC_CLASS_ID "{f8615163-df3e-46c5-913f-f2d2f965ed0e}"
49 : : #define NETVSC_MAX_ROUTE_LINE_SIZE 300
50 : :
51 [ - + ]: 252 : RTE_LOG_REGISTER_DEFAULT(vdev_netvsc_logtype, NOTICE);
52 : : #define RTE_LOGTYPE_VDEV_NETVSC vdev_netvsc_logtype
53 : :
54 : : #define DRV_LOG(level, ...) \
55 : : RTE_LOG_LINE(level, VDEV_NETVSC, __VA_ARGS__)
56 : :
57 : : /** Context structure for a vdev_netvsc instance. */
58 : : struct vdev_netvsc_ctx {
59 : : LIST_ENTRY(vdev_netvsc_ctx) entry; /**< Next entry in list. */
60 : : unsigned int id; /**< Unique ID. */
61 : : char name[64]; /**< Unique name. */
62 : : char devname[64]; /**< Fail-safe instance name. */
63 : : char devargs[256]; /**< Fail-safe device arguments. */
64 : : char if_name[IF_NAMESIZE]; /**< NetVSC netdevice name. */
65 : : unsigned int if_index; /**< NetVSC netdevice index. */
66 : : struct rte_ether_addr if_addr; /**< NetVSC MAC address. */
67 : : int pipe[2]; /**< Fail-safe communication pipe. */
68 : : char yield[256]; /**< PCI sub-device arguments. */
69 : : };
70 : :
71 : : /** Context list is common to all driver instances. */
72 : : static LIST_HEAD(, vdev_netvsc_ctx) vdev_netvsc_ctx_list =
73 : : LIST_HEAD_INITIALIZER(vdev_netvsc_ctx_list);
74 : :
75 : : /** Number of entries in context list. */
76 : : static unsigned int vdev_netvsc_ctx_count;
77 : :
78 : : /** Number of driver instances relying on context list. */
79 : : static unsigned int vdev_netvsc_ctx_inst;
80 : :
81 : : /**
82 : : * Destroy a vdev_netvsc context instance.
83 : : *
84 : : * @param ctx
85 : : * Context to destroy.
86 : : */
87 : : static void
88 : 0 : vdev_netvsc_ctx_destroy(struct vdev_netvsc_ctx *ctx)
89 : : {
90 [ # # ]: 0 : if (ctx->pipe[0] != -1)
91 : 0 : close(ctx->pipe[0]);
92 [ # # ]: 0 : if (ctx->pipe[1] != -1)
93 : 0 : close(ctx->pipe[1]);
94 : 0 : free(ctx);
95 : 0 : }
96 : :
97 : : /**
98 : : * Determine if a network interface is NetVSC.
99 : : *
100 : : * @param[in] iface
101 : : * Pointer to netdevice description structure (name and index).
102 : : *
103 : : * @return
104 : : * A nonzero value when interface is detected as NetVSC. In case of error,
105 : : * rte_errno is updated and 0 returned.
106 : : */
107 : : static int
108 : 0 : vdev_netvsc_iface_is_netvsc(const struct if_nameindex *iface)
109 : : {
110 : : static const char temp[] = "/sys/class/net/%s/device/class_id";
111 : : char path[sizeof(temp) + IF_NAMESIZE];
112 : : FILE *f;
113 : : int ret;
114 : 0 : int len = 0;
115 : :
116 [ # # ]: 0 : ret = snprintf(path, sizeof(path), temp, iface->if_name);
117 [ # # ]: 0 : if (ret == -1 || (size_t)ret >= sizeof(path)) {
118 : 0 : rte_errno = ENOBUFS;
119 : 0 : return 0;
120 : : }
121 : 0 : f = fopen(path, "r");
122 [ # # ]: 0 : if (!f) {
123 : 0 : rte_errno = errno;
124 : 0 : return 0;
125 : : }
126 : 0 : ret = fscanf(f, NETVSC_CLASS_ID "%n", &len);
127 [ # # ]: 0 : if (ret == EOF)
128 : 0 : rte_errno = errno;
129 : 0 : ret = len == (int)strlen(NETVSC_CLASS_ID);
130 : 0 : fclose(f);
131 : 0 : return ret;
132 : : }
133 : :
134 : : /**
135 : : * Iterate over system network interfaces.
136 : : *
137 : : * This function runs a given callback function for each netdevice found on
138 : : * the system.
139 : : *
140 : : * @param func
141 : : * Callback function pointer. List traversal is aborted when this function
142 : : * returns a nonzero value.
143 : : * @param is_netvsc
144 : : * Indicates the device type to iterate - netvsc or non-netvsc.
145 : : * @param ...
146 : : * Variable parameter list passed as @p va_list to @p func.
147 : : *
148 : : * @return
149 : : * 0 when the entire list is traversed successfully, a negative error code
150 : : * in case or failure, or the nonzero value returned by @p func when list
151 : : * traversal is aborted.
152 : : */
153 : : static int
154 : 0 : vdev_netvsc_foreach_iface(int (*func)(const struct if_nameindex *iface,
155 : : const struct rte_ether_addr *eth_addr,
156 : : va_list ap), int is_netvsc, ...)
157 : : {
158 : 0 : struct if_nameindex *iface = if_nameindex();
159 : 0 : int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
160 : : unsigned int i;
161 : : int ret = 0;
162 : :
163 [ # # ]: 0 : if (!iface) {
164 : : ret = -ENOBUFS;
165 : 0 : DRV_LOG(ERR, "cannot retrieve system network interfaces");
166 : 0 : goto error;
167 : : }
168 [ # # ]: 0 : if (s == -1) {
169 : 0 : ret = -errno;
170 : 0 : DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
171 : 0 : goto error;
172 : : }
173 [ # # ]: 0 : for (i = 0; iface[i].if_name; ++i) {
174 : : int is_netvsc_ret;
175 : : struct ifreq req;
176 : : struct rte_ether_addr eth_addr;
177 : : va_list ap;
178 : :
179 : 0 : is_netvsc_ret = vdev_netvsc_iface_is_netvsc(&iface[i]) ? 1 : 0;
180 [ # # ]: 0 : if (is_netvsc ^ is_netvsc_ret)
181 : 0 : continue;
182 : 0 : strlcpy(req.ifr_name, iface[i].if_name, sizeof(req.ifr_name));
183 [ # # ]: 0 : if (ioctl(s, SIOCGIFHWADDR, &req) == -1) {
184 : 0 : DRV_LOG(WARNING, "cannot retrieve information about"
185 : : " interface \"%s\": %s",
186 : : req.ifr_name, rte_strerror(errno));
187 : 0 : continue;
188 : : }
189 [ # # ]: 0 : if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER)
190 : 0 : continue;
191 : : memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
192 : : RTE_DIM(eth_addr.addr_bytes));
193 : 0 : va_start(ap, is_netvsc);
194 : 0 : ret = func(&iface[i], ð_addr, ap);
195 : 0 : va_end(ap);
196 [ # # ]: 0 : if (ret)
197 : : break;
198 : : }
199 : 0 : error:
200 [ # # ]: 0 : if (s != -1)
201 : 0 : close(s);
202 [ # # ]: 0 : if (iface)
203 : 0 : if_freenameindex(iface);
204 : 0 : return ret;
205 : : }
206 : :
207 : : /**
208 : : * Determine if a network interface has a route.
209 : : *
210 : : * @param[in] name
211 : : * Network device name.
212 : : * @param[in] family
213 : : * Address family: AF_INET for IPv4 or AF_INET6 for IPv6.
214 : : *
215 : : * @return
216 : : * 1 when interface has a route, negative errno value in case of error and
217 : : * 0 otherwise.
218 : : */
219 : : static int
220 : 0 : vdev_netvsc_has_route(const struct if_nameindex *iface,
221 : : const unsigned char family)
222 : : {
223 : : /*
224 : : * The implementation can be simpler by getifaddrs() function usage but
225 : : * it works for IPv6 only starting from glibc 2.3.3.
226 : : */
227 : : char buf[4096];
228 : : int len;
229 : : int ret = 0;
230 : : int res;
231 : : int sock;
232 : : struct nlmsghdr *retmsg = (struct nlmsghdr *)buf;
233 : : struct sockaddr_nl sa;
234 : : struct {
235 : : struct nlmsghdr nlhdr;
236 : : struct ifaddrmsg addrmsg;
237 : : } msg;
238 : :
239 [ # # # # ]: 0 : if (!iface || (family != AF_INET && family != AF_INET6)) {
240 : 0 : DRV_LOG(ERR, "%s", rte_strerror(EINVAL));
241 : 0 : return -EINVAL;
242 : : }
243 : 0 : sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
244 [ # # ]: 0 : if (sock == -1) {
245 : 0 : DRV_LOG(ERR, "cannot open socket: %s", rte_strerror(errno));
246 : 0 : return -errno;
247 : : }
248 : : memset(&sa, 0, sizeof(sa));
249 : 0 : sa.nl_family = AF_NETLINK;
250 : 0 : sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
251 : 0 : res = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
252 [ # # ]: 0 : if (res == -1) {
253 : 0 : ret = -errno;
254 : 0 : DRV_LOG(ERR, "cannot bind socket: %s", rte_strerror(errno));
255 : 0 : goto close;
256 : : }
257 : : memset(&msg, 0, sizeof(msg));
258 : 0 : msg.nlhdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
259 : 0 : msg.nlhdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
260 : 0 : msg.nlhdr.nlmsg_type = RTM_GETADDR;
261 : 0 : msg.nlhdr.nlmsg_pid = getpid();
262 : 0 : msg.addrmsg.ifa_family = family;
263 : 0 : msg.addrmsg.ifa_index = iface->if_index;
264 : 0 : res = send(sock, &msg, msg.nlhdr.nlmsg_len, 0);
265 [ # # ]: 0 : if (res == -1) {
266 : 0 : ret = -errno;
267 : 0 : DRV_LOG(ERR, "cannot send socket message: %s",
268 : : rte_strerror(errno));
269 : 0 : goto close;
270 : : }
271 : : memset(buf, 0, sizeof(buf));
272 : 0 : len = recv(sock, buf, sizeof(buf), 0);
273 [ # # ]: 0 : if (len == -1) {
274 : 0 : ret = -errno;
275 : 0 : DRV_LOG(ERR, "cannot receive socket message: %s",
276 : : rte_strerror(errno));
277 : 0 : goto close;
278 : : }
279 [ # # # # : 0 : while (NLMSG_OK(retmsg, (unsigned int)len)) {
# # ]
280 : : struct ifaddrmsg *retaddr =
281 : : (struct ifaddrmsg *)NLMSG_DATA(retmsg);
282 : :
283 [ # # ]: 0 : if (retaddr->ifa_family == family &&
284 [ # # ]: 0 : retaddr->ifa_index == iface->if_index) {
285 : 0 : struct rtattr *retrta = IFA_RTA(retaddr);
286 : 0 : int attlen = IFA_PAYLOAD(retmsg);
287 : :
288 [ # # # # : 0 : while (RTA_OK(retrta, attlen)) {
# # ]
289 [ # # ]: 0 : if (retrta->rta_type == IFA_ADDRESS) {
290 : : ret = 1;
291 : 0 : DRV_LOG(DEBUG, "interface %s has IP",
292 : : iface->if_name);
293 : 0 : goto close;
294 : : }
295 : 0 : retrta = RTA_NEXT(retrta, attlen);
296 : : }
297 : : }
298 : 0 : retmsg = NLMSG_NEXT(retmsg, len);
299 : : }
300 : 0 : close:
301 : 0 : close(sock);
302 : 0 : return ret;
303 : : }
304 : :
305 : : /**
306 : : * Retrieve network interface data from sysfs symbolic link.
307 : : *
308 : : * @param[out] buf
309 : : * Output data buffer.
310 : : * @param size
311 : : * Output buffer size.
312 : : * @param[in] if_name
313 : : * Netdevice name.
314 : : * @param[in] relpath
315 : : * Symbolic link path relative to netdevice sysfs entry.
316 : : *
317 : : * @return
318 : : * 0 on success, a negative error code otherwise.
319 : : */
320 : : static int
321 : 0 : vdev_netvsc_sysfs_readlink(char *buf, size_t size, const char *if_name,
322 : : const char *relpath)
323 : 0 : {
324 : : struct vdev_netvsc_ctx *ctx;
325 [ # # ]: 0 : char in[RTE_MAX(sizeof(ctx->yield), 256u)];
326 : : int ret;
327 : :
328 : : ret = snprintf(in, sizeof(in), "/sys/class/net/%s/%s",
329 : : if_name, relpath);
330 [ # # # # ]: 0 : if (ret == -1 || (size_t)ret >= sizeof(in))
331 : : return -ENOBUFS;
332 : 0 : ret = readlink(in, buf, size);
333 [ # # ]: 0 : if (ret == -1)
334 : 0 : return -errno;
335 [ # # ]: 0 : if ((size_t)ret >= size - 1)
336 : : return -ENOBUFS;
337 : 0 : buf[ret] = '\0';
338 : 0 : return 0;
339 : : }
340 : :
341 : : /**
342 : : * Probe a network interface to associate with vdev_netvsc context.
343 : : *
344 : : * This function determines if the network device matches the properties of
345 : : * the NetVSC interface associated with the vdev_netvsc context and
346 : : * communicates its bus address to the fail-safe PMD instance if so.
347 : : *
348 : : * It is normally used with vdev_netvsc_foreach_iface().
349 : : *
350 : : * @param[in] iface
351 : : * Pointer to netdevice description structure (name and index).
352 : : * @param[in] eth_addr
353 : : * MAC address associated with @p iface.
354 : : * @param ap
355 : : * Variable arguments list comprising:
356 : : *
357 : : * - struct vdev_netvsc_ctx *ctx:
358 : : * Context to associate network interface with.
359 : : *
360 : : * @return
361 : : * A nonzero value when interface matches, 0 otherwise or in case of
362 : : * error.
363 : : */
364 : : static int
365 : 0 : vdev_netvsc_device_probe(const struct if_nameindex *iface,
366 : : const struct rte_ether_addr *eth_addr,
367 : : va_list ap)
368 : 0 : {
369 : 0 : struct vdev_netvsc_ctx *ctx = va_arg(ap, struct vdev_netvsc_ctx *);
370 : 0 : char buf[RTE_MAX(sizeof(ctx->yield), 256u)];
371 : : const char *addr;
372 : : size_t len;
373 : : int ret;
374 : :
375 : : /* Skip non-matching or unwanted NetVSC interfaces. */
376 [ # # ]: 0 : if (ctx->if_index == iface->if_index) {
377 [ # # ]: 0 : if (!strcmp(ctx->if_name, iface->if_name))
378 : : return 0;
379 : 0 : DRV_LOG(DEBUG,
380 : : "NetVSC interface \"%s\" (index %u) renamed \"%s\"",
381 : : ctx->if_name, ctx->if_index, iface->if_name);
382 : 0 : strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
383 : 0 : return 0;
384 : : }
385 [ # # ]: 0 : if (!rte_is_same_ether_addr(eth_addr, &ctx->if_addr))
386 : : return 0;
387 : : /* Look for associated PCI device. */
388 : 0 : ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
389 : : "device/subsystem");
390 [ # # ]: 0 : if (ret)
391 : : return 0;
392 : 0 : addr = strrchr(buf, '/');
393 [ # # ]: 0 : addr = addr ? addr + 1 : buf;
394 [ # # ]: 0 : if (strcmp(addr, "pci"))
395 : : return 0;
396 : 0 : ret = vdev_netvsc_sysfs_readlink(buf, sizeof(buf), iface->if_name,
397 : : "device");
398 [ # # ]: 0 : if (ret)
399 : : return 0;
400 : 0 : addr = strrchr(buf, '/');
401 [ # # ]: 0 : addr = addr ? addr + 1 : buf;
402 : 0 : len = strlen(addr);
403 [ # # ]: 0 : if (!len)
404 : : return 0;
405 : : /* Send PCI device argument to fail-safe PMD instance. */
406 [ # # ]: 0 : if (strcmp(addr, ctx->yield))
407 : 0 : DRV_LOG(DEBUG, "associating PCI device \"%s\" with NetVSC"
408 : : " interface \"%s\" (index %u)", addr, ctx->if_name,
409 : : ctx->if_index);
410 : 0 : memmove(buf, addr, len + 1);
411 : : addr = buf;
412 : 0 : buf[len] = '\n';
413 : 0 : ret = write(ctx->pipe[1], addr, len + 1);
414 : 0 : buf[len] = '\0';
415 [ # # ]: 0 : if (ret == -1) {
416 [ # # ]: 0 : if (errno == EINTR || errno == EAGAIN)
417 : : return 1;
418 : 0 : DRV_LOG(WARNING, "cannot associate PCI device name \"%s\" with"
419 : : " interface \"%s\": %s", addr, ctx->if_name,
420 : : rte_strerror(errno));
421 : 0 : return 1;
422 : : }
423 [ # # ]: 0 : if ((size_t)ret != len + 1) {
424 : : /*
425 : : * Attempt to override previous partial write, no need to
426 : : * recover if that fails.
427 : : */
428 : 0 : ret = write(ctx->pipe[1], "\n", 1);
429 : : (void)ret;
430 : 0 : return 1;
431 : : }
432 : 0 : fsync(ctx->pipe[1]);
433 : : memcpy(ctx->yield, addr, len + 1);
434 : 0 : return 1;
435 : : }
436 : :
437 : : /**
438 : : * Alarm callback that regularly probes system network interfaces.
439 : : *
440 : : * This callback runs at a frequency determined by VDEV_NETVSC_PROBE_MS as
441 : : * long as an vdev_netvsc context instance exists.
442 : : *
443 : : * @param arg
444 : : * Ignored.
445 : : */
446 : : static void
447 : 0 : vdev_netvsc_alarm(__rte_unused void *arg)
448 : : {
449 : : struct vdev_netvsc_ctx *ctx;
450 : : int ret;
451 : :
452 [ # # ]: 0 : LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry) {
453 : 0 : ret = vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0,
454 : : ctx);
455 [ # # ]: 0 : if (ret < 0)
456 : : break;
457 : : }
458 [ # # ]: 0 : if (!vdev_netvsc_ctx_count)
459 : : return;
460 : 0 : ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
461 : : vdev_netvsc_alarm, NULL);
462 [ # # ]: 0 : if (ret < 0) {
463 : 0 : DRV_LOG(ERR, "unable to reschedule alarm callback: %s",
464 : : rte_strerror(-ret));
465 : : }
466 : : }
467 : :
468 : : /**
469 : : * Probe a NetVSC interface to generate a vdev_netvsc context from.
470 : : *
471 : : * This function instantiates vdev_netvsc contexts either for all NetVSC
472 : : * devices found on the system or only a subset provided as device
473 : : * arguments.
474 : : *
475 : : * It is normally used with vdev_netvsc_foreach_iface().
476 : : *
477 : : * @param[in] iface
478 : : * Pointer to netdevice description structure (name and index).
479 : : * @param[in] eth_addr
480 : : * MAC address associated with @p iface.
481 : : * @param ap
482 : : * Variable arguments list comprising:
483 : : *
484 : : * - const char *name:
485 : : * Name associated with current driver instance.
486 : : *
487 : : * - struct rte_kvargs *kvargs:
488 : : * Device arguments provided to current driver instance.
489 : : *
490 : : * - int force:
491 : : * Accept specified interface even if not detected as NetVSC.
492 : : *
493 : : * - unsigned int specified:
494 : : * Number of specific netdevices provided as device arguments.
495 : : *
496 : : * - unsigned int *matched:
497 : : * The number of specified netdevices matched by this function.
498 : : *
499 : : * @return
500 : : * A nonzero value when interface matches, 0 otherwise or in case of
501 : : * error.
502 : : */
503 : : static int
504 : 0 : vdev_netvsc_netvsc_probe(const struct if_nameindex *iface,
505 : : const struct rte_ether_addr *eth_addr,
506 : : va_list ap)
507 : : {
508 : 0 : const char *name = va_arg(ap, const char *);
509 : 0 : struct rte_kvargs *kvargs = va_arg(ap, struct rte_kvargs *);
510 : 0 : unsigned int specified = va_arg(ap, unsigned int);
511 : 0 : unsigned int *matched = va_arg(ap, unsigned int *);
512 : : unsigned int i;
513 : : struct vdev_netvsc_ctx *ctx;
514 : : int ret;
515 : :
516 : : /* Probe all interfaces when none are specified. */
517 [ # # ]: 0 : if (specified) {
518 [ # # ]: 0 : for (i = 0; i != kvargs->count; ++i) {
519 : : const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
520 : :
521 [ # # ]: 0 : if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE)) {
522 [ # # ]: 0 : if (!strcmp(pair->value, iface->if_name))
523 : : break;
524 [ # # ]: 0 : } else if (!strcmp(pair->key, VDEV_NETVSC_ARG_MAC)) {
525 : : struct rte_ether_addr tmp;
526 : :
527 [ # # ]: 0 : if (rte_ether_unformat_addr(pair->value, &tmp) != 0) {
528 : 0 : DRV_LOG(ERR,
529 : : "invalid MAC address format"
530 : : " \"%s\"",
531 : : pair->value);
532 : 0 : return -EINVAL;
533 : : }
534 [ # # ]: 0 : if (rte_is_same_ether_addr(eth_addr, &tmp))
535 : : break;
536 : : }
537 : : }
538 [ # # ]: 0 : if (i == kvargs->count)
539 : : return 0;
540 : 0 : ++(*matched);
541 : : }
542 : : /* Weed out interfaces already handled. */
543 [ # # ]: 0 : LIST_FOREACH(ctx, &vdev_netvsc_ctx_list, entry)
544 [ # # ]: 0 : if (ctx->if_index == iface->if_index)
545 : : break;
546 [ # # ]: 0 : if (ctx) {
547 [ # # ]: 0 : if (!specified)
548 : : return 0;
549 : 0 : DRV_LOG(WARNING,
550 : : "interface \"%s\" (index %u) is already handled,"
551 : : " skipping",
552 : : iface->if_name, iface->if_index);
553 : 0 : return 0;
554 : : }
555 : : /* Routed NetVSC should not be probed. */
556 [ # # # # ]: 0 : if (vdev_netvsc_has_route(iface, AF_INET) ||
557 : 0 : vdev_netvsc_has_route(iface, AF_INET6)) {
558 [ # # ]: 0 : if (!specified)
559 : : return 0;
560 : 0 : DRV_LOG(WARNING, "probably using routed NetVSC interface \"%s\""
561 : : " (index %u)", iface->if_name, iface->if_index);
562 : : }
563 : : /* Create interface context. */
564 : 0 : ctx = calloc(1, sizeof(*ctx));
565 [ # # ]: 0 : if (!ctx) {
566 : 0 : ret = -errno;
567 : 0 : DRV_LOG(ERR, "cannot allocate context for interface \"%s\": %s",
568 : : iface->if_name, rte_strerror(errno));
569 : 0 : goto error;
570 : : }
571 : 0 : ctx->id = vdev_netvsc_ctx_count;
572 : 0 : strlcpy(ctx->if_name, iface->if_name, sizeof(ctx->if_name));
573 : 0 : ctx->if_index = iface->if_index;
574 : 0 : ctx->if_addr = *eth_addr;
575 : 0 : ctx->pipe[0] = -1;
576 : 0 : ctx->pipe[1] = -1;
577 : 0 : ctx->yield[0] = '\0';
578 [ # # ]: 0 : if (pipe(ctx->pipe) == -1) {
579 : 0 : ret = -errno;
580 : 0 : DRV_LOG(ERR,
581 : : "cannot allocate control pipe for interface \"%s\": %s",
582 : : ctx->if_name, rte_strerror(errno));
583 : 0 : goto error;
584 : : }
585 [ # # ]: 0 : for (i = 0; i != RTE_DIM(ctx->pipe); ++i) {
586 : 0 : int flf = fcntl(ctx->pipe[i], F_GETFL);
587 : :
588 [ # # # # ]: 0 : if (flf != -1 &&
589 : 0 : fcntl(ctx->pipe[i], F_SETFL, flf | O_NONBLOCK) != -1)
590 : : continue;
591 : 0 : ret = -errno;
592 : 0 : DRV_LOG(ERR, "cannot toggle non-blocking flag on control file"
593 : : " descriptor #%u (%d): %s", i, ctx->pipe[i],
594 : : rte_strerror(errno));
595 : 0 : goto error;
596 : : }
597 : : /* Generate virtual device name and arguments. */
598 : : i = 0;
599 [ # # ]: 0 : ret = snprintf(ctx->name, sizeof(ctx->name), "%s_id%u",
600 : : name, ctx->id);
601 [ # # ]: 0 : if (ret == -1 || (size_t)ret >= sizeof(ctx->name))
602 : : ++i;
603 [ # # ]: 0 : ret = snprintf(ctx->devname, sizeof(ctx->devname), "net_failsafe_vsc%u",
604 : : ctx->id);
605 [ # # ]: 0 : if (ret == -1 || (size_t)ret >= sizeof(ctx->devname))
606 : 0 : ++i;
607 [ # # ]: 0 : ret = snprintf(ctx->devargs, sizeof(ctx->devargs),
608 : : "fd(%d),dev(net_tap_vsc%u,remote=%s)",
609 : : ctx->pipe[0], ctx->id, ctx->if_name);
610 [ # # ]: 0 : if (ret == -1 || (size_t)ret >= sizeof(ctx->devargs))
611 : 0 : ++i;
612 [ # # ]: 0 : if (i) {
613 : : ret = -ENOBUFS;
614 : 0 : DRV_LOG(ERR, "generated virtual device name or argument list"
615 : : " too long for interface \"%s\"", ctx->if_name);
616 : 0 : goto error;
617 : : }
618 : : /* Request virtual device generation. */
619 : 0 : DRV_LOG(DEBUG, "generating virtual device \"%s\" with arguments \"%s\"",
620 : : ctx->devname, ctx->devargs);
621 : 0 : vdev_netvsc_foreach_iface(vdev_netvsc_device_probe, 0, ctx);
622 : 0 : ret = rte_eal_hotplug_add("vdev", ctx->devname, ctx->devargs);
623 [ # # ]: 0 : if (ret < 0)
624 : 0 : goto error;
625 [ # # ]: 0 : LIST_INSERT_HEAD(&vdev_netvsc_ctx_list, ctx, entry);
626 : 0 : ++vdev_netvsc_ctx_count;
627 : 0 : DRV_LOG(DEBUG, "added NetVSC interface \"%s\" to context list",
628 : : ctx->if_name);
629 : 0 : return 0;
630 : 0 : error:
631 [ # # ]: 0 : if (ctx)
632 : 0 : vdev_netvsc_ctx_destroy(ctx);
633 : : return ret;
634 : : }
635 : :
636 : : /**
637 : : * Probe NetVSC interfaces.
638 : : *
639 : : * This function probes system netdevices according to the specified device
640 : : * arguments and starts a periodic alarm callback to notify the resulting
641 : : * fail-safe PMD instances of their sub-devices whereabouts.
642 : : *
643 : : * @param dev
644 : : * Virtual device context for driver instance.
645 : : *
646 : : * @return
647 : : * Always 0, even in case of errors.
648 : : */
649 : : static int
650 [ # # ]: 0 : vdev_netvsc_vdev_probe(struct rte_vdev_device *dev)
651 : : {
652 : : static const char *const vdev_netvsc_arg[] = {
653 : : VDEV_NETVSC_ARG_IFACE,
654 : : VDEV_NETVSC_ARG_MAC,
655 : : VDEV_NETVSC_ARG_FORCE,
656 : : VDEV_NETVSC_ARG_IGNORE,
657 : : NULL,
658 : : };
659 : : const char *name = rte_vdev_device_name(dev);
660 : : const char *args = rte_vdev_device_args(dev);
661 [ # # ]: 0 : struct rte_kvargs *kvargs = rte_kvargs_parse(args ? args : "",
662 : : vdev_netvsc_arg);
663 : : unsigned int specified = 0;
664 : 0 : unsigned int matched = 0;
665 : : int force = 0;
666 : : int ignore = 0;
667 : : unsigned int i;
668 : : int ret;
669 : :
670 : 0 : DRV_LOG(DEBUG, "invoked as \"%s\", using arguments \"%s\"", name, args);
671 : 0 : rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
672 [ # # ]: 0 : if (!kvargs) {
673 : 0 : DRV_LOG(ERR, "cannot parse arguments list");
674 : 0 : goto error;
675 : : }
676 [ # # ]: 0 : for (i = 0; i != kvargs->count; ++i) {
677 : : const struct rte_kvargs_pair *pair = &kvargs->pairs[i];
678 : :
679 [ # # ]: 0 : if (!strcmp(pair->key, VDEV_NETVSC_ARG_FORCE))
680 : 0 : force = !!atoi(pair->value);
681 [ # # ]: 0 : else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IGNORE))
682 : 0 : ignore = !!atoi(pair->value);
683 [ # # ]: 0 : else if (!strcmp(pair->key, VDEV_NETVSC_ARG_IFACE) ||
684 [ # # ]: 0 : !strcmp(pair->key, VDEV_NETVSC_ARG_MAC))
685 : 0 : ++specified;
686 : : }
687 [ # # ]: 0 : if (ignore)
688 : 0 : goto ignore;
689 [ # # ]: 0 : if (specified > 1) {
690 : 0 : DRV_LOG(ERR, "More than one way used to specify the netvsc"
691 : : " device.");
692 : 0 : goto error;
693 : : }
694 : : /* Gather interfaces. */
695 : 0 : ret = vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 1, name,
696 : : kvargs, specified, &matched);
697 [ # # ]: 0 : if (ret < 0)
698 : 0 : goto error;
699 [ # # # # ]: 0 : if (specified && matched < specified) {
700 [ # # ]: 0 : if (!force) {
701 : 0 : DRV_LOG(ERR, "Cannot find the specified netvsc device");
702 : 0 : goto error;
703 : : }
704 : : /* Try to force probing on non-netvsc specified device. */
705 [ # # ]: 0 : if (vdev_netvsc_foreach_iface(vdev_netvsc_netvsc_probe, 0, name,
706 : : kvargs, specified, &matched) < 0)
707 : 0 : goto error;
708 [ # # ]: 0 : if (matched < specified) {
709 : 0 : DRV_LOG(ERR, "Cannot find the specified device");
710 : 0 : goto error;
711 : : }
712 : 0 : DRV_LOG(WARNING, "non-netvsc device was probed as netvsc");
713 : : }
714 : 0 : error:
715 : 0 : ++vdev_netvsc_ctx_inst;
716 : 0 : ignore:
717 : 0 : rte_kvargs_free(kvargs);
718 : : /* Reset alarm if there are device context created */
719 [ # # ]: 0 : if (vdev_netvsc_ctx_count) {
720 : 0 : ret = rte_eal_alarm_set(VDEV_NETVSC_PROBE_MS * 1000,
721 : : vdev_netvsc_alarm, NULL);
722 [ # # ]: 0 : if (ret < 0)
723 : 0 : DRV_LOG(ERR, "unable to schedule alarm callback: %s",
724 : : rte_strerror(-ret));
725 : : }
726 : 0 : return 0;
727 : : }
728 : :
729 : : /**
730 : : * Remove driver instance.
731 : : *
732 : : * The alarm callback and underlying vdev_netvsc context instances are only
733 : : * destroyed after the last PMD instance is removed.
734 : : *
735 : : * @param dev
736 : : * Virtual device context for driver instance.
737 : : *
738 : : * @return
739 : : * Always 0.
740 : : */
741 : : static int
742 : 0 : vdev_netvsc_vdev_remove(__rte_unused struct rte_vdev_device *dev)
743 : : {
744 [ # # ]: 0 : if (--vdev_netvsc_ctx_inst)
745 : : return 0;
746 : 0 : rte_eal_alarm_cancel(vdev_netvsc_alarm, NULL);
747 [ # # ]: 0 : while (!LIST_EMPTY(&vdev_netvsc_ctx_list)) {
748 : : struct vdev_netvsc_ctx *ctx = LIST_FIRST(&vdev_netvsc_ctx_list);
749 : :
750 [ # # ]: 0 : LIST_REMOVE(ctx, entry);
751 : 0 : --vdev_netvsc_ctx_count;
752 : 0 : vdev_netvsc_ctx_destroy(ctx);
753 : : }
754 : : return 0;
755 : : }
756 : :
757 : : /** Virtual device descriptor. */
758 : : static struct rte_vdev_driver vdev_netvsc_vdev = {
759 : : .probe = vdev_netvsc_vdev_probe,
760 : : .remove = vdev_netvsc_vdev_remove,
761 : : };
762 : :
763 : 252 : RTE_PMD_REGISTER_VDEV(VDEV_NETVSC_DRIVER, vdev_netvsc_vdev);
764 : : RTE_PMD_REGISTER_ALIAS(VDEV_NETVSC_DRIVER, eth_vdev_netvsc);
765 : : RTE_PMD_REGISTER_PARAM_STRING(net_vdev_netvsc,
766 : : VDEV_NETVSC_ARG_IFACE "=<string> "
767 : : VDEV_NETVSC_ARG_MAC "=<string> "
768 : : VDEV_NETVSC_ARG_FORCE "=<int> "
769 : : VDEV_NETVSC_ARG_IGNORE "=<int>");
770 : :
771 : : /** Compare function for vdev find device operation. */
772 : : static int
773 : 0 : vdev_netvsc_cmp_rte_device(const struct rte_device *dev1,
774 : : __rte_unused const void *_dev2)
775 : : {
776 : 0 : return strncmp(dev1->devargs->name, VDEV_NETVSC_DRIVER_NAME,
777 : : VDEV_NETVSC_DRIVER_NAME_LEN);
778 : : }
779 : :
780 : : /**
781 : : * A callback called by vdev bus scan function to ensure this driver probing
782 : : * automatically in Hyper-V VM system unless it already exists in the
783 : : * devargs list.
784 : : */
785 : : static void
786 : 0 : vdev_netvsc_scan_callback(__rte_unused void *arg)
787 : : {
788 : : struct rte_device *dev;
789 : : struct rte_devargs *devargs;
790 : 0 : struct rte_bus *vbus = rte_bus_find_by_name("vdev");
791 : :
792 [ # # ]: 0 : RTE_EAL_DEVARGS_FOREACH("vdev", devargs)
793 [ # # ]: 0 : if (!strncmp(devargs->name, VDEV_NETVSC_DRIVER_NAME,
794 : : VDEV_NETVSC_DRIVER_NAME_LEN))
795 : : return;
796 : :
797 : 0 : dev = vbus->find_device(NULL, vdev_netvsc_cmp_rte_device,
798 : : VDEV_NETVSC_DRIVER_NAME);
799 [ # # ]: 0 : if (dev)
800 : : return;
801 [ # # ]: 0 : if (rte_devargs_add(RTE_DEVTYPE_VIRTUAL, VDEV_NETVSC_DRIVER_NAME))
802 : 0 : DRV_LOG(ERR, "unable to add netvsc devargs.");
803 : : }
804 : :
805 : : /** Initialize the custom scan. */
806 : 252 : RTE_INIT(vdev_netvsc_custom_scan_add)
807 : : {
808 [ - + ]: 252 : if (rte_hypervisor_get() == RTE_HYPERVISOR_HYPERV)
809 : 0 : rte_vdev_add_custom_scan(vdev_netvsc_scan_callback, NULL);
810 : 252 : }
|