Blame view

include/rdma/ib_addr.h 8.91 KB
7025fcd36   Sean Hefty   IB: address trans...
1
2
3
4
  /*
   * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
   * Copyright (c) 2005 Intel Corporation.  All rights reserved.
   *
a94749170   Sean Hefty   RDMA: Fix license...
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   * This software is available to you under a choice of one of two
   * licenses.  You may choose to be licensed under the terms of the GNU
   * General Public License (GPL) Version 2, available from the file
   * COPYING in the main directory of this source tree, or the
   * OpenIB.org BSD license below:
   *
   *     Redistribution and use in source and binary forms, with or
   *     without modification, are permitted provided that the following
   *     conditions are met:
   *
   *      - Redistributions of source code must retain the above
   *        copyright notice, this list of conditions and the following
   *        disclaimer.
   *
   *      - Redistributions in binary form must reproduce the above
   *        copyright notice, this list of conditions and the following
   *        disclaimer in the documentation and/or other materials
   *        provided with the distribution.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   * SOFTWARE.
7025fcd36   Sean Hefty   IB: address trans...
32
33
34
35
36
37
38
   */
  
  #if !defined(IB_ADDR_H)
  #define IB_ADDR_H
  
  #include <linux/in.h>
  #include <linux/in6.h>
c4315d85f   Sean Hefty   IB/addr: Store ne...
39
  #include <linux/if_arp.h>
7025fcd36   Sean Hefty   IB: address trans...
40
  #include <linux/netdevice.h>
7b85627b9   Moni Shoua   IB/cma: IBoE (RoC...
41
  #include <linux/inetdevice.h>
7025fcd36   Sean Hefty   IB: address trans...
42
  #include <linux/socket.h>
af7bd4637   Eli Cohen   IB/core: Add VLAN...
43
  #include <linux/if_vlan.h>
7b85627b9   Moni Shoua   IB/cma: IBoE (RoC...
44
45
46
  #include <net/ipv6.h>
  #include <net/if_inet6.h>
  #include <net/ip.h>
7025fcd36   Sean Hefty   IB: address trans...
47
  #include <rdma/ib_verbs.h>
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
48
  #include <rdma/ib_pack.h>
565edd1d5   Guy Shapiro   IB/addr: Pass net...
49
  #include <net/net_namespace.h>
7025fcd36   Sean Hefty   IB: address trans...
50

565edd1d5   Guy Shapiro   IB/addr: Pass net...
51
52
53
54
55
56
57
58
59
  /**
   * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
   * @src_dev_addr:	Source MAC address.
   * @dst_dev_addr:	Destination MAC address.
   * @broadcast:		Broadcast address of the device.
   * @dev_type:		The interface hardware type of the device.
   * @bound_dev_if:	An optional device interface index.
   * @transport:		The transport type used.
   * @net:		Network namespace containing the bound_dev_if net_dev.
4ed13a5f2   Parav Pandit   IB/cm: Keep track...
60
   * @sgid_attr:		GID attribute to use for identified SGID
565edd1d5   Guy Shapiro   IB/addr: Pass net...
61
   */
7025fcd36   Sean Hefty   IB: address trans...
62
63
64
65
  struct rdma_dev_addr {
  	unsigned char src_dev_addr[MAX_ADDR_LEN];
  	unsigned char dst_dev_addr[MAX_ADDR_LEN];
  	unsigned char broadcast[MAX_ADDR_LEN];
c4315d85f   Sean Hefty   IB/addr: Store ne...
66
  	unsigned short dev_type;
6266ed6e4   Sean Hefty   RDMA/cma: Replace...
67
  	int bound_dev_if;
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
68
  	enum rdma_transport_type transport;
565edd1d5   Guy Shapiro   IB/addr: Pass net...
69
  	struct net *net;
4ed13a5f2   Parav Pandit   IB/cm: Keep track...
70
  	const struct ib_gid_attr *sgid_attr;
c865f2462   Somnath Kotur   IB/core: Add rdma...
71
  	enum rdma_network_type network;
c3efe7500   Matan Barak   IB/core: Use hop-...
72
  	int hoplimit;
7025fcd36   Sean Hefty   IB: address trans...
73
74
75
76
77
  };
  
  /**
   * rdma_translate_ip - Translate a local IP address to an RDMA hardware
   *   address.
565edd1d5   Guy Shapiro   IB/addr: Pass net...
78
79
   *
   * The dev_addr->net field must be initialized.
7025fcd36   Sean Hefty   IB: address trans...
80
   */
200298326   Matan Barak   IB/core: Validate...
81
  int rdma_translate_ip(const struct sockaddr *addr,
575c7e583   Parav Pandit   RDMA/{core, cma}:...
82
  		      struct rdma_dev_addr *dev_addr);
7025fcd36   Sean Hefty   IB: address trans...
83
84
85
86
87
88
89
90
91
92
  
  /**
   * rdma_resolve_ip - Resolve source and destination IP addresses to
   *   RDMA hardware addresses.
   * @src_addr: An optional source address to use in the resolution.  If a
   *   source address is not provided, a usable address will be returned via
   *   the callback.
   * @dst_addr: The destination address to resolve.
   * @addr: A reference to a data location that will receive the resolved
   *   addresses.  The data location must remain valid until the callback has
565edd1d5   Guy Shapiro   IB/addr: Pass net...
93
   *   been invoked. The net field of the addr struct must be valid.
7025fcd36   Sean Hefty   IB: address trans...
94
95
96
   * @timeout_ms: Amount of time to wait for the address resolution to complete.
   * @callback: Call invoked once address resolution has completed, timed out,
   *   or been canceled.  A status of 0 indicates success.
0e9d2c19b   Parav Pandit   RDMA/core: Consid...
97
98
   * @resolve_by_gid_attr:	Resolve the ip based on the GID attribute from
   *				rdma_dev_addr.
7025fcd36   Sean Hefty   IB: address trans...
99
100
   * @context: User-specified context associated with the call.
   */
2df7dba85   Parav Pandit   RDMA/core: Consti...
101
  int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
dbace111e   Leon Romanovsky   RDMA/core: Annota...
102
  		    struct rdma_dev_addr *addr, unsigned long timeout_ms,
7025fcd36   Sean Hefty   IB: address trans...
103
104
  		    void (*callback)(int status, struct sockaddr *src_addr,
  				     struct rdma_dev_addr *addr, void *context),
9549c2bd0   Leon Romanovsky   RDMA/core: Align ...
105
  		    bool resolve_by_gid_attr, void *context);
7025fcd36   Sean Hefty   IB: address trans...
106
107
  
  void rdma_addr_cancel(struct rdma_dev_addr *addr);
2df7dba85   Parav Pandit   RDMA/core: Consti...
108
  int rdma_addr_size(const struct sockaddr *addr);
84652aefb   Roland Dreier   RDMA/ucma: Introd...
109
110
  int rdma_addr_size_in6(struct sockaddr_in6 *addr);
  int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr);
