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