net/mlx5: use dynamic logging
[dpdk.git] / drivers / net / mlx5 / mlx5_mac.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox.
4  */
5
6 #include <stddef.h>
7 #include <assert.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <inttypes.h>
11 #include <errno.h>
12 #include <netinet/in.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
15
16 /* Verbs header. */
17 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
18 #ifdef PEDANTIC
19 #pragma GCC diagnostic ignored "-Wpedantic"
20 #endif
21 #include <infiniband/verbs.h>
22 #ifdef PEDANTIC
23 #pragma GCC diagnostic error "-Wpedantic"
24 #endif
25
26 #include <rte_ether.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_common.h>
29
30 #include "mlx5.h"
31 #include "mlx5_utils.h"
32 #include "mlx5_rxtx.h"
33 #include "mlx5_defs.h"
34
35 /**
36  * Get MAC address by querying netdevice.
37  *
38  * @param[in] dev
39  *   Pointer to Ethernet device.
40  * @param[out] mac
41  *   MAC address output buffer.
42  *
43  * @return
44  *   0 on success, a negative errno value otherwise and rte_errno is set.
45  */
46 int
47 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[ETHER_ADDR_LEN])
48 {
49         struct ifreq request;
50         int ret;
51
52         ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
53         if (ret)
54                 return ret;
55         memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
56         return 0;
57 }
58
59 /**
60  * DPDK callback to remove a MAC address.
61  *
62  * @param dev
63  *   Pointer to Ethernet device structure.
64  * @param index
65  *   MAC address index.
66  */
67 void
68 mlx5_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
69 {
70         assert(index < MLX5_MAX_MAC_ADDRESSES);
71         memset(&dev->data->mac_addrs[index], 0, sizeof(struct ether_addr));
72         if (!dev->data->promiscuous) {
73                 int ret = mlx5_traffic_restart(dev);
74
75                 if (ret)
76                         DRV_LOG(ERR, "port %u cannot remove mac address: %s",
77                                 dev->data->port_id, strerror(rte_errno));
78         }
79 }
80
81 /**
82  * DPDK callback to add a MAC address.
83  *
84  * @param dev
85  *   Pointer to Ethernet device structure.
86  * @param mac_addr
87  *   MAC address to register.
88  * @param index
89  *   MAC address index.
90  * @param vmdq
91  *   VMDq pool index to associate address with (ignored).
92  *
93  * @return
94  *   0 on success, a negative errno value otherwise and rte_errno is set.
95  */
96 int
97 mlx5_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac,
98                   uint32_t index, uint32_t vmdq __rte_unused)
99 {
100         unsigned int i;
101
102         assert(index < MLX5_MAX_MAC_ADDRESSES);
103         /* First, make sure this address isn't already configured. */
104         for (i = 0; (i != MLX5_MAX_MAC_ADDRESSES); ++i) {
105                 /* Skip this index, it's going to be reconfigured. */
106                 if (i == index)
107                         continue;
108                 if (memcmp(&dev->data->mac_addrs[i], mac, sizeof(*mac)))
109                         continue;
110                 /* Address already configured elsewhere, return with error. */
111                 rte_errno = EADDRINUSE;
112                 return -rte_errno;
113         }
114         dev->data->mac_addrs[index] = *mac;
115         if (!dev->data->promiscuous)
116                 return mlx5_traffic_restart(dev);
117         return 0;
118 }
119
120 /**
121  * DPDK callback to set primary MAC address.
122  *
123  * @param dev
124  *   Pointer to Ethernet device structure.
125  * @param mac_addr
126  *   MAC address to register.
127  */
128 void
129 mlx5_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
130 {
131         int ret;
132
133         DRV_LOG(DEBUG, "port %u setting primary MAC address",
134                 dev->data->port_id);
135
136         ret = mlx5_mac_addr_add(dev, mac_addr, 0, 0);
137         if (ret)
138                 DRV_LOG(ERR, "port %u cannot set mac address: %s",
139                         dev->data->port_id, strerror(rte_errno));
140 }