7025fcd36   Sean Hefty   IB: address trans...
111
112
113
114
115
116
117
118
119
120
121
  
  static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
  {
  	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
  }
  
  static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
  {
  	dev_addr->broadcast[8] = pkey >> 8;
  	dev_addr->broadcast[9] = (unsigned char) pkey;
  }
faec2f7b9   Sean Hefty   IB/sa: Track mult...
122
123
124
125
126
  static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
  				    union ib_gid *gid)
  {
  	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
  }
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
127
  static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
7025fcd36   Sean Hefty   IB: address trans...
128
  {
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
129
  	return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
7025fcd36   Sean Hefty   IB: address trans...
130
  }
7b85627b9   Moni Shoua   IB/cma: IBoE (RoC...
131
  static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
132
  {
d0d7b10b0   Parav Pandit   net-next: treewid...
133
  	return is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 0xffff;
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
134
  }
dd5f03beb   Matan Barak   IB/core: Ethernet...
135
136
137
138
139
140
141
142
143
  static inline int rdma_ip2gid(struct sockaddr *addr, union ib_gid *gid)
  {
  	switch (addr->sa_family) {
  	case AF_INET:
  		ipv6_addr_set_v4mapped(((struct sockaddr_in *)
  					addr)->sin_addr.s_addr,
  				       (struct in6_addr *)gid);
  		break;
  	case AF_INET6:
5f8a4db71   Arnd Bergmann   infiniband: avoid...
144
145
  		*(struct in6_addr *)&gid->raw =
  			((struct sockaddr_in6 *)addr)->sin6_addr;
dd5f03beb   Matan Barak   IB/core: Ethernet...
146
147
148
149
150
151
152
153
  		break;
  	default:
  		return -EINVAL;
  	}
  	return 0;
  }
  
  /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
73cdaaeed   Ira Weiny   IB/core cleanup: ...
154
  static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
dd5f03beb   Matan Barak   IB/core: Ethernet...
155
156
157
158
159
160
161
162
163
164
165
166
  {
  	if (ipv6_addr_v4mapped((struct in6_addr *)gid)) {
  		struct sockaddr_in *out_in = (struct sockaddr_in *)out;
  		memset(out_in, 0, sizeof(*out_in));
  		out_in->sin_family = AF_INET;
  		memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
  	} else {
  		struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
  		memset(out_in, 0, sizeof(*out_in));
  		out_in->sin6_family = AF_INET6;
  		memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
  	}
dd5f03beb   Matan Barak   IB/core: Ethernet...
167
  }
15cbc5197   Parav Pandit   RDMA/core: Simpli...
168
169
170
171
172
  /*
   * rdma_get/set_sgid/dgid() APIs are applicable to IB, and iWarp.
   * They are not applicable to RoCE.
   * RoCE GIDs are derived from the IP addresses.
   */
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
173
  static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
7025fcd36   Sean Hefty   IB: address trans...
174
  {
15cbc5197   Parav Pandit   RDMA/core: Simpli...
175
176
  	memcpy(gid, dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr),
  	       sizeof(*gid));
7025fcd36   Sean Hefty   IB: address trans...
177
  }
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
178
  static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
