blob: 9fdf03cd672ba2e269b910f7599927af68d4f97b [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;
Ying Xue37cb0622013-12-10 20:45:41 -080065static int recv_msg(struct sk_buff *buf, struct net_device *dev,
66 struct packet_type *pt, struct net_device *orig_dev);
67
68static struct packet_type tipc_packet_type __read_mostly = {
69 .type = __constant_htons(ETH_P_TIPC),
70 .func = recv_msg,
71};
Patrick McHardya29a1942013-04-17 06:18:28 +000072
73/**
74 * ib_media_addr_set - initialize Infiniband media address structure
75 *
76 * Media-dependent "value" field stores MAC address in first 6 bytes
77 * and zeroes out the remaining bytes.
78 */
79static void ib_media_addr_set(const struct tipc_bearer *tb_ptr,
80 struct tipc_media_addr *a, char *mac)
81{
82 BUILD_BUG_ON(sizeof(a->value) < INFINIBAND_ALEN);
83 memcpy(a->value, mac, INFINIBAND_ALEN);
84 a->media_id = TIPC_MEDIA_TYPE_IB;
85 a->broadcast = !memcmp(mac, tb_ptr->bcast_addr.value, INFINIBAND_ALEN);
86}
87
88/**
89 * send_msg - send a TIPC message out over an InfiniBand interface
90 */
91static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
92 struct tipc_media_addr *dest)
93{
94 struct sk_buff *clone;
95 struct net_device *dev;
96 int delta;
97
98 clone = skb_clone(buf, GFP_ATOMIC);
99 if (!clone)
100 return 0;
101
Ying Xue4babbaa2013-10-18 07:23:17 +0200102 dev = ((struct ib_media *)(tb_ptr->usr_handle))->dev;
Patrick McHardya29a1942013-04-17 06:18:28 +0000103 delta = dev->hard_header_len - skb_headroom(buf);
104
105 if ((delta > 0) &&
106 pskb_expand_head(clone, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
107 kfree_skb(clone);
108 return 0;
109 }
110
111 skb_reset_network_header(clone);
112 clone->dev = dev;
113 clone->protocol = htons(ETH_P_TIPC);
114 dev_hard_header(clone, dev, ETH_P_TIPC, dest->value,
115 dev->dev_addr, clone->len);
116 dev_queue_xmit(clone);
117 return 0;
118}
119
120/**
121 * recv_msg - handle incoming TIPC message from an InfiniBand interface
122 *
123 * Accept only packets explicitly sent to this node, or broadcast packets;
124 * ignores packets sent using InfiniBand multicast, and traffic sent to other
125 * nodes (which can happen if interface is running in promiscuous mode).
126 */
127static int recv_msg(struct sk_buff *buf, struct net_device *dev,
128 struct packet_type *pt, struct net_device *orig_dev)
129{
Ying Xue37cb0622013-12-10 20:45:41 -0800130 struct tipc_bearer *b_ptr;
Patrick McHardya29a1942013-04-17 06:18:28 +0000131
132 if (!net_eq(dev_net(dev), &init_net)) {
133 kfree_skb(buf);
Ying Xue67981582013-10-18 07:23:19 +0200134 return NET_RX_DROP;
Patrick McHardya29a1942013-04-17 06:18:28 +0000135 }
136
Ying Xue37cb0622013-12-10 20:45:41 -0800137 rcu_read_lock();
138 b_ptr = rcu_dereference(dev->tipc_ptr);
139 if (likely(b_ptr)) {
Patrick McHardya29a1942013-04-17 06:18:28 +0000140 if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
141 buf->next = NULL;
Ying Xue37cb0622013-12-10 20:45:41 -0800142 tipc_recv_msg(buf, b_ptr);
143 rcu_read_unlock();
Ying Xue67981582013-10-18 07:23:19 +0200144 return NET_RX_SUCCESS;
Patrick McHardya29a1942013-04-17 06:18:28 +0000145 }
146 }
Ying Xue37cb0622013-12-10 20:45:41 -0800147 rcu_read_unlock();
148
Patrick McHardya29a1942013-04-17 06:18:28 +0000149 kfree_skb(buf);
Ying Xue67981582013-10-18 07:23:19 +0200150 return NET_RX_DROP;
Patrick McHardya29a1942013-04-17 06:18:28 +0000151}
152
153/**
154 * setup_bearer - setup association between InfiniBand bearer and interface
155 */
Ying Xue4babbaa2013-10-18 07:23:17 +0200156static void setup_media(struct work_struct *work)
Patrick McHardya29a1942013-04-17 06:18:28 +0000157{
Ying Xue37cb0622013-12-10 20:45:41 -0800158 dev_add_pack(&tipc_packet_type);
Patrick McHardya29a1942013-04-17 06:18:28 +0000159}
160
161/**
Ying Xue4babbaa2013-10-18 07:23:17 +0200162 * enable_media - attach TIPC bearer to an InfiniBand interface
Patrick McHardya29a1942013-04-17 06:18:28 +0000163 */
Ying Xue4babbaa2013-10-18 07:23:17 +0200164static int enable_media(struct tipc_bearer *tb_ptr)
Patrick McHardya29a1942013-04-17 06:18:28 +0000165{
Ying Xue2537af92013-06-17 10:54:51 -0400166 struct net_device *dev;
Ying Xue4babbaa2013-10-18 07:23:17 +0200167 struct ib_media *ib_ptr = &ib_media_array[0];
168 struct ib_media *stop = &ib_media_array[MAX_IB_MEDIA];
Patrick McHardya29a1942013-04-17 06:18:28 +0000169 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
170 int pending_dev = 0;
171
172 /* Find unused InfiniBand bearer structure */
173 while (ib_ptr->dev) {
174 if (!ib_ptr->bearer)
175 pending_dev++;
176 if (++ib_ptr == stop)
177 return pending_dev ? -EAGAIN : -EDQUOT;
178 }
179
180 /* Find device with specified name */
Ying Xue2537af92013-06-17 10:54:51 -0400181 dev = dev_get_by_name(&init_net, driver_name);
Patrick McHardya29a1942013-04-17 06:18:28 +0000182 if (!dev)
183 return -ENODEV;
184
185 /* Create InfiniBand bearer for device */
186 ib_ptr->dev = dev;
Ying Xue4babbaa2013-10-18 07:23:17 +0200187 INIT_WORK(&ib_ptr->setup, setup_media);
Patrick McHardya29a1942013-04-17 06:18:28 +0000188 schedule_work(&ib_ptr->setup);
189
190 /* Associate TIPC bearer with InfiniBand bearer */
Ying Xue37cb0622013-12-10 20:45:41 -0800191 tb_ptr->dev = dev;
Patrick McHardya29a1942013-04-17 06:18:28 +0000192 ib_ptr->bearer = tb_ptr;
193 tb_ptr->usr_handle = (void *)ib_ptr;
194 memset(tb_ptr->bcast_addr.value, 0, sizeof(tb_ptr->bcast_addr.value));
195 memcpy(tb_ptr->bcast_addr.value, dev->broadcast, INFINIBAND_ALEN);
196 tb_ptr->bcast_addr.media_id = TIPC_MEDIA_TYPE_IB;
197 tb_ptr->bcast_addr.broadcast = 1;
198 tb_ptr->mtu = dev->mtu;
Patrick McHardya29a1942013-04-17 06:18:28 +0000199 ib_media_addr_set(tb_ptr, &tb_ptr->addr, (char *)dev->dev_addr);
Ying Xue37cb0622013-12-10 20:45:41 -0800200 rcu_assign_pointer(dev->tipc_ptr, tb_ptr);
Patrick McHardya29a1942013-04-17 06:18:28 +0000201 return 0;
202}
203
204/**
205 * cleanup_bearer - break association between InfiniBand bearer and interface
206 *
207 * This routine must be invoked from a work queue because it can sleep.
208 */
209static void cleanup_bearer(struct work_struct *work)
210{
Ying Xue4babbaa2013-10-18 07:23:17 +0200211 struct ib_media *ib_ptr =
212 container_of(work, struct ib_media, cleanup);
Patrick McHardya29a1942013-04-17 06:18:28 +0000213
Ying Xue37cb0622013-12-10 20:45:41 -0800214 dev_remove_pack(&tipc_packet_type);
Patrick McHardya29a1942013-04-17 06:18:28 +0000215 dev_put(ib_ptr->dev);
216 ib_ptr->dev = NULL;
217}
218
219/**
Ying Xue4babbaa2013-10-18 07:23:17 +0200220 * disable_media - detach TIPC bearer from an InfiniBand interface
Patrick McHardya29a1942013-04-17 06:18:28 +0000221 *
222 * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away,
223 * then get worker thread to complete bearer cleanup. (Can't do cleanup
224 * here because cleanup code needs to sleep and caller holds spinlocks.)
225 */
Ying Xue4babbaa2013-10-18 07:23:17 +0200226static void disable_media(struct tipc_bearer *tb_ptr)
Patrick McHardya29a1942013-04-17 06:18:28 +0000227{
Ying Xue4babbaa2013-10-18 07:23:17 +0200228 struct ib_media *ib_ptr = (struct ib_media *)tb_ptr->usr_handle;
Patrick McHardya29a1942013-04-17 06:18:28 +0000229
230 ib_ptr->bearer = NULL;
231 INIT_WORK(&ib_ptr->cleanup, cleanup_bearer);
232 schedule_work(&ib_ptr->cleanup);
Ying Xue37cb0622013-12-10 20:45:41 -0800233 RCU_INIT_POINTER(tb_ptr->dev->tipc_ptr, NULL);
Patrick McHardya29a1942013-04-17 06:18:28 +0000234}
235
236/**
237 * recv_notification - handle device updates from OS
238 *
239 * Change the state of the InfiniBand bearer (if any) associated with the
240 * specified device.
241 */
242static int recv_notification(struct notifier_block *nb, unsigned long evt,
Jiri Pirko351638e2013-05-28 01:30:21 +0000243 void *ptr)
Patrick McHardya29a1942013-04-17 06:18:28 +0000244{
Ying Xue37cb0622013-12-10 20:45:41 -0800245 struct tipc_bearer *b_ptr;
Jiri Pirko351638e2013-05-28 01:30:21 +0000246 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Patrick McHardya29a1942013-04-17 06:18:28 +0000247
248 if (!net_eq(dev_net(dev), &init_net))
249 return NOTIFY_DONE;
250
Ying Xue37cb0622013-12-10 20:45:41 -0800251 rcu_read_lock();
252 b_ptr = rcu_dereference(dev->tipc_ptr);
253 if (!b_ptr) {
254 rcu_read_unlock();
Patrick McHardya29a1942013-04-17 06:18:28 +0000255 return NOTIFY_DONE; /* bearer had been disabled */
Ying Xue37cb0622013-12-10 20:45:41 -0800256 }
Patrick McHardya29a1942013-04-17 06:18:28 +0000257
Ying Xue37cb0622013-12-10 20:45:41 -0800258 b_ptr->mtu = dev->mtu;
Patrick McHardya29a1942013-04-17 06:18:28 +0000259
260 switch (evt) {
261 case NETDEV_CHANGE:
262 if (netif_carrier_ok(dev))
Erik Hugne512137e2013-12-06 10:08:00 -0500263 break;
Patrick McHardya29a1942013-04-17 06:18:28 +0000264 case NETDEV_DOWN:
Patrick McHardya29a1942013-04-17 06:18:28 +0000265 case NETDEV_CHANGEMTU:
266 case NETDEV_CHANGEADDR:
Ying Xue37cb0622013-12-10 20:45:41 -0800267 tipc_reset_bearer(b_ptr);
Patrick McHardya29a1942013-04-17 06:18:28 +0000268 break;
269 case NETDEV_UNREGISTER:
270 case NETDEV_CHANGENAME:
Ying Xue37cb0622013-12-10 20:45:41 -0800271 tipc_disable_bearer(b_ptr->name);
Patrick McHardya29a1942013-04-17 06:18:28 +0000272 break;
273 }
Ying Xue37cb0622013-12-10 20:45:41 -0800274 rcu_read_unlock();
275
Patrick McHardya29a1942013-04-17 06:18:28 +0000276 return NOTIFY_OK;
277}
278
279static struct notifier_block notifier = {
280 .notifier_call = recv_notification,
281 .priority = 0,
282};
283
284/**
285 * ib_addr2str - convert InfiniBand address to string
286 */
287static int ib_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
288{
289 if (str_size < 60) /* 60 = 19 * strlen("xx:") + strlen("xx\0") */
290 return 1;
291
Andy Shevchenkod77e41e2013-07-10 17:30:34 +0300292 sprintf(str_buf, "%20phC", a->value);
Patrick McHardya29a1942013-04-17 06:18:28 +0000293
294 return 0;
295}
296
297/**
298 * ib_addr2msg - convert InfiniBand address format to message header format
299 */
300static int ib_addr2msg(struct tipc_media_addr *a, char *msg_area)
301{
302 memset(msg_area, 0, TIPC_MEDIA_ADDR_SIZE);
303 msg_area[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_IB;
304 memcpy(msg_area, a->value, INFINIBAND_ALEN);
305 return 0;
306}
307
308/**
309 * ib_msg2addr - convert message header address format to InfiniBand format
310 */
311static int ib_msg2addr(const struct tipc_bearer *tb_ptr,
312 struct tipc_media_addr *a, char *msg_area)
313{
314 ib_media_addr_set(tb_ptr, a, msg_area);
315 return 0;
316}
317
318/*
319 * InfiniBand media registration info
320 */
Jon Paul Maloy5702dba2013-12-10 20:45:39 -0800321struct tipc_media ib_media_info = {
Patrick McHardya29a1942013-04-17 06:18:28 +0000322 .send_msg = send_msg,
Ying Xue4babbaa2013-10-18 07:23:17 +0200323 .enable_media = enable_media,
324 .disable_media = disable_media,
Patrick McHardya29a1942013-04-17 06:18:28 +0000325 .addr2str = ib_addr2str,
326 .addr2msg = ib_addr2msg,
327 .msg2addr = ib_msg2addr,
328 .priority = TIPC_DEF_LINK_PRI,
329 .tolerance = TIPC_DEF_LINK_TOL,
330 .window = TIPC_DEF_LINK_WIN,
331 .type_id = TIPC_MEDIA_TYPE_IB,
332 .name = "ib"
333};
334
335/**
336 * tipc_ib_media_start - activate InfiniBand bearer support
337 *
338 * Register InfiniBand media type with TIPC bearer code. Also register
339 * with OS for notifications about device state changes.
340 */
341int tipc_ib_media_start(void)
342{
343 int res;
344
345 if (ib_started)
346 return -EINVAL;
347
Patrick McHardya29a1942013-04-17 06:18:28 +0000348 res = register_netdevice_notifier(&notifier);
349 if (!res)
350 ib_started = 1;
351 return res;
352}
353
354/**
355 * tipc_ib_media_stop - deactivate InfiniBand bearer support
356 */
357void tipc_ib_media_stop(void)
358{
359 if (!ib_started)
360 return;
361
362 flush_scheduled_work();
363 unregister_netdevice_notifier(&notifier);
364 ib_started = 0;
365}