blob: 48e1c07842e6e38618554562177ee904830e51a2 [file] [log] [blame]
Patrick McHardya29a1942013-04-17 06:18:28 +00001/*
2 * net/tipc/ib_media.c: Infiniband bearer support for TIPC
3 *
4 * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
5 *
6 * Based on eth_media.c, which carries the following copyright notice:
7 *
8 * Copyright (c) 2001-2007, Ericsson AB
9 * Copyright (c) 2005-2008, 2011, Wind River Systems
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the names of the copyright holders nor the names of its
21 * contributors may be used to endorse or promote products derived from
22 * this software without specific prior written permission.
23 *
24 * Alternatively, this software may be distributed under the terms of the
25 * GNU General Public License ("GPL") version 2 as published by the Free
26 * Software Foundation.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41#include <linux/if_infiniband.h>
42#include "core.h"
43#include "bearer.h"
44
Ying Xue4babbaa2013-10-18 07:23:17 +020045#define MAX_IB_MEDIA MAX_BEARERS
Patrick McHardya29a1942013-04-17 06:18:28 +000046
47/**
Ying Xue4babbaa2013-10-18 07:23:17 +020048 * struct ib_media - Infiniband media data structure
Patrick McHardya29a1942013-04-17 06:18:28 +000049 * @bearer: ptr to associated "generic" bearer structure
50 * @dev: ptr to associated Infiniband network device
51 * @tipc_packet_type: used in binding TIPC to Infiniband driver
52 * @cleanup: work item used when disabling bearer
53 */
54
Ying Xue4babbaa2013-10-18 07:23:17 +020055struct ib_media {
Patrick McHardya29a1942013-04-17 06:18:28 +000056 struct tipc_bearer *bearer;
57 struct net_device *dev;
58 struct packet_type tipc_packet_type;
59 struct work_struct setup;
60 struct work_struct cleanup;
61};
62
Ying Xue4babbaa2013-10-18 07:23:17 +020063static struct ib_media ib_media_array[MAX_IB_MEDIA];
Patrick McHardya29a1942013-04-17 06:18:28 +000064static int ib_started;
65
66/**
67 * ib_media_addr_set - initialize Infiniband media address structure
68 *
69 * Media-dependent "value" field stores MAC address in first 6 bytes
70 * and zeroes out the remaining bytes.
71 */
72static void ib_media_addr_set(const struct tipc_bearer *tb_ptr,
73 struct tipc_media_addr *a, char *mac)
74{
75 BUILD_BUG_ON(sizeof(a->value) < INFINIBAND_ALEN);
76 memcpy(a->value, mac, INFINIBAND_ALEN);
77 a->media_id = TIPC_MEDIA_TYPE_IB;
78 a->broadcast = !memcmp(mac, tb_ptr->bcast_addr.value, INFINIBAND_ALEN);
79}
80
81/**
82 * send_msg - send a TIPC message out over an InfiniBand interface
83 */
84static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
85 struct tipc_media_addr *dest)
86{
87 struct sk_buff *clone;
88 struct net_device *dev;
89 int delta;
90
91 clone = skb_clone(buf, GFP_ATOMIC);
92 if (!clone)
93 return 0;
94
Ying Xue4babbaa2013-10-18 07:23:17 +020095 dev = ((struct ib_media *)(tb_ptr->usr_handle))->dev;
Patrick McHardya29a1942013-04-17 06:18:28 +000096 delta = dev->hard_header_len - skb_headroom(buf);
97
98 if ((delta > 0) &&
99 pskb_expand_head(clone, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
100 kfree_skb(clone);
101 return 0;
102 }
103
104 skb_reset_network_header(clone);
105 clone->dev = dev;
106 clone->protocol = htons(ETH_P_TIPC);
107 dev_hard_header(clone, dev, ETH_P_TIPC, dest->value,
108 dev->dev_addr, clone->len);
109 dev_queue_xmit(clone);
110 return 0;
111}
112
113/**
114 * recv_msg - handle incoming TIPC message from an InfiniBand interface
115 *
116 * Accept only packets explicitly sent to this node, or broadcast packets;
117 * ignores packets sent using InfiniBand multicast, and traffic sent to other
118 * nodes (which can happen if interface is running in promiscuous mode).
119 */
120static int recv_msg(struct sk_buff *buf, struct net_device *dev,
121 struct packet_type *pt, struct net_device *orig_dev)
122{
Ying Xue4babbaa2013-10-18 07:23:17 +0200123 struct ib_media *ib_ptr = (struct ib_media *)pt->af_packet_priv;
Patrick McHardya29a1942013-04-17 06:18:28 +0000124
125 if (!net_eq(dev_net(dev), &init_net)) {
126 kfree_skb(buf);
Ying Xue67981582013-10-18 07:23:19 +0200127 return NET_RX_DROP;
Patrick McHardya29a1942013-04-17 06:18:28 +0000128 }
129
130 if (likely(ib_ptr->bearer)) {
131 if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
132 buf->next = NULL;
133 tipc_recv_msg(buf, ib_ptr->bearer);
Ying Xue67981582013-10-18 07:23:19 +0200134 return NET_RX_SUCCESS;
Patrick McHardya29a1942013-04-17 06:18:28 +0000135 }
136 }
137 kfree_skb(buf);
Ying Xue67981582013-10-18 07:23:19 +0200138 return NET_RX_DROP;
Patrick McHardya29a1942013-04-17 06:18:28 +0000139}
140
141/**
142 * setup_bearer - setup association between InfiniBand bearer and interface
143 */
Ying Xue4babbaa2013-10-18 07:23:17 +0200144static void setup_media(struct work_struct *work)
Patrick McHardya29a1942013-04-17 06:18:28 +0000145{
Ying Xue4babbaa2013-10-18 07:23:17 +0200146 struct ib_media *ib_ptr =
147 container_of(work, struct ib_media, setup);
Patrick McHardya29a1942013-04-17 06:18:28 +0000148
149 dev_add_pack(&ib_ptr->tipc_packet_type);
150}
151
152/**
Ying Xue4babbaa2013-10-18 07:23:17 +0200153 * enable_media - attach TIPC bearer to an InfiniBand interface
Patrick McHardya29a1942013-04-17 06:18:28 +0000154 */
Ying Xue4babbaa2013-10-18 07:23:17 +0200155static int enable_media(struct tipc_bearer *tb_ptr)
Patrick McHardya29a1942013-04-17 06:18:28 +0000156{
Ying Xue2537af92013-06-17 10:54:51 -0400157 struct net_device *dev;
Ying Xue4babbaa2013-10-18 07:23:17 +0200158 struct ib_media *ib_ptr = &ib_media_array[0];
159 struct ib_media *stop = &ib_media_array[MAX_IB_MEDIA];
Patrick McHardya29a1942013-04-17 06:18:28 +0000160 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
161 int pending_dev = 0;
162
163 /* Find unused InfiniBand bearer structure */
164 while (ib_ptr->dev) {
165 if (!ib_ptr->bearer)
166 pending_dev++;
167 if (++ib_ptr == stop)
168 return pending_dev ? -EAGAIN : -EDQUOT;
169 }
170
171 /* Find device with specified name */
Ying Xue2537af92013-06-17 10:54:51 -0400172 dev = dev_get_by_name(&init_net, driver_name);
Patrick McHardya29a1942013-04-17 06:18:28 +0000173 if (!dev)
174 return -ENODEV;
175
176 /* Create InfiniBand bearer for device */
177 ib_ptr->dev = dev;
178 ib_ptr->tipc_packet_type.type = htons(ETH_P_TIPC);
179 ib_ptr->tipc_packet_type.dev = dev;
180 ib_ptr->tipc_packet_type.func = recv_msg;
181 ib_ptr->tipc_packet_type.af_packet_priv = ib_ptr;
182 INIT_LIST_HEAD(&(ib_ptr->tipc_packet_type.list));
Ying Xue4babbaa2013-10-18 07:23:17 +0200183 INIT_WORK(&ib_ptr->setup, setup_media);
Patrick McHardya29a1942013-04-17 06:18:28 +0000184 schedule_work(&ib_ptr->setup);
185
186 /* Associate TIPC bearer with InfiniBand bearer */
187 ib_ptr->bearer = tb_ptr;
188 tb_ptr->usr_handle = (void *)ib_ptr;
189 memset(tb_ptr->bcast_addr.value, 0, sizeof(tb_ptr->bcast_addr.value));
190 memcpy(tb_ptr->bcast_addr.value, dev->broadcast, INFINIBAND_ALEN);
191 tb_ptr->bcast_addr.media_id = TIPC_MEDIA_TYPE_IB;
192 tb_ptr->bcast_addr.broadcast = 1;
193 tb_ptr->mtu = dev->mtu;
Patrick McHardya29a1942013-04-17 06:18:28 +0000194 ib_media_addr_set(tb_ptr, &tb_ptr->addr, (char *)dev->dev_addr);
195 return 0;
196}
197
198/**
199 * cleanup_bearer - break association between InfiniBand bearer and interface
200 *
201 * This routine must be invoked from a work queue because it can sleep.
202 */
203static void cleanup_bearer(struct work_struct *work)
204{
Ying Xue4babbaa2013-10-18 07:23:17 +0200205 struct ib_media *ib_ptr =
206 container_of(work, struct ib_media, cleanup);
Patrick McHardya29a1942013-04-17 06:18:28 +0000207
208 dev_remove_pack(&ib_ptr->tipc_packet_type);
209 dev_put(ib_ptr->dev);
210 ib_ptr->dev = NULL;
211}
212
213/**
Ying Xue4babbaa2013-10-18 07:23:17 +0200214 * disable_media - detach TIPC bearer from an InfiniBand interface
Patrick McHardya29a1942013-04-17 06:18:28 +0000215 *
216 * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away,
217 * then get worker thread to complete bearer cleanup. (Can't do cleanup
218 * here because cleanup code needs to sleep and caller holds spinlocks.)
219 */
Ying Xue4babbaa2013-10-18 07:23:17 +0200220static void disable_media(struct tipc_bearer *tb_ptr)
Patrick McHardya29a1942013-04-17 06:18:28 +0000221{
Ying Xue4babbaa2013-10-18 07:23:17 +0200222 struct ib_media *ib_ptr = (struct ib_media *)tb_ptr->usr_handle;
Patrick McHardya29a1942013-04-17 06:18:28 +0000223
224 ib_ptr->bearer = NULL;
225 INIT_WORK(&ib_ptr->cleanup, cleanup_bearer);
226 schedule_work(&ib_ptr->cleanup);
227}
228
229/**
230 * recv_notification - handle device updates from OS
231 *
232 * Change the state of the InfiniBand bearer (if any) associated with the
233 * specified device.
234 */
235static int recv_notification(struct notifier_block *nb, unsigned long evt,
Jiri Pirko351638e2013-05-28 01:30:21 +0000236 void *ptr)
Patrick McHardya29a1942013-04-17 06:18:28 +0000237{
Jiri Pirko351638e2013-05-28 01:30:21 +0000238 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Ying Xue4babbaa2013-10-18 07:23:17 +0200239 struct ib_media *ib_ptr = &ib_media_array[0];
240 struct ib_media *stop = &ib_media_array[MAX_IB_MEDIA];
Patrick McHardya29a1942013-04-17 06:18:28 +0000241
242 if (!net_eq(dev_net(dev), &init_net))
243 return NOTIFY_DONE;
244
245 while ((ib_ptr->dev != dev)) {
246 if (++ib_ptr == stop)
247 return NOTIFY_DONE; /* couldn't find device */
248 }
249 if (!ib_ptr->bearer)
250 return NOTIFY_DONE; /* bearer had been disabled */
251
252 ib_ptr->bearer->mtu = dev->mtu;
253
254 switch (evt) {
255 case NETDEV_CHANGE:
256 if (netif_carrier_ok(dev))
Erik Hugne512137e2013-12-06 10:08:00 -0500257 break;
Patrick McHardya29a1942013-04-17 06:18:28 +0000258 case NETDEV_DOWN:
Patrick McHardya29a1942013-04-17 06:18:28 +0000259 case NETDEV_CHANGEMTU:
260 case NETDEV_CHANGEADDR:
Erik Hugne512137e2013-12-06 10:08:00 -0500261 tipc_reset_bearer(ib_ptr->bearer);
Patrick McHardya29a1942013-04-17 06:18:28 +0000262 break;
263 case NETDEV_UNREGISTER:
264 case NETDEV_CHANGENAME:
265 tipc_disable_bearer(ib_ptr->bearer->name);
266 break;
267 }
268 return NOTIFY_OK;
269}
270
271static struct notifier_block notifier = {
272 .notifier_call = recv_notification,
273 .priority = 0,
274};
275
276/**
277 * ib_addr2str - convert InfiniBand address to string
278 */
279static int ib_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
280{
281 if (str_size < 60) /* 60 = 19 * strlen("xx:") + strlen("xx\0") */
282 return 1;
283
Andy Shevchenkod77e41e2013-07-10 17:30:34 +0300284 sprintf(str_buf, "%20phC", a->value);
Patrick McHardya29a1942013-04-17 06:18:28 +0000285
286 return 0;
287}
288
289/**
290 * ib_addr2msg - convert InfiniBand address format to message header format
291 */
292static int ib_addr2msg(struct tipc_media_addr *a, char *msg_area)
293{
294 memset(msg_area, 0, TIPC_MEDIA_ADDR_SIZE);
295 msg_area[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_IB;
296 memcpy(msg_area, a->value, INFINIBAND_ALEN);
297 return 0;
298}
299
300/**
301 * ib_msg2addr - convert message header address format to InfiniBand format
302 */
303static int ib_msg2addr(const struct tipc_bearer *tb_ptr,
304 struct tipc_media_addr *a, char *msg_area)
305{
306 ib_media_addr_set(tb_ptr, a, msg_area);
307 return 0;
308}
309
310/*
311 * InfiniBand media registration info
312 */
Jon Paul Maloy5702dba2013-12-10 20:45:39 -0800313struct tipc_media ib_media_info = {
Patrick McHardya29a1942013-04-17 06:18:28 +0000314 .send_msg = send_msg,
Ying Xue4babbaa2013-10-18 07:23:17 +0200315 .enable_media = enable_media,
316 .disable_media = disable_media,
Patrick McHardya29a1942013-04-17 06:18:28 +0000317 .addr2str = ib_addr2str,
318 .addr2msg = ib_addr2msg,
319 .msg2addr = ib_msg2addr,
320 .priority = TIPC_DEF_LINK_PRI,
321 .tolerance = TIPC_DEF_LINK_TOL,
322 .window = TIPC_DEF_LINK_WIN,
323 .type_id = TIPC_MEDIA_TYPE_IB,
324 .name = "ib"
325};
326
327/**
328 * tipc_ib_media_start - activate InfiniBand bearer support
329 *
330 * Register InfiniBand media type with TIPC bearer code. Also register
331 * with OS for notifications about device state changes.
332 */
333int tipc_ib_media_start(void)
334{
335 int res;
336
337 if (ib_started)
338 return -EINVAL;
339
Patrick McHardya29a1942013-04-17 06:18:28 +0000340 res = register_netdevice_notifier(&notifier);
341 if (!res)
342 ib_started = 1;
343 return res;
344}
345
346/**
347 * tipc_ib_media_stop - deactivate InfiniBand bearer support
348 */
349void tipc_ib_media_stop(void)
350{
351 if (!ib_started)
352 return;
353
354 flush_scheduled_work();
355 unregister_netdevice_notifier(&notifier);
356 ib_started = 0;
357}