net/mlx5: set VF MAC address from host
[dpdk.git] / drivers / net / mlx5 / mlx5_nl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 6WIND S.A.
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5
6 #include <errno.h>
7 #include <linux/if_link.h>
8 #include <linux/netlink.h>
9 #include <linux/rtnetlink.h>
10 #include <net/if.h>
11 #include <rdma/rdma_netlink.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <stdalign.h>
16 #include <string.h>
17 #include <sys/socket.h>
18 #include <unistd.h>
19
20 #include <rte_errno.h>
21 #include <rte_malloc.h>
22 #include <rte_hypervisor.h>
23
24 #include "mlx5.h"
25 #include "mlx5_utils.h"
26
27 /* Size of the buffer to receive kernel messages */
28 #define MLX5_NL_BUF_SIZE (32 * 1024)
29 /* Send buffer size for the Netlink socket */
30 #define MLX5_SEND_BUF_SIZE 32768
31 /* Receive buffer size for the Netlink socket */
32 #define MLX5_RECV_BUF_SIZE 32768
33
34 /** Parameters of VLAN devices created by driver. */
35 #define MLX5_VMWA_VLAN_DEVICE_PFX "evmlx"
36 /*
37  * Define NDA_RTA as defined in iproute2 sources.
38  *
39  * see in iproute2 sources file include/libnetlink.h
40  */
41 #ifndef MLX5_NDA_RTA
42 #define MLX5_NDA_RTA(r) \
43         ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ndmsg))))
44 #endif
45 /*
46  * Define NLMSG_TAIL as defined in iproute2 sources.
47  *
48  * see in iproute2 sources file include/libnetlink.h
49  */
50 #ifndef NLMSG_TAIL
51 #define NLMSG_TAIL(nmsg) \
52         ((struct rtattr *)(((char *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
53 #endif
54 /*
55  * The following definitions are normally found in rdma/rdma_netlink.h,
56  * however they are so recent that most systems do not expose them yet.
57  */
58 #ifndef HAVE_RDMA_NL_NLDEV
59 #define RDMA_NL_NLDEV 5
60 #endif
61 #ifndef HAVE_RDMA_NLDEV_CMD_GET
62 #define RDMA_NLDEV_CMD_GET 1
63 #endif
64 #ifndef HAVE_RDMA_NLDEV_CMD_PORT_GET
65 #define RDMA_NLDEV_CMD_PORT_GET 5
66 #endif
67 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_INDEX
68 #define RDMA_NLDEV_ATTR_DEV_INDEX 1
69 #endif
70 #ifndef HAVE_RDMA_NLDEV_ATTR_DEV_NAME
71 #define RDMA_NLDEV_ATTR_DEV_NAME 2
72 #endif
73 #ifndef HAVE_RDMA_NLDEV_ATTR_PORT_INDEX
74 #define RDMA_NLDEV_ATTR_PORT_INDEX 3
75 #endif
76 #ifndef HAVE_RDMA_NLDEV_ATTR_NDEV_INDEX
77 #define RDMA_NLDEV_ATTR_NDEV_INDEX 50
78 #endif
79
80 /* These are normally found in linux/if_link.h. */
81 #ifndef HAVE_IFLA_NUM_VF
82 #define IFLA_NUM_VF 21
83 #endif
84 #ifndef HAVE_IFLA_EXT_MASK
85 #define IFLA_EXT_MASK 29
86 #endif
87 #ifndef HAVE_IFLA_PHYS_SWITCH_ID
88 #define IFLA_PHYS_SWITCH_ID 36
89 #endif
90 #ifndef HAVE_IFLA_PHYS_PORT_NAME
91 #define IFLA_PHYS_PORT_NAME 38
92 #endif
93
94 /* Add/remove MAC address through Netlink */
95 struct mlx5_nl_mac_addr {
96         struct rte_ether_addr (*mac)[];
97         /**< MAC address handled by the device. */
98         int mac_n; /**< Number of addresses in the array. */
99 };
100
101 #define MLX5_NL_CMD_GET_IB_NAME (1 << 0)
102 #define MLX5_NL_CMD_GET_IB_INDEX (1 << 1)
103 #define MLX5_NL_CMD_GET_NET_INDEX (1 << 2)
104 #define MLX5_NL_CMD_GET_PORT_INDEX (1 << 3)
105
106 /** Data structure used by mlx5_nl_cmdget_cb(). */
107 struct mlx5_nl_ifindex_data {
108         const char *name; /**< IB device name (in). */
109         uint32_t flags; /**< found attribute flags (out). */
110         uint32_t ibindex; /**< IB device index (out). */
111         uint32_t ifindex; /**< Network interface index (out). */
112         uint32_t portnum; /**< IB device max port number (out). */
113 };
114
115 /**
116  * Opens a Netlink socket.
117  *
118  * @param protocol
119  *   Netlink protocol (e.g. NETLINK_ROUTE, NETLINK_RDMA).
120  *
121  * @return
122  *   A file descriptor on success, a negative errno value otherwise and
123  *   rte_errno is set.
124  */
125 int
126 mlx5_nl_init(int protocol)
127 {
128         int fd;
129         int sndbuf_size = MLX5_SEND_BUF_SIZE;
130         int rcvbuf_size = MLX5_RECV_BUF_SIZE;
131         struct sockaddr_nl local = {
132                 .nl_family = AF_NETLINK,
133         };
134         int ret;
135
136         fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
137         if (fd == -1) {
138                 rte_errno = errno;
139                 return -rte_errno;
140         }
141         ret = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int));
142         if (ret == -1) {
143                 rte_errno = errno;
144                 goto error;
145         }
146         ret = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int));
147         if (ret == -1) {
148                 rte_errno = errno;
149                 goto error;
150         }
151         ret = bind(fd, (struct sockaddr *)&local, sizeof(local));
152         if (ret == -1) {
153                 rte_errno = errno;
154                 goto error;
155         }
156         return fd;
157 error:
158         close(fd);
159         return -rte_errno;
160 }
161
162 /**
163  * Send a request message to the kernel on the Netlink socket.
164  *
165  * @param[in] nlsk_fd
166  *   Netlink socket file descriptor.
167  * @param[in] nh
168  *   The Netlink message send to the kernel.
169  * @param[in] ssn
170  *   Sequence number.
171  * @param[in] req
172  *   Pointer to the request structure.
173  * @param[in] len
174  *   Length of the request in bytes.
175  *
176  * @return
177  *   The number of sent bytes on success, a negative errno value otherwise and
178  *   rte_errno is set.
179  */
180 static int
181 mlx5_nl_request(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn, void *req,
182                 int len)
183 {
184         struct sockaddr_nl sa = {
185                 .nl_family = AF_NETLINK,
186         };
187         struct iovec iov[2] = {
188                 { .iov_base = nh, .iov_len = sizeof(*nh), },
189                 { .iov_base = req, .iov_len = len, },
190         };
191         struct msghdr msg = {
192                 .msg_name = &sa,
193                 .msg_namelen = sizeof(sa),
194                 .msg_iov = iov,
195                 .msg_iovlen = 2,
196         };
197         int send_bytes;
198
199         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
200         nh->nlmsg_seq = sn;
201         send_bytes = sendmsg(nlsk_fd, &msg, 0);
202         if (send_bytes < 0) {
203                 rte_errno = errno;
204                 return -rte_errno;
205         }
206         return send_bytes;
207 }
208
209 /**
210  * Send a message to the kernel on the Netlink socket.
211  *
212  * @param[in] nlsk_fd
213  *   The Netlink socket file descriptor used for communication.
214  * @param[in] nh
215  *   The Netlink message send to the kernel.
216  * @param[in] sn
217  *   Sequence number.
218  *
219  * @return
220  *   The number of sent bytes on success, a negative errno value otherwise and
221  *   rte_errno is set.
222  */
223 static int
224 mlx5_nl_send(int nlsk_fd, struct nlmsghdr *nh, uint32_t sn)
225 {
226         struct sockaddr_nl sa = {
227                 .nl_family = AF_NETLINK,
228         };
229         struct iovec iov = {
230                 .iov_base = nh,
231                 .iov_len = nh->nlmsg_len,
232         };
233         struct msghdr msg = {
234                 .msg_name = &sa,
235                 .msg_namelen = sizeof(sa),
236                 .msg_iov = &iov,
237                 .msg_iovlen = 1,
238         };
239         int send_bytes;
240
241         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
242         nh->nlmsg_seq = sn;
243         send_bytes = sendmsg(nlsk_fd, &msg, 0);
244         if (send_bytes < 0) {
245                 rte_errno = errno;
246                 return -rte_errno;
247         }
248         return send_bytes;
249 }
250
251 /**
252  * Receive a message from the kernel on the Netlink socket, following
253  * mlx5_nl_send().
254  *
255  * @param[in] nlsk_fd
256  *   The Netlink socket file descriptor used for communication.
257  * @param[in] sn
258  *   Sequence number.
259  * @param[in] cb
260  *   The callback function to call for each Netlink message received.
261  * @param[in, out] arg
262  *   Custom arguments for the callback.
263  *
264  * @return
265  *   0 on success, a negative errno value otherwise and rte_errno is set.
266  */
267 static int
268 mlx5_nl_recv(int nlsk_fd, uint32_t sn, int (*cb)(struct nlmsghdr *, void *arg),
269              void *arg)
270 {
271         struct sockaddr_nl sa;
272         char buf[MLX5_RECV_BUF_SIZE];
273         struct iovec iov = {
274                 .iov_base = buf,
275                 .iov_len = sizeof(buf),
276         };
277         struct msghdr msg = {
278                 .msg_name = &sa,
279                 .msg_namelen = sizeof(sa),
280                 .msg_iov = &iov,
281                 /* One message at a time */
282                 .msg_iovlen = 1,
283         };
284         int multipart = 0;
285         int ret = 0;
286
287         do {
288                 struct nlmsghdr *nh;
289                 int recv_bytes = 0;
290
291                 do {
292                         recv_bytes = recvmsg(nlsk_fd, &msg, 0);
293                         if (recv_bytes == -1) {
294                                 rte_errno = errno;
295                                 return -rte_errno;
296                         }
297                         nh = (struct nlmsghdr *)buf;
298                 } while (nh->nlmsg_seq != sn);
299                 for (;
300                      NLMSG_OK(nh, (unsigned int)recv_bytes);
301                      nh = NLMSG_NEXT(nh, recv_bytes)) {
302                         if (nh->nlmsg_type == NLMSG_ERROR) {
303                                 struct nlmsgerr *err_data = NLMSG_DATA(nh);
304
305                                 if (err_data->error < 0) {
306                                         rte_errno = -err_data->error;
307                                         return -rte_errno;
308                                 }
309                                 /* Ack message. */
310                                 return 0;
311                         }
312                         /* Multi-part msgs and their trailing DONE message. */
313                         if (nh->nlmsg_flags & NLM_F_MULTI) {
314                                 if (nh->nlmsg_type == NLMSG_DONE)
315                                         return 0;
316                                 multipart = 1;
317                         }
318                         if (cb) {
319                                 ret = cb(nh, arg);
320                                 if (ret < 0)
321                                         return ret;
322                         }
323                 }
324         } while (multipart);
325         return ret;
326 }
327
328 /**
329  * Parse Netlink message to retrieve the bridge MAC address.
330  *
331  * @param nh
332  *   Pointer to Netlink Message Header.
333  * @param arg
334  *   PMD data register with this callback.
335  *
336  * @return
337  *   0 on success, a negative errno value otherwise and rte_errno is set.
338  */
339 static int
340 mlx5_nl_mac_addr_cb(struct nlmsghdr *nh, void *arg)
341 {
342         struct mlx5_nl_mac_addr *data = arg;
343         struct ndmsg *r = NLMSG_DATA(nh);
344         struct rtattr *attribute;
345         int len;
346
347         len = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
348         for (attribute = MLX5_NDA_RTA(r);
349              RTA_OK(attribute, len);
350              attribute = RTA_NEXT(attribute, len)) {
351                 if (attribute->rta_type == NDA_LLADDR) {
352                         if (data->mac_n == MLX5_MAX_MAC_ADDRESSES) {
353                                 DRV_LOG(WARNING,
354                                         "not enough room to finalize the"
355                                         " request");
356                                 rte_errno = ENOMEM;
357                                 return -rte_errno;
358                         }
359 #ifndef NDEBUG
360                         char m[18];
361
362                         rte_ether_format_addr(m, 18, RTA_DATA(attribute));
363                         DRV_LOG(DEBUG, "bridge MAC address %s", m);
364 #endif
365                         memcpy(&(*data->mac)[data->mac_n++],
366                                RTA_DATA(attribute), RTE_ETHER_ADDR_LEN);
367                 }
368         }
369         return 0;
370 }
371
372 /**
373  * Get bridge MAC addresses.
374  *
375  * @param dev
376  *   Pointer to Ethernet device.
377  * @param mac[out]
378  *   Pointer to the array table of MAC addresses to fill.
379  *   Its size should be of MLX5_MAX_MAC_ADDRESSES.
380  * @param mac_n[out]
381  *   Number of entries filled in MAC array.
382  *
383  * @return
384  *   0 on success, a negative errno value otherwise and rte_errno is set.
385  */
386 static int
387 mlx5_nl_mac_addr_list(struct rte_eth_dev *dev, struct rte_ether_addr (*mac)[],
388                       int *mac_n)
389 {
390         struct mlx5_priv *priv = dev->data->dev_private;
391         unsigned int iface_idx = mlx5_ifindex(dev);
392         struct {
393                 struct nlmsghdr hdr;
394                 struct ifinfomsg ifm;
395         } req = {
396                 .hdr = {
397                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
398                         .nlmsg_type = RTM_GETNEIGH,
399                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
400                 },
401                 .ifm = {
402                         .ifi_family = PF_BRIDGE,
403                         .ifi_index = iface_idx,
404                 },
405         };
406         struct mlx5_nl_mac_addr data = {
407                 .mac = mac,
408                 .mac_n = 0,
409         };
410         int fd;
411         int ret;
412         uint32_t sn = priv->nl_sn++;
413
414         if (priv->nl_socket_route == -1)
415                 return 0;
416         fd = priv->nl_socket_route;
417         ret = mlx5_nl_request(fd, &req.hdr, sn, &req.ifm,
418                               sizeof(struct ifinfomsg));
419         if (ret < 0)
420                 goto error;
421         ret = mlx5_nl_recv(fd, sn, mlx5_nl_mac_addr_cb, &data);
422         if (ret < 0)
423                 goto error;
424         *mac_n = data.mac_n;
425         return 0;
426 error:
427         DRV_LOG(DEBUG, "port %u cannot retrieve MAC address list %s",
428                 dev->data->port_id, strerror(rte_errno));
429         return -rte_errno;
430 }
431
432 /**
433  * Modify the MAC address neighbour table with Netlink.
434  *
435  * @param dev
436  *   Pointer to Ethernet device.
437  * @param mac
438  *   MAC address to consider.
439  * @param add
440  *   1 to add the MAC address, 0 to remove the MAC address.
441  *
442  * @return
443  *   0 on success, a negative errno value otherwise and rte_errno is set.
444  */
445 static int
446 mlx5_nl_mac_addr_modify(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
447                         int add)
448 {
449         struct mlx5_priv *priv = dev->data->dev_private;
450         unsigned int iface_idx = mlx5_ifindex(dev);
451         struct {
452                 struct nlmsghdr hdr;
453                 struct ndmsg ndm;
454                 struct rtattr rta;
455                 uint8_t buffer[RTE_ETHER_ADDR_LEN];
456         } req = {
457                 .hdr = {
458                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ndmsg)),
459                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
460                                 NLM_F_EXCL | NLM_F_ACK,
461                         .nlmsg_type = add ? RTM_NEWNEIGH : RTM_DELNEIGH,
462                 },
463                 .ndm = {
464                         .ndm_family = PF_BRIDGE,
465                         .ndm_state = NUD_NOARP | NUD_PERMANENT,
466                         .ndm_ifindex = iface_idx,
467                         .ndm_flags = NTF_SELF,
468                 },
469                 .rta = {
470                         .rta_type = NDA_LLADDR,
471                         .rta_len = RTA_LENGTH(RTE_ETHER_ADDR_LEN),
472                 },
473         };
474         int fd;
475         int ret;
476         uint32_t sn = priv->nl_sn++;
477
478         if (priv->nl_socket_route == -1)
479                 return 0;
480         fd = priv->nl_socket_route;
481         memcpy(RTA_DATA(&req.rta), mac, RTE_ETHER_ADDR_LEN);
482         req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
483                 RTA_ALIGN(req.rta.rta_len);
484         ret = mlx5_nl_send(fd, &req.hdr, sn);
485         if (ret < 0)
486                 goto error;
487         ret = mlx5_nl_recv(fd, sn, NULL, NULL);
488         if (ret < 0)
489                 goto error;
490         return 0;
491 error:
492         DRV_LOG(DEBUG,
493                 "port %u cannot %s MAC address %02X:%02X:%02X:%02X:%02X:%02X"
494                 " %s",
495                 dev->data->port_id,
496                 add ? "add" : "remove",
497                 mac->addr_bytes[0], mac->addr_bytes[1],
498                 mac->addr_bytes[2], mac->addr_bytes[3],
499                 mac->addr_bytes[4], mac->addr_bytes[5],
500                 strerror(rte_errno));
501         return -rte_errno;
502 }
503
504 /**
505  * Modify the VF MAC address neighbour table with Netlink.
506  *
507  * @param dev
508  *    Pointer to Ethernet device.
509  * @param mac
510  *    MAC address to consider.
511  * @param vf_index
512  *    VF index.
513  *
514  * @return
515  *    0 on success, a negative errno value otherwise and rte_errno is set.
516  */
517 int
518 mlx5_nl_vf_mac_addr_modify(struct rte_eth_dev *dev,
519                            struct rte_ether_addr *mac, int vf_index)
520 {
521         int fd, ret;
522         struct mlx5_priv *priv = dev->data->dev_private;
523         unsigned int iface_idx = mlx5_ifindex(dev);
524         struct {
525                 struct nlmsghdr hdr;
526                 struct ifinfomsg ifm;
527                 struct rtattr vf_list_rta;
528                 struct rtattr vf_info_rta;
529                 struct rtattr vf_mac_rta;
530                 struct ifla_vf_mac ivm;
531         } req = {
532                 .hdr = {
533                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
534                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
535                         .nlmsg_type = RTM_BASE,
536                 },
537                 .ifm = {
538                         .ifi_index = iface_idx,
539                 },
540                 .vf_list_rta = {
541                         .rta_type = IFLA_VFINFO_LIST,
542                         .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
543                 },
544                 .vf_info_rta = {
545                         .rta_type = IFLA_VF_INFO,
546                         .rta_len = RTA_ALIGN(RTA_LENGTH(0)),
547                 },
548                 .vf_mac_rta = {
549                         .rta_type = IFLA_VF_MAC,
550                 },
551         };
552         uint32_t sn = priv->nl_sn++;
553         struct ifla_vf_mac ivm = {
554                 .vf = vf_index,
555         };
556
557         memcpy(&ivm.mac, mac, RTE_ETHER_ADDR_LEN);
558         memcpy(RTA_DATA(&req.vf_mac_rta), &ivm, sizeof(ivm));
559
560         req.vf_mac_rta.rta_len = RTA_LENGTH(sizeof(ivm));
561         req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
562                 RTA_ALIGN(req.vf_list_rta.rta_len) +
563                 RTA_ALIGN(req.vf_info_rta.rta_len) +
564                 RTA_ALIGN(req.vf_mac_rta.rta_len);
565         req.vf_list_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
566                                                &req.vf_list_rta);
567         req.vf_info_rta.rta_len = RTE_PTR_DIFF(NLMSG_TAIL(&req.hdr),
568                                                &req.vf_info_rta);
569
570         fd = priv->nl_socket_route;
571         if (fd < 0)
572                 return -1;
573         ret = mlx5_nl_send(fd, &req.hdr, sn);
574         if (ret < 0)
575                 goto error;
576         ret = mlx5_nl_recv(fd, sn, NULL, NULL);
577         if (ret < 0)
578                 goto error;
579         return 0;
580 error:
581         DRV_LOG(ERR,
582                 "representor %u cannot set VF MAC address "
583                 "%02X:%02X:%02X:%02X:%02X:%02X : %s",
584                 vf_index,
585                 mac->addr_bytes[0], mac->addr_bytes[1],
586                 mac->addr_bytes[2], mac->addr_bytes[3],
587                 mac->addr_bytes[4], mac->addr_bytes[5],
588                 strerror(rte_errno));
589         return -rte_errno;
590 }
591
592 /**
593  * Add a MAC address.
594  *
595  * @param dev
596  *   Pointer to Ethernet device.
597  * @param mac
598  *   MAC address to register.
599  * @param index
600  *   MAC address index.
601  *
602  * @return
603  *   0 on success, a negative errno value otherwise and rte_errno is set.
604  */
605 int
606 mlx5_nl_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
607                      uint32_t index)
608 {
609         struct mlx5_priv *priv = dev->data->dev_private;
610         int ret;
611
612         ret = mlx5_nl_mac_addr_modify(dev, mac, 1);
613         if (!ret)
614                 BITFIELD_SET(priv->mac_own, index);
615         if (ret == -EEXIST)
616                 return 0;
617         return ret;
618 }
619
620 /**
621  * Remove a MAC address.
622  *
623  * @param dev
624  *   Pointer to Ethernet device.
625  * @param mac
626  *   MAC address to remove.
627  * @param index
628  *   MAC address index.
629  *
630  * @return
631  *   0 on success, a negative errno value otherwise and rte_errno is set.
632  */
633 int
634 mlx5_nl_mac_addr_remove(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
635                         uint32_t index)
636 {
637         struct mlx5_priv *priv = dev->data->dev_private;
638
639         BITFIELD_RESET(priv->mac_own, index);
640         return mlx5_nl_mac_addr_modify(dev, mac, 0);
641 }
642
643 /**
644  * Synchronize Netlink bridge table to the internal table.
645  *
646  * @param dev
647  *   Pointer to Ethernet device.
648  */
649 void
650 mlx5_nl_mac_addr_sync(struct rte_eth_dev *dev)
651 {
652         struct rte_ether_addr macs[MLX5_MAX_MAC_ADDRESSES];
653         int macs_n = 0;
654         int i;
655         int ret;
656
657         ret = mlx5_nl_mac_addr_list(dev, &macs, &macs_n);
658         if (ret)
659                 return;
660         for (i = 0; i != macs_n; ++i) {
661                 int j;
662
663                 /* Verify the address is not in the array yet. */
664                 for (j = 0; j != MLX5_MAX_MAC_ADDRESSES; ++j)
665                         if (rte_is_same_ether_addr(&macs[i],
666                                                &dev->data->mac_addrs[j]))
667                                 break;
668                 if (j != MLX5_MAX_MAC_ADDRESSES)
669                         continue;
670                 /* Find the first entry available. */
671                 for (j = 0; j != MLX5_MAX_MAC_ADDRESSES; ++j) {
672                         if (rte_is_zero_ether_addr(&dev->data->mac_addrs[j])) {
673                                 dev->data->mac_addrs[j] = macs[i];
674                                 break;
675                         }
676                 }
677         }
678 }
679
680 /**
681  * Flush all added MAC addresses.
682  *
683  * @param dev
684  *   Pointer to Ethernet device.
685  */
686 void
687 mlx5_nl_mac_addr_flush(struct rte_eth_dev *dev)
688 {
689         struct mlx5_priv *priv = dev->data->dev_private;
690         int i;
691
692         for (i = MLX5_MAX_MAC_ADDRESSES - 1; i >= 0; --i) {
693                 struct rte_ether_addr *m = &dev->data->mac_addrs[i];
694
695                 if (BITFIELD_ISSET(priv->mac_own, i))
696                         mlx5_nl_mac_addr_remove(dev, m, i);
697         }
698 }
699
700 /**
701  * Enable promiscuous / all multicast mode through Netlink.
702  *
703  * @param dev
704  *   Pointer to Ethernet device structure.
705  * @param flags
706  *   IFF_PROMISC for promiscuous, IFF_ALLMULTI for allmulti.
707  * @param enable
708  *   Nonzero to enable, disable otherwise.
709  *
710  * @return
711  *   0 on success, a negative errno value otherwise and rte_errno is set.
712  */
713 static int
714 mlx5_nl_device_flags(struct rte_eth_dev *dev, uint32_t flags, int enable)
715 {
716         struct mlx5_priv *priv = dev->data->dev_private;
717         unsigned int iface_idx = mlx5_ifindex(dev);
718         struct {
719                 struct nlmsghdr hdr;
720                 struct ifinfomsg ifi;
721         } req = {
722                 .hdr = {
723                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
724                         .nlmsg_type = RTM_NEWLINK,
725                         .nlmsg_flags = NLM_F_REQUEST,
726                 },
727                 .ifi = {
728                         .ifi_flags = enable ? flags : 0,
729                         .ifi_change = flags,
730                         .ifi_index = iface_idx,
731                 },
732         };
733         int fd;
734         int ret;
735
736         assert(!(flags & ~(IFF_PROMISC | IFF_ALLMULTI)));
737         if (priv->nl_socket_route < 0)
738                 return 0;
739         fd = priv->nl_socket_route;
740         ret = mlx5_nl_send(fd, &req.hdr, priv->nl_sn++);
741         if (ret < 0)
742                 return ret;
743         return 0;
744 }
745
746 /**
747  * Enable promiscuous mode through Netlink.
748  *
749  * @param dev
750  *   Pointer to Ethernet device structure.
751  * @param enable
752  *   Nonzero to enable, disable otherwise.
753  *
754  * @return
755  *   0 on success, a negative errno value otherwise and rte_errno is set.
756  */
757 int
758 mlx5_nl_promisc(struct rte_eth_dev *dev, int enable)
759 {
760         int ret = mlx5_nl_device_flags(dev, IFF_PROMISC, enable);
761
762         if (ret)
763                 DRV_LOG(DEBUG,
764                         "port %u cannot %s promisc mode: Netlink error %s",
765                         dev->data->port_id, enable ? "enable" : "disable",
766                         strerror(rte_errno));
767         return ret;
768 }
769
770 /**
771  * Enable all multicast mode through Netlink.
772  *
773  * @param dev
774  *   Pointer to Ethernet device structure.
775  * @param enable
776  *   Nonzero to enable, disable otherwise.
777  *
778  * @return
779  *   0 on success, a negative errno value otherwise and rte_errno is set.
780  */
781 int
782 mlx5_nl_allmulti(struct rte_eth_dev *dev, int enable)
783 {
784         int ret = mlx5_nl_device_flags(dev, IFF_ALLMULTI, enable);
785
786         if (ret)
787                 DRV_LOG(DEBUG,
788                         "port %u cannot %s allmulti mode: Netlink error %s",
789                         dev->data->port_id, enable ? "enable" : "disable",
790                         strerror(rte_errno));
791         return ret;
792 }
793
794 /**
795  * Process network interface information from Netlink message.
796  *
797  * @param nh
798  *   Pointer to Netlink message header.
799  * @param arg
800  *   Opaque data pointer for this callback.
801  *
802  * @return
803  *   0 on success, a negative errno value otherwise and rte_errno is set.
804  */
805 static int
806 mlx5_nl_cmdget_cb(struct nlmsghdr *nh, void *arg)
807 {
808         struct mlx5_nl_ifindex_data *data = arg;
809         struct mlx5_nl_ifindex_data local = {
810                 .flags = 0,
811         };
812         size_t off = NLMSG_HDRLEN;
813
814         if (nh->nlmsg_type !=
815             RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_GET) &&
816             nh->nlmsg_type !=
817             RDMA_NL_GET_TYPE(RDMA_NL_NLDEV, RDMA_NLDEV_CMD_PORT_GET))
818                 goto error;
819         while (off < nh->nlmsg_len) {
820                 struct nlattr *na = (void *)((uintptr_t)nh + off);
821                 void *payload = (void *)((uintptr_t)na + NLA_HDRLEN);
822
823                 if (na->nla_len > nh->nlmsg_len - off)
824                         goto error;
825                 switch (na->nla_type) {
826                 case RDMA_NLDEV_ATTR_DEV_INDEX:
827                         local.ibindex = *(uint32_t *)payload;
828                         local.flags |= MLX5_NL_CMD_GET_IB_INDEX;
829                         break;
830                 case RDMA_NLDEV_ATTR_DEV_NAME:
831                         if (!strcmp(payload, data->name))
832                                 local.flags |= MLX5_NL_CMD_GET_IB_NAME;
833                         break;
834                 case RDMA_NLDEV_ATTR_NDEV_INDEX:
835                         local.ifindex = *(uint32_t *)payload;
836                         local.flags |= MLX5_NL_CMD_GET_NET_INDEX;
837                         break;
838                 case RDMA_NLDEV_ATTR_PORT_INDEX:
839                         local.portnum = *(uint32_t *)payload;
840                         local.flags |= MLX5_NL_CMD_GET_PORT_INDEX;
841                         break;
842                 default:
843                         break;
844                 }
845                 off += NLA_ALIGN(na->nla_len);
846         }
847         /*
848          * It is possible to have multiple messages for all
849          * Infiniband devices in the system with appropriate name.
850          * So we should gather parameters locally and copy to
851          * query context only in case of coinciding device name.
852          */
853         if (local.flags & MLX5_NL_CMD_GET_IB_NAME) {
854                 data->flags = local.flags;
855                 data->ibindex = local.ibindex;
856                 data->ifindex = local.ifindex;
857                 data->portnum = local.portnum;
858         }
859         return 0;
860 error:
861         rte_errno = EINVAL;
862         return -rte_errno;
863 }
864
865 /**
866  * Get index of network interface associated with some IB device.
867  *
868  * This is the only somewhat safe method to avoid resorting to heuristics
869  * when faced with port representors. Unfortunately it requires at least
870  * Linux 4.17.
871  *
872  * @param nl
873  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
874  * @param[in] name
875  *   IB device name.
876  * @param[in] pindex
877  *   IB device port index, starting from 1
878  * @return
879  *   A valid (nonzero) interface index on success, 0 otherwise and rte_errno
880  *   is set.
881  */
882 unsigned int
883 mlx5_nl_ifindex(int nl, const char *name, uint32_t pindex)
884 {
885         uint32_t seq = random();
886         struct mlx5_nl_ifindex_data data = {
887                 .name = name,
888                 .flags = 0,
889                 .ibindex = 0, /* Determined during first pass. */
890                 .ifindex = 0, /* Determined during second pass. */
891         };
892         union {
893                 struct nlmsghdr nh;
894                 uint8_t buf[NLMSG_HDRLEN +
895                             NLA_HDRLEN + NLA_ALIGN(sizeof(data.ibindex)) +
896                             NLA_HDRLEN + NLA_ALIGN(sizeof(pindex))];
897         } req = {
898                 .nh = {
899                         .nlmsg_len = NLMSG_LENGTH(0),
900                         .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
901                                                        RDMA_NLDEV_CMD_GET),
902                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
903                 },
904         };
905         struct nlattr *na;
906         int ret;
907
908         ret = mlx5_nl_send(nl, &req.nh, seq);
909         if (ret < 0)
910                 return 0;
911         ret = mlx5_nl_recv(nl, seq, mlx5_nl_cmdget_cb, &data);
912         if (ret < 0)
913                 return 0;
914         if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
915             !(data.flags & MLX5_NL_CMD_GET_IB_INDEX))
916                 goto error;
917         data.flags = 0;
918         ++seq;
919         req.nh.nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
920                                              RDMA_NLDEV_CMD_PORT_GET);
921         req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
922         req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.buf) - NLMSG_HDRLEN);
923         na = (void *)((uintptr_t)req.buf + NLMSG_HDRLEN);
924         na->nla_len = NLA_HDRLEN + sizeof(data.ibindex);
925         na->nla_type = RDMA_NLDEV_ATTR_DEV_INDEX;
926         memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
927                &data.ibindex, sizeof(data.ibindex));
928         na = (void *)((uintptr_t)na + NLA_ALIGN(na->nla_len));
929         na->nla_len = NLA_HDRLEN + sizeof(pindex);
930         na->nla_type = RDMA_NLDEV_ATTR_PORT_INDEX;
931         memcpy((void *)((uintptr_t)na + NLA_HDRLEN),
932                &pindex, sizeof(pindex));
933         ret = mlx5_nl_send(nl, &req.nh, seq);
934         if (ret < 0)
935                 return 0;
936         ret = mlx5_nl_recv(nl, seq, mlx5_nl_cmdget_cb, &data);
937         if (ret < 0)
938                 return 0;
939         if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
940             !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
941             !(data.flags & MLX5_NL_CMD_GET_NET_INDEX) ||
942             !data.ifindex)
943                 goto error;
944         return data.ifindex;
945 error:
946         rte_errno = ENODEV;
947         return 0;
948 }
949
950 /**
951  * Get the number of physical ports of given IB device.
952  *
953  * @param nl
954  *   Netlink socket of the RDMA kind (NETLINK_RDMA).
955  * @param[in] name
956  *   IB device name.
957  *
958  * @return
959  *   A valid (nonzero) number of ports on success, 0 otherwise
960  *   and rte_errno is set.
961  */
962 unsigned int
963 mlx5_nl_portnum(int nl, const char *name)
964 {
965         uint32_t seq = random();
966         struct mlx5_nl_ifindex_data data = {
967                 .flags = 0,
968                 .name = name,
969                 .ifindex = 0,
970                 .portnum = 0,
971         };
972         struct nlmsghdr req = {
973                 .nlmsg_len = NLMSG_LENGTH(0),
974                 .nlmsg_type = RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
975                                                RDMA_NLDEV_CMD_GET),
976                 .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
977         };
978         int ret;
979
980         ret = mlx5_nl_send(nl, &req, seq);
981         if (ret < 0)
982                 return 0;
983         ret = mlx5_nl_recv(nl, seq, mlx5_nl_cmdget_cb, &data);
984         if (ret < 0)
985                 return 0;
986         if (!(data.flags & MLX5_NL_CMD_GET_IB_NAME) ||
987             !(data.flags & MLX5_NL_CMD_GET_IB_INDEX) ||
988             !(data.flags & MLX5_NL_CMD_GET_PORT_INDEX)) {
989                 rte_errno = ENODEV;
990                 return 0;
991         }
992         if (!data.portnum)
993                 rte_errno = EINVAL;
994         return data.portnum;
995 }
996
997 /**
998  * Process switch information from Netlink message.
999  *
1000  * @param nh
1001  *   Pointer to Netlink message header.
1002  * @param arg
1003  *   Opaque data pointer for this callback.
1004  *
1005  * @return
1006  *   0 on success, a negative errno value otherwise and rte_errno is set.
1007  */
1008 static int
1009 mlx5_nl_switch_info_cb(struct nlmsghdr *nh, void *arg)
1010 {
1011         struct mlx5_switch_info info = {
1012                 .master = 0,
1013                 .representor = 0,
1014                 .name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET,
1015                 .port_name = 0,
1016                 .switch_id = 0,
1017         };
1018         size_t off = NLMSG_LENGTH(sizeof(struct ifinfomsg));
1019         bool switch_id_set = false;
1020         bool num_vf_set = false;
1021
1022         if (nh->nlmsg_type != RTM_NEWLINK)
1023                 goto error;
1024         while (off < nh->nlmsg_len) {
1025                 struct rtattr *ra = (void *)((uintptr_t)nh + off);
1026                 void *payload = RTA_DATA(ra);
1027                 unsigned int i;
1028
1029                 if (ra->rta_len > nh->nlmsg_len - off)
1030                         goto error;
1031                 switch (ra->rta_type) {
1032                 case IFLA_NUM_VF:
1033                         num_vf_set = true;
1034                         break;
1035                 case IFLA_PHYS_PORT_NAME:
1036                         mlx5_translate_port_name((char *)payload, &info);
1037                         break;
1038                 case IFLA_PHYS_SWITCH_ID:
1039                         info.switch_id = 0;
1040                         for (i = 0; i < RTA_PAYLOAD(ra); ++i) {
1041                                 info.switch_id <<= 8;
1042                                 info.switch_id |= ((uint8_t *)payload)[i];
1043                         }
1044                         switch_id_set = true;
1045                         break;
1046                 }
1047                 off += RTA_ALIGN(ra->rta_len);
1048         }
1049         if (switch_id_set) {
1050                 /* We have some E-Switch configuration. */
1051                 mlx5_nl_check_switch_info(num_vf_set, &info);
1052         }
1053         assert(!(info.master && info.representor));
1054         memcpy(arg, &info, sizeof(info));
1055         return 0;
1056 error:
1057         rte_errno = EINVAL;
1058         return -rte_errno;
1059 }
1060
1061 /**
1062  * Get switch information associated with network interface.
1063  *
1064  * @param nl
1065  *   Netlink socket of the ROUTE kind (NETLINK_ROUTE).
1066  * @param ifindex
1067  *   Network interface index.
1068  * @param[out] info
1069  *   Switch information object, populated in case of success.
1070  *
1071  * @return
1072  *   0 on success, a negative errno value otherwise and rte_errno is set.
1073  */
1074 int
1075 mlx5_nl_switch_info(int nl, unsigned int ifindex, struct mlx5_switch_info *info)
1076 {
1077         uint32_t seq = random();
1078         struct {
1079                 struct nlmsghdr nh;
1080                 struct ifinfomsg info;
1081                 struct rtattr rta;
1082                 uint32_t extmask;
1083         } req = {
1084                 .nh = {
1085                         .nlmsg_len = NLMSG_LENGTH
1086                                         (sizeof(req.info) +
1087                                          RTA_LENGTH(sizeof(uint32_t))),
1088                         .nlmsg_type = RTM_GETLINK,
1089                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1090                 },
1091                 .info = {
1092                         .ifi_family = AF_UNSPEC,
1093                         .ifi_index = ifindex,
1094                 },
1095                 .rta = {
1096                         .rta_type = IFLA_EXT_MASK,
1097                         .rta_len = RTA_LENGTH(sizeof(int32_t)),
1098                 },
1099                 .extmask = RTE_LE32(1),
1100         };
1101         int ret;
1102
1103         ret = mlx5_nl_send(nl, &req.nh, seq);
1104         if (ret >= 0)
1105                 ret = mlx5_nl_recv(nl, seq, mlx5_nl_switch_info_cb, info);
1106         if (info->master && info->representor) {
1107                 DRV_LOG(ERR, "ifindex %u device is recognized as master"
1108                              " and as representor", ifindex);
1109                 rte_errno = ENODEV;
1110                 ret = -rte_errno;
1111         }
1112         return ret;
1113 }
1114
1115 /*
1116  * Delete VLAN network device by ifindex.
1117  *
1118  * @param[in] tcf
1119  *   Context object initialized by mlx5_vlan_vmwa_init().
1120  * @param[in] ifindex
1121  *   Interface index of network device to delete.
1122  */
1123 static void
1124 mlx5_vlan_vmwa_delete(struct mlx5_vlan_vmwa_context *vmwa,
1125                       uint32_t ifindex)
1126 {
1127         int ret;
1128         struct {
1129                 struct nlmsghdr nh;
1130                 struct ifinfomsg info;
1131         } req = {
1132                 .nh = {
1133                         .nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
1134                         .nlmsg_type = RTM_DELLINK,
1135                         .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK,
1136                 },
1137                 .info = {
1138                         .ifi_family = AF_UNSPEC,
1139                         .ifi_index = ifindex,
1140                 },
1141         };
1142
1143         if (ifindex) {
1144                 ++vmwa->nl_sn;
1145                 if (!vmwa->nl_sn)
1146                         ++vmwa->nl_sn;
1147                 ret = mlx5_nl_send(vmwa->nl_socket, &req.nh, vmwa->nl_sn);
1148                 if (ret >= 0)
1149                         ret = mlx5_nl_recv(vmwa->nl_socket,
1150                                            vmwa->nl_sn,
1151                                            NULL, NULL);
1152                 if (ret < 0)
1153                         DRV_LOG(WARNING, "netlink: error deleting"
1154                                          " VLAN WA ifindex %u, %d",
1155                                          ifindex, ret);
1156         }
1157 }
1158
1159 /* Set of subroutines to build Netlink message. */
1160 static struct nlattr *
1161 nl_msg_tail(struct nlmsghdr *nlh)
1162 {
1163         return (struct nlattr *)
1164                 (((uint8_t *)nlh) + NLMSG_ALIGN(nlh->nlmsg_len));
1165 }
1166
1167 static void
1168 nl_attr_put(struct nlmsghdr *nlh, int type, const void *data, int alen)
1169 {
1170         struct nlattr *nla = nl_msg_tail(nlh);
1171
1172         nla->nla_type = type;
1173         nla->nla_len = NLMSG_ALIGN(sizeof(struct nlattr) + alen);
1174         nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + nla->nla_len;
1175
1176         if (alen)
1177                 memcpy((uint8_t *)nla + sizeof(struct nlattr), data, alen);
1178 }
1179
1180 static struct nlattr *
1181 nl_attr_nest_start(struct nlmsghdr *nlh, int type)
1182 {
1183         struct nlattr *nest = (struct nlattr *)nl_msg_tail(nlh);
1184
1185         nl_attr_put(nlh, type, NULL, 0);
1186         return nest;
1187 }
1188
1189 static void
1190 nl_attr_nest_end(struct nlmsghdr *nlh, struct nlattr *nest)
1191 {
1192         nest->nla_len = (uint8_t *)nl_msg_tail(nlh) - (uint8_t *)nest;
1193 }
1194
1195 /*
1196  * Create network VLAN device with specified VLAN tag.
1197  *
1198  * @param[in] tcf
1199  *   Context object initialized by mlx5_vlan_vmwa_init().
1200  * @param[in] ifindex
1201  *   Base network interface index.
1202  * @param[in] tag
1203  *   VLAN tag for VLAN network device to create.
1204  */
1205 static uint32_t
1206 mlx5_vlan_vmwa_create(struct mlx5_vlan_vmwa_context *vmwa,
1207                       uint32_t ifindex,
1208                       uint16_t tag)
1209 {
1210         struct nlmsghdr *nlh;
1211         struct ifinfomsg *ifm;
1212         char name[sizeof(MLX5_VMWA_VLAN_DEVICE_PFX) + 32];
1213
1214         alignas(RTE_CACHE_LINE_SIZE)
1215         uint8_t buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
1216                     NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
1217                     NLMSG_ALIGN(sizeof(struct nlattr)) * 8 +
1218                     NLMSG_ALIGN(sizeof(uint32_t)) +
1219                     NLMSG_ALIGN(sizeof(name)) +
1220                     NLMSG_ALIGN(sizeof("vlan")) +
1221                     NLMSG_ALIGN(sizeof(uint32_t)) +
1222                     NLMSG_ALIGN(sizeof(uint16_t)) + 16];
1223         struct nlattr *na_info;
1224         struct nlattr *na_vlan;
1225         int ret;
1226
1227         memset(buf, 0, sizeof(buf));
1228         ++vmwa->nl_sn;
1229         if (!vmwa->nl_sn)
1230                 ++vmwa->nl_sn;
1231         nlh = (struct nlmsghdr *)buf;
1232         nlh->nlmsg_len = sizeof(struct nlmsghdr);
1233         nlh->nlmsg_type = RTM_NEWLINK;
1234         nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE |
1235                            NLM_F_EXCL | NLM_F_ACK;
1236         ifm = (struct ifinfomsg *)nl_msg_tail(nlh);
1237         nlh->nlmsg_len += sizeof(struct ifinfomsg);
1238         ifm->ifi_family = AF_UNSPEC;
1239         ifm->ifi_type = 0;
1240         ifm->ifi_index = 0;
1241         ifm->ifi_flags = IFF_UP;
1242         ifm->ifi_change = 0xffffffff;
1243         nl_attr_put(nlh, IFLA_LINK, &ifindex, sizeof(ifindex));
1244         ret = snprintf(name, sizeof(name), "%s.%u.%u",
1245                        MLX5_VMWA_VLAN_DEVICE_PFX, ifindex, tag);
1246         nl_attr_put(nlh, IFLA_IFNAME, name, ret + 1);
1247         na_info = nl_attr_nest_start(nlh, IFLA_LINKINFO);
1248         nl_attr_put(nlh, IFLA_INFO_KIND, "vlan", sizeof("vlan"));
1249         na_vlan = nl_attr_nest_start(nlh, IFLA_INFO_DATA);
1250         nl_attr_put(nlh, IFLA_VLAN_ID, &tag, sizeof(tag));
1251         nl_attr_nest_end(nlh, na_vlan);
1252         nl_attr_nest_end(nlh, na_info);
1253         assert(sizeof(buf) >= nlh->nlmsg_len);
1254         ret = mlx5_nl_send(vmwa->nl_socket, nlh, vmwa->nl_sn);
1255         if (ret >= 0)
1256                 ret = mlx5_nl_recv(vmwa->nl_socket, vmwa->nl_sn, NULL, NULL);
1257         if (ret < 0) {
1258                 DRV_LOG(WARNING,
1259                         "netlink: VLAN %s create failure (%d)",
1260                         name, ret);
1261         }
1262         // Try to get ifindex of created or pre-existing device.
1263         ret = if_nametoindex(name);
1264         if (!ret) {
1265                 DRV_LOG(WARNING,
1266                         "VLAN %s failed to get index (%d)",
1267                         name, errno);
1268                 return 0;
1269         }
1270         return ret;
1271 }
1272
1273 /*
1274  * Release VLAN network device, created for VM workaround.
1275  *
1276  * @param[in] dev
1277  *   Ethernet device object, Netlink context provider.
1278  * @param[in] vlan
1279  *   Object representing the network device to release.
1280  */
1281 void mlx5_vlan_vmwa_release(struct rte_eth_dev *dev,
1282                             struct mlx5_vf_vlan *vlan)
1283 {
1284         struct mlx5_priv *priv = dev->data->dev_private;
1285         struct mlx5_vlan_vmwa_context *vmwa = priv->vmwa_context;
1286         struct mlx5_vlan_dev *vlan_dev = &vmwa->vlan_dev[0];
1287
1288         assert(vlan->created);
1289         assert(priv->vmwa_context);
1290         if (!vlan->created || !vmwa)
1291                 return;
1292         vlan->created = 0;
1293         assert(vlan_dev[vlan->tag].refcnt);
1294         if (--vlan_dev[vlan->tag].refcnt == 0 &&
1295             vlan_dev[vlan->tag].ifindex) {
1296                 mlx5_vlan_vmwa_delete(vmwa, vlan_dev[vlan->tag].ifindex);
1297                 vlan_dev[vlan->tag].ifindex = 0;
1298         }
1299 }
1300
1301 /**
1302  * Acquire VLAN interface with specified tag for VM workaround.
1303  *
1304  * @param[in] dev
1305  *   Ethernet device object, Netlink context provider.
1306  * @param[in] vlan
1307  *   Object representing the network device to acquire.
1308  */
1309 void mlx5_vlan_vmwa_acquire(struct rte_eth_dev *dev,
1310                             struct mlx5_vf_vlan *vlan)
1311 {
1312         struct mlx5_priv *priv = dev->data->dev_private;
1313         struct mlx5_vlan_vmwa_context *vmwa = priv->vmwa_context;
1314         struct mlx5_vlan_dev *vlan_dev = &vmwa->vlan_dev[0];
1315
1316         assert(!vlan->created);
1317         assert(priv->vmwa_context);
1318         if (vlan->created || !vmwa)
1319                 return;
1320         if (vlan_dev[vlan->tag].refcnt == 0) {
1321                 assert(!vlan_dev[vlan->tag].ifindex);
1322                 vlan_dev[vlan->tag].ifindex =
1323                         mlx5_vlan_vmwa_create(vmwa,
1324                                               vmwa->vf_ifindex,
1325                                               vlan->tag);
1326         }
1327         if (vlan_dev[vlan->tag].ifindex) {
1328                 vlan_dev[vlan->tag].refcnt++;
1329                 vlan->created = 1;
1330         }
1331 }
1332
1333 /*
1334  * Create per ethernet device VLAN VM workaround context
1335  */
1336 struct mlx5_vlan_vmwa_context *
1337 mlx5_vlan_vmwa_init(struct rte_eth_dev *dev,
1338                     uint32_t ifindex)
1339 {
1340         struct mlx5_priv *priv = dev->data->dev_private;
1341         struct mlx5_dev_config *config = &priv->config;
1342         struct mlx5_vlan_vmwa_context *vmwa;
1343         enum rte_hypervisor hv_type;
1344
1345         /* Do not engage workaround over PF. */
1346         if (!config->vf)
1347                 return NULL;
1348         /* Check whether there is desired virtual environment */
1349         hv_type = rte_hypervisor_get();
1350         switch (hv_type) {
1351         case RTE_HYPERVISOR_UNKNOWN:
1352         case RTE_HYPERVISOR_VMWARE:
1353                 /*
1354                  * The "white list" of configurations
1355                  * to engage the workaround.
1356                  */
1357                 break;
1358         default:
1359                 /*
1360                  * The configuration is not found in the "white list".
1361                  * We should not engage the VLAN workaround.
1362                  */
1363                 return NULL;
1364         }
1365         vmwa = rte_zmalloc(__func__, sizeof(*vmwa), sizeof(uint32_t));
1366         if (!vmwa) {
1367                 DRV_LOG(WARNING,
1368                         "Can not allocate memory"
1369                         " for VLAN workaround context");
1370                 return NULL;
1371         }
1372         vmwa->nl_socket = mlx5_nl_init(NETLINK_ROUTE);
1373         if (vmwa->nl_socket < 0) {
1374                 DRV_LOG(WARNING,
1375                         "Can not create Netlink socket"
1376                         " for VLAN workaround context");
1377                 rte_free(vmwa);
1378                 return NULL;
1379         }
1380         vmwa->nl_sn = random();
1381         vmwa->vf_ifindex = ifindex;
1382         vmwa->dev = dev;
1383         /* Cleanup for existing VLAN devices. */
1384         return vmwa;
1385 }
1386
1387 /*
1388  * Destroy per ethernet device VLAN VM workaround context
1389  */
1390 void mlx5_vlan_vmwa_exit(struct mlx5_vlan_vmwa_context *vmwa)
1391 {
1392         unsigned int i;
1393
1394         /* Delete all remaining VLAN devices. */
1395         for (i = 0; i < RTE_DIM(vmwa->vlan_dev); i++) {
1396                 if (vmwa->vlan_dev[i].ifindex)
1397                         mlx5_vlan_vmwa_delete(vmwa, vmwa->vlan_dev[i].ifindex);
1398         }
1399         if (vmwa->nl_socket >= 0)
1400                 close(vmwa->nl_socket);
1401         rte_free(vmwa);
1402 }