blob: 8ef34901c2d8eef46ade52600c31359042463297 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Equalizer Load-balancer for serial network interfaces.
3 *
4 * (c) Copyright 1995 Simon "Guru Aleph-Null" Janes
5 * NCM: Network and Communications Management, Inc.
6 *
7 * (c) Copyright 2002 David S. Miller (davem@redhat.com)
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * The author may be reached as simon@ncm.com, or C/O
13 * NCM
14 * Attn: Simon Janes
15 * 6803 Whittier Ave
16 * McLean VA 22101
17 * Phone: 1-703-847-0040 ext 103
18 */
19
20/*
21 * Sources:
22 * skeleton.c by Donald Becker.
23 * Inspirations:
24 * The Harried and Overworked Alan Cox
25 * Conspiracies:
Jeff Garzik6aa20a22006-09-13 13:24:59 -040026 * The Alan Cox and Mike McLagan plot to get someone else to do the code,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 * which turned out to be me.
28 */
29
30/*
31 * $Log: eql.c,v $
32 * Revision 1.2 1996/04/11 17:51:52 guru
33 * Added one-line eql_remove_slave patch.
34 *
35 * Revision 1.1 1996/04/11 17:44:17 guru
36 * Initial revision
37 *
38 * Revision 3.13 1996/01/21 15:17:18 alan
39 * tx_queue_len changes.
40 * reformatted.
41 *
42 * Revision 3.12 1995/03/22 21:07:51 anarchy
43 * Added capable() checks on configuration.
44 * Moved header file.
45 *
46 * Revision 3.11 1995/01/19 23:14:31 guru
47 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
48 * (priority_Bps) + bytes_queued * 8;
49 *
50 * Revision 3.10 1995/01/19 23:07:53 guru
51 * back to
52 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
53 * (priority_Bps) + bytes_queued;
54 *
55 * Revision 3.9 1995/01/19 22:38:20 guru
56 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
57 * (priority_Bps) + bytes_queued * 4;
58 *
59 * Revision 3.8 1995/01/19 22:30:55 guru
60 * slave_load = (ULONG_MAX - (ULONG_MAX / 2)) -
61 * (priority_Bps) + bytes_queued * 2;
62 *
63 * Revision 3.7 1995/01/19 21:52:35 guru
64 * printk's trimmed out.
65 *
66 * Revision 3.6 1995/01/19 21:49:56 guru
67 * This is working pretty well. I gained 1 K/s in speed.. now it's just
68 * robustness and printk's to be diked out.
69 *
70 * Revision 3.5 1995/01/18 22:29:59 guru
71 * still crashes the kernel when the lock_wait thing is woken up.
72 *
73 * Revision 3.4 1995/01/18 21:59:47 guru
74 * Broken set-bit locking snapshot
75 *
76 * Revision 3.3 1995/01/17 22:09:18 guru
77 * infinite sleep in a lock somewhere..
78 *
79 * Revision 3.2 1995/01/15 16:46:06 guru
80 * Log trimmed of non-pertinent 1.x branch messages
81 *
82 * Revision 3.1 1995/01/15 14:41:45 guru
83 * New Scheduler and timer stuff...
84 *
85 * Revision 1.15 1995/01/15 14:29:02 guru
86 * Will make 1.14 (now 1.15) the 3.0 branch, and the 1.12 the 2.0 branch, the one
87 * with the dumber scheduler
88 *
89 * Revision 1.14 1995/01/15 02:37:08 guru
90 * shock.. the kept-new-versions could have zonked working
91 * stuff.. shudder
92 *
93 * Revision 1.13 1995/01/15 02:36:31 guru
94 * big changes
95 *
96 * scheduler was torn out and replaced with something smarter
97 *
98 * global names not prefixed with eql_ were renamed to protect
99 * against namespace collisions
100 *
101 * a few more abstract interfaces were added to facilitate any
102 * potential change of datastructure. the driver is still using
103 * a linked list of slaves. going to a heap would be a bit of
104 * an overkill.
105 *
106 * this compiles fine with no warnings.
107 *
108 * the locking mechanism and timer stuff must be written however,
109 * this version will not work otherwise
110 *
111 * Sorry, I had to rewrite most of this for 2.5.x -DaveM
112 */
113
Joe Perches63f97422011-03-03 13:30:20 -0800114#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
115
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200116#include <linux/compat.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +0400117#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#include <linux/module.h>
119#include <linux/kernel.h>
120#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900121#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <linux/timer.h>
123#include <linux/netdevice.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -0700124#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126#include <linux/if.h>
127#include <linux/if_arp.h>
128#include <linux/if_eql.h>
Eric Dumazet09e79d62012-03-26 22:52:00 +0000129#include <linux/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800131#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static int eql_open(struct net_device *dev);
134static int eql_close(struct net_device *dev);
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200135static int eql_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
136 void __user *data, int cmd);
Stephen Hemminger424efe92009-08-31 19:50:51 +0000137static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#define eql_is_slave(dev) ((dev->flags & IFF_SLAVE) == IFF_SLAVE)
140#define eql_is_master(dev) ((dev->flags & IFF_MASTER) == IFF_MASTER)
141
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700142static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Kees Cooke99e88a2017-10-16 14:43:17 -0700144static void eql_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Kees Cooke99e88a2017-10-16 14:43:17 -0700146 equalizer_t *eql = from_timer(eql, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct list_head *this, *tmp, *head;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400148
Eric Dumazet09e79d62012-03-26 22:52:00 +0000149 spin_lock(&eql->queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 head = &eql->queue.all_slaves;
151 list_for_each_safe(this, tmp, head) {
152 slave_t *slave = list_entry(this, slave_t, list);
153
154 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
155 slave->bytes_queued -= slave->priority_Bps;
156 if (slave->bytes_queued < 0)
157 slave->bytes_queued = 0;
158 } else {
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700159 eql_kill_one_slave(&eql->queue, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 }
Eric Dumazet09e79d62012-03-26 22:52:00 +0000163 spin_unlock(&eql->queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
166 add_timer(&eql->timer);
167}
168
Stephen Hemmingeraec464b2009-03-20 07:14:14 +0000169static const char version[] __initconst =
Joe Perches63f97422011-03-03 13:30:20 -0800170 "Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Stephen Hemminger99921b72008-11-21 17:37:54 -0800172static const struct net_device_ops eql_netdev_ops = {
173 .ndo_open = eql_open,
174 .ndo_stop = eql_close,
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200175 .ndo_siocdevprivate = eql_siocdevprivate,
Stephen Hemminger99921b72008-11-21 17:37:54 -0800176 .ndo_start_xmit = eql_slave_xmit,
177};
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static void __init eql_setup(struct net_device *dev)
180{
181 equalizer_t *eql = netdev_priv(dev);
182
Kees Cooke99e88a2017-10-16 14:43:17 -0700183 timer_setup(&eql->timer, eql_timer, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 spin_lock_init(&eql->queue.lock);
187 INIT_LIST_HEAD(&eql->queue.all_slaves);
188 eql->queue.master_dev = dev;
189
Stephen Hemminger99921b72008-11-21 17:37:54 -0800190 dev->netdev_ops = &eql_netdev_ops;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 /*
193 * Now we undo some of the things that eth_setup does
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400194 * that we don't like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 dev->mtu = EQL_DEFAULT_MTU; /* set to 576 in if_eql.h */
198 dev->flags = IFF_MASTER;
199
200 dev->type = ARPHRD_SLIP;
201 dev->tx_queue_len = 5; /* Hands them off fast */
Eric Dumazet02875872014-10-05 18:38:35 -0700202 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205static int eql_open(struct net_device *dev)
206{
207 equalizer_t *eql = netdev_priv(dev);
208
209 /* XXX We should force this off automatically for the user. */
Joe Perches63f97422011-03-03 13:30:20 -0800210 netdev_info(dev,
211 "remember to turn off Van-Jacobson compression on your slave devices\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200213 BUG_ON(!list_empty(&eql->queue.all_slaves));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 eql->min_slaves = 1;
216 eql->max_slaves = EQL_DEFAULT_MAX_SLAVES; /* 4 usually... */
217
218 add_timer(&eql->timer);
219
220 return 0;
221}
222
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700223static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 list_del(&slave->list);
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700226 queue->num_slaves--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 slave->dev->flags &= ~IFF_SLAVE;
228 dev_put(slave->dev);
229 kfree(slave);
230}
231
232static void eql_kill_slave_queue(slave_queue_t *queue)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400233{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct list_head *head, *tmp, *this;
235
236 spin_lock_bh(&queue->lock);
237
238 head = &queue->all_slaves;
239 list_for_each_safe(this, tmp, head) {
240 slave_t *s = list_entry(this, slave_t, list);
241
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700242 eql_kill_one_slave(queue, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 spin_unlock_bh(&queue->lock);
246}
247
248static int eql_close(struct net_device *dev)
249{
250 equalizer_t *eql = netdev_priv(dev);
251
252 /*
253 * The timer has to be stopped first before we start hacking away
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400254 * at the data structure it scans every so often...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256
257 del_timer_sync(&eql->timer);
258
259 eql_kill_slave_queue(&eql->queue);
260
261 return 0;
262}
263
264static int eql_enslave(struct net_device *dev, slaving_request_t __user *srq);
265static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq);
266
267static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
268static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc);
269
270static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc);
271static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc);
272
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200273static int eql_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
274 void __user *data, int cmd)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG &&
277 !capable(CAP_NET_ADMIN))
278 return -EPERM;
279
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200280 if (in_compat_syscall()) /* to be implemented */
281 return -EOPNOTSUPP;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 switch (cmd) {
284 case EQL_ENSLAVE:
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200285 return eql_enslave(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 case EQL_EMANCIPATE:
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200287 return eql_emancipate(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case EQL_GETSLAVECFG:
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200289 return eql_g_slave_cfg(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case EQL_SETSLAVECFG:
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200291 return eql_s_slave_cfg(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case EQL_GETMASTRCFG:
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200293 return eql_g_master_cfg(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case EQL_SETMASTRCFG:
Arnd Bergmannd92f7b52021-07-27 15:44:58 +0200295 return eql_s_master_cfg(dev, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 default:
297 return -EOPNOTSUPP;
Joe Perchesee289b62010-05-17 22:47:34 -0700298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/* queue->lock must be held */
302static slave_t *__eql_schedule_slaves(slave_queue_t *queue)
303{
304 unsigned long best_load = ~0UL;
305 struct list_head *this, *tmp, *head;
306 slave_t *best_slave;
307
308 best_slave = NULL;
309
310 /* Make a pass to set the best slave. */
311 head = &queue->all_slaves;
312 list_for_each_safe(this, tmp, head) {
313 slave_t *slave = list_entry(this, slave_t, list);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400314 unsigned long slave_load, bytes_queued, priority_Bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Go through the slave list once, updating best_slave
317 * whenever a new best_load is found.
318 */
319 bytes_queued = slave->bytes_queued;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400320 priority_Bps = slave->priority_Bps;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if ((slave->dev->flags & IFF_UP) == IFF_UP) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400322 slave_load = (~0UL - (~0UL / 2)) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 (priority_Bps) + bytes_queued * 8;
324
325 if (slave_load < best_load) {
326 best_load = slave_load;
327 best_slave = slave;
328 }
329 } else {
330 /* We found a dead slave, kill it. */
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700331 eql_kill_one_slave(queue, slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
333 }
334 return best_slave;
335}
336
Stephen Hemminger424efe92009-08-31 19:50:51 +0000337static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 equalizer_t *eql = netdev_priv(dev);
340 slave_t *slave;
341
342 spin_lock(&eql->queue.lock);
343
344 slave = __eql_schedule_slaves(&eql->queue);
345 if (slave) {
346 struct net_device *slave_dev = slave->dev;
347
348 skb->dev = slave_dev;
Eric Dumazet09e79d62012-03-26 22:52:00 +0000349 skb->priority = TC_PRIO_FILLER;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400350 slave->bytes_queued += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 dev_queue_xmit(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700352 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700354 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 dev_kfree_skb(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 spin_unlock(&eql->queue.lock);
359
Patrick McHardy6ed10652009-06-23 06:03:08 +0000360 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*
364 * Private ioctl functions
365 */
366
367/* queue->lock must be held */
368static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev)
369{
370 struct list_head *this, *head;
371
372 head = &queue->all_slaves;
373 list_for_each(this, head) {
374 slave_t *slave = list_entry(this, slave_t, list);
375
376 if (slave->dev == dev)
377 return slave;
378 }
379
380 return NULL;
381}
382
383static inline int eql_is_full(slave_queue_t *queue)
384{
385 equalizer_t *eql = netdev_priv(queue->master_dev);
386
387 if (queue->num_slaves >= eql->max_slaves)
388 return 1;
389 return 0;
390}
391
392/* queue->lock must be held */
393static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave)
394{
395 if (!eql_is_full(queue)) {
396 slave_t *duplicate_slave = NULL;
397
398 duplicate_slave = __eql_find_slave_dev(queue, slave->dev);
Stephen Hemminger37d2e732007-08-24 22:37:49 -0700399 if (duplicate_slave)
Loic Le Loarer14a59e12005-07-21 14:16:54 -0700400 eql_kill_one_slave(queue, duplicate_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Ying Xueebd93a72014-01-15 10:23:38 +0800402 dev_hold(slave->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 list_add(&slave->list, &queue->all_slaves);
404 queue->num_slaves++;
405 slave->dev->flags |= IFF_SLAVE;
406
407 return 0;
408 }
409
410 return -ENOSPC;
411}
412
413static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp)
414{
415 struct net_device *slave_dev;
416 slaving_request_t srq;
417
418 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
419 return -EFAULT;
420
Ying Xueebd93a72014-01-15 10:23:38 +0800421 slave_dev = __dev_get_by_name(&init_net, srq.slave_name);
422 if (!slave_dev)
423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Ying Xueebd93a72014-01-15 10:23:38 +0800425 if ((master_dev->flags & IFF_UP) == IFF_UP) {
426 /* slave is not a master & not already a slave: */
427 if (!eql_is_master(slave_dev) && !eql_is_slave(slave_dev)) {
428 slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL);
429 equalizer_t *eql = netdev_priv(master_dev);
430 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Ying Xueebd93a72014-01-15 10:23:38 +0800432 if (!s)
433 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Ying Xueebd93a72014-01-15 10:23:38 +0800435 memset(s, 0, sizeof(*s));
436 s->dev = slave_dev;
437 s->priority = srq.priority;
438 s->priority_bps = srq.priority;
439 s->priority_Bps = srq.priority / 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Ying Xueebd93a72014-01-15 10:23:38 +0800441 spin_lock_bh(&eql->queue.lock);
442 ret = __eql_insert_slave(&eql->queue, s);
443 if (ret)
444 kfree(s);
445
446 spin_unlock_bh(&eql->queue.lock);
447
448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 return -EINVAL;
453}
454
455static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp)
456{
457 equalizer_t *eql = netdev_priv(master_dev);
458 struct net_device *slave_dev;
459 slaving_request_t srq;
460 int ret;
461
462 if (copy_from_user(&srq, srqp, sizeof (slaving_request_t)))
463 return -EFAULT;
464
Ying Xueebd93a72014-01-15 10:23:38 +0800465 slave_dev = __dev_get_by_name(&init_net, srq.slave_name);
466 if (!slave_dev)
467 return -ENODEV;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ret = -EINVAL;
Ying Xueebd93a72014-01-15 10:23:38 +0800470 spin_lock_bh(&eql->queue.lock);
471 if (eql_is_slave(slave_dev)) {
472 slave_t *slave = __eql_find_slave_dev(&eql->queue, slave_dev);
473 if (slave) {
474 eql_kill_one_slave(&eql->queue, slave);
475 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Ying Xueebd93a72014-01-15 10:23:38 +0800478 spin_unlock_bh(&eql->queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 return ret;
481}
482
483static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
484{
485 equalizer_t *eql = netdev_priv(dev);
486 slave_t *slave;
487 struct net_device *slave_dev;
488 slave_config_t sc;
489 int ret;
490
491 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
492 return -EFAULT;
493
Ying Xueebd93a72014-01-15 10:23:38 +0800494 slave_dev = __dev_get_by_name(&init_net, sc.slave_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!slave_dev)
496 return -ENODEV;
497
498 ret = -EINVAL;
499
500 spin_lock_bh(&eql->queue.lock);
501 if (eql_is_slave(slave_dev)) {
502 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
503 if (slave) {
504 sc.priority = slave->priority;
505 ret = 0;
506 }
507 }
508 spin_unlock_bh(&eql->queue.lock);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t)))
511 ret = -EFAULT;
512
513 return ret;
514}
515
516static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp)
517{
518 slave_t *slave;
519 equalizer_t *eql;
520 struct net_device *slave_dev;
521 slave_config_t sc;
522 int ret;
523
524 if (copy_from_user(&sc, scp, sizeof (slave_config_t)))
525 return -EFAULT;
526
Ying Xueebd93a72014-01-15 10:23:38 +0800527 slave_dev = __dev_get_by_name(&init_net, sc.slave_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!slave_dev)
529 return -ENODEV;
530
531 ret = -EINVAL;
532
533 eql = netdev_priv(dev);
534 spin_lock_bh(&eql->queue.lock);
535 if (eql_is_slave(slave_dev)) {
536 slave = __eql_find_slave_dev(&eql->queue, slave_dev);
537 if (slave) {
538 slave->priority = sc.priority;
539 slave->priority_bps = sc.priority;
540 slave->priority_Bps = sc.priority / 8;
541 ret = 0;
542 }
543 }
544 spin_unlock_bh(&eql->queue.lock);
545
546 return ret;
547}
548
549static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp)
550{
551 equalizer_t *eql;
552 master_config_t mc;
553
Dan Rosenberg44467182010-09-15 11:43:04 +0000554 memset(&mc, 0, sizeof(master_config_t));
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (eql_is_master(dev)) {
557 eql = netdev_priv(dev);
558 mc.max_slaves = eql->max_slaves;
559 mc.min_slaves = eql->min_slaves;
560 if (copy_to_user(mcp, &mc, sizeof (master_config_t)))
561 return -EFAULT;
562 return 0;
563 }
564 return -EINVAL;
565}
566
567static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp)
568{
569 equalizer_t *eql;
570 master_config_t mc;
571
572 if (copy_from_user(&mc, mcp, sizeof (master_config_t)))
573 return -EFAULT;
574
575 if (eql_is_master(dev)) {
576 eql = netdev_priv(dev);
577 eql->max_slaves = mc.max_slaves;
578 eql->min_slaves = mc.min_slaves;
579 return 0;
580 }
581 return -EINVAL;
582}
583
584static struct net_device *dev_eql;
585
586static int __init eql_init_module(void)
587{
588 int err;
589
Joe Perches63f97422011-03-03 13:30:20 -0800590 pr_info("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Tom Gundersenc835a672014-07-14 16:37:24 +0200592 dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", NET_NAME_UNKNOWN,
593 eql_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!dev_eql)
595 return -ENOMEM;
596
597 err = register_netdev(dev_eql);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400598 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 free_netdev(dev_eql);
600 return err;
601}
602
603static void __exit eql_cleanup_module(void)
604{
605 unregister_netdev(dev_eql);
606 free_netdev(dev_eql);
607}
608
609module_init(eql_init_module);
610module_exit(eql_cleanup_module);
611MODULE_LICENSE("GPL");