4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <netinet/in.h>
42 #include <sys/ioctl.h>
43 #include <arpa/inet.h>
46 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
48 #pragma GCC diagnostic ignored "-pedantic"
50 #include <infiniband/verbs.h>
52 #pragma GCC diagnostic error "-pedantic"
55 /* DPDK headers don't like -pedantic. */
57 #pragma GCC diagnostic ignored "-pedantic"
59 #include <rte_ether.h>
60 #include <rte_ethdev.h>
61 #include <rte_common.h>
63 #pragma GCC diagnostic error "-pedantic"
67 #include "mlx5_utils.h"
70 * Get MAC address by querying netdevice.
73 * struct priv for the requested device.
75 * MAC address output buffer.
78 * 0 on success, -1 on failure and errno is set.
81 priv_get_mac(struct priv *priv, uint8_t (*mac)[ETHER_ADDR_LEN])
85 if (priv_ifreq(priv, SIOCGIFHWADDR, &request))
87 memcpy(mac, request.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
92 * Unregister a MAC address.
95 * Pointer to private structure.
100 priv_mac_addr_del(struct priv *priv, unsigned int mac_index)
102 assert(mac_index < RTE_DIM(priv->mac));
103 if (!BITFIELD_ISSET(priv->mac_configured, mac_index))
105 BITFIELD_RESET(priv->mac_configured, mac_index);
109 * Register a MAC address.
112 * Pointer to private structure.
114 * MAC address index to use.
116 * MAC address to register.
119 * 0 on success, errno value on failure.
122 priv_mac_addr_add(struct priv *priv, unsigned int mac_index,
123 const uint8_t (*mac)[ETHER_ADDR_LEN])
127 assert(mac_index < RTE_DIM(priv->mac));
128 /* First, make sure this address isn't already configured. */
129 for (i = 0; (i != RTE_DIM(priv->mac)); ++i) {
130 /* Skip this index, it's going to be reconfigured. */
133 if (!BITFIELD_ISSET(priv->mac_configured, i))
135 if (memcmp(priv->mac[i].addr_bytes, *mac, sizeof(*mac)))
137 /* Address already configured elsewhere, return with error. */
140 if (BITFIELD_ISSET(priv->mac_configured, mac_index))
141 priv_mac_addr_del(priv, mac_index);
142 priv->mac[mac_index] = (struct ether_addr){
144 (*mac)[0], (*mac)[1], (*mac)[2],
145 (*mac)[3], (*mac)[4], (*mac)[5]
148 BITFIELD_SET(priv->mac_configured, mac_index);