7025fcd36   Sean Hefty   IB: address trans...
179
  {
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
180
  	memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
7025fcd36   Sean Hefty   IB: address trans...
181
  }
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
182
  static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
7025fcd36   Sean Hefty   IB: address trans...
183
  {
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
184
  	memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
7025fcd36   Sean Hefty   IB: address trans...
185
  }
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
186
  static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
07ebafbaa   Tom Tucker   RDMA: iWARP Core ...
187
  {
6f8372b69   Sean Hefty   RDMA/cm: fix loop...
188
  	memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
07ebafbaa   Tom Tucker   RDMA: iWARP Core ...
189
  }
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
190
191
192
  static inline enum ib_mtu iboe_get_mtu(int mtu)
  {
  	/*
99260132f   Parav Pandit   IB/core: Fix calc...
193
  	 * Reduce IB headers from effective IBoE MTU.
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
194
  	 */
99260132f   Parav Pandit   IB/core: Fix calc...
195
196
197
  	mtu = mtu - (IB_GRH_BYTES + IB_UDP_BYTES + IB_BTH_BYTES +
  		     IB_EXT_XRC_BYTES + IB_EXT_ATOMICETH_BYTES +
  		     IB_ICRC_BYTES);
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  
  	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
  		return IB_MTU_4096;
  	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
  		return IB_MTU_2048;
  	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
  		return IB_MTU_1024;
  	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
  		return IB_MTU_512;
  	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
  		return IB_MTU_256;
  	else
  		return 0;
  }
  
  static inline int iboe_get_rate(struct net_device *dev)
  {
17605b961   David Decotigny   net: rdma: use __...
215
  	struct ethtool_link_ksettings cmd;
4bc71cb98   Jiri Pirko   net: consolidate ...
216
  	int err;
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
217

4bc71cb98   Jiri Pirko   net: consolidate ...
218
  	rtnl_lock();
17605b961   David Decotigny   net: rdma: use __...
219
  	err = __ethtool_get_link_ksettings(dev, &cmd);
4bc71cb98   Jiri Pirko   net: consolidate ...
220
221
  	rtnl_unlock();
  	if (err)
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
222
  		return IB_RATE_PORT_CURRENT;
17605b961   David Decotigny   net: rdma: use __...
223
  	if (cmd.base.speed >= 40000)
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
224
  		return IB_RATE_40_GBPS;
17605b961   David Decotigny   net: rdma: use __...
225
  	else if (cmd.base.speed >= 30000)
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
226
  		return IB_RATE_30_GBPS;
17605b961   David Decotigny   net: rdma: use __...
227
  	else if (cmd.base.speed >= 20000)
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
228
  		return IB_RATE_20_GBPS;
17605b961   David Decotigny   net: rdma: use __...
229
  	else if (cmd.base.speed >= 10000)
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  		return IB_RATE_10_GBPS;
  	else
  		return IB_RATE_PORT_CURRENT;
  }
  
  static inline int rdma_link_local_addr(struct in6_addr *addr)
  {
  	if (addr->s6_addr32[0] == htonl(0xfe800000) &&
  	    addr->s6_addr32[1] == 0)
  		return 1;
  
  	return 0;
  }
  
  static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
  {
  	memcpy(mac, &addr->s6_addr[8], 3);
  	memcpy(mac + 3, &addr->s6_addr[13], 3);
  	mac[0] ^= 2;
  }
  
  static inline int rdma_is_multicast_addr(struct in6_addr *addr)
  {
1c3aea2bc   Bart Van Assche   IB/core: Fix endi...
253
  	__be32 ipv4_addr;
be1d325a3   Noa Osherovich   IB/core: Set RoCE...
254
255
256
  
  	if (addr->s6_addr[0] == 0xff)
  		return 1;
1c3aea2bc   Bart Van Assche   IB/core: Fix endi...
257
  	ipv4_addr = addr->s6_addr32[3];
be1d325a3   Noa Osherovich   IB/core: Set RoCE...
258
  	return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
3c86aa70b   Eli Cohen   RDMA/cm: Add RDMA...
259
260
261
262
263
264
265
266
267
268
269
  }
  
  static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
  {
  	int i;
  
  	mac[0] = 0x33;
  	mac[1] = 0x33;
  	for (i = 2; i < 6; ++i)
  		mac[i] = addr->s6_addr[i + 10];
  }
af7bd4637   Eli Cohen   IB/core: Add VLAN...
270
271
272
273
274
275
276
277
278
279
  static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
  {
  	u16 vid;
  
  	vid = dgid->raw[11] << 8 | dgid->raw[12];
  	return vid < 0x1000 ? vid : 0xffff;
  }
  
  static inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev)
  {
d0d7b10b0   Parav Pandit   net-next: treewid...
280
  	return is_vlan_dev(dev) ? vlan_dev_real_dev(dev) : NULL;
af7bd4637   Eli Cohen   IB/core: Add VLAN...
281
  }
7025fcd36   Sean Hefty   IB: address trans...
282
  #endif /* IB_ADDR_H */