Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * 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 Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 26 | * The Alan Cox and Mike McLagan plot to get someone else to do the code, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * 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 Perches | 63f9742 | 2011-03-03 13:30:20 -0800 | [diff] [blame] | 114 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 115 | |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 116 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | #include <linux/module.h> |
| 118 | #include <linux/kernel.h> |
| 119 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 120 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | #include <linux/timer.h> |
| 122 | #include <linux/netdevice.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 123 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | #include <linux/if.h> |
| 126 | #include <linux/if_arp.h> |
| 127 | #include <linux/if_eql.h> |
Eric Dumazet | 09e79d6 | 2012-03-26 22:52:00 +0000 | [diff] [blame] | 128 | #include <linux/pkt_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | #include <asm/uaccess.h> |
| 131 | |
| 132 | static int eql_open(struct net_device *dev); |
| 133 | static int eql_close(struct net_device *dev); |
| 134 | static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd); |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 135 | static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | #define eql_is_slave(dev) ((dev->flags & IFF_SLAVE) == IFF_SLAVE) |
| 138 | #define eql_is_master(dev) ((dev->flags & IFF_MASTER) == IFF_MASTER) |
| 139 | |
Loic Le Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 140 | static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | static void eql_timer(unsigned long param) |
| 143 | { |
| 144 | equalizer_t *eql = (equalizer_t *) param; |
| 145 | struct list_head *this, *tmp, *head; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 146 | |
Eric Dumazet | 09e79d6 | 2012-03-26 22:52:00 +0000 | [diff] [blame] | 147 | spin_lock(&eql->queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | head = &eql->queue.all_slaves; |
| 149 | list_for_each_safe(this, tmp, head) { |
| 150 | slave_t *slave = list_entry(this, slave_t, list); |
| 151 | |
| 152 | if ((slave->dev->flags & IFF_UP) == IFF_UP) { |
| 153 | slave->bytes_queued -= slave->priority_Bps; |
| 154 | if (slave->bytes_queued < 0) |
| 155 | slave->bytes_queued = 0; |
| 156 | } else { |
Loic Le Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 157 | eql_kill_one_slave(&eql->queue, slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | } |
Eric Dumazet | 09e79d6 | 2012-03-26 22:52:00 +0000 | [diff] [blame] | 161 | spin_unlock(&eql->queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL; |
| 164 | add_timer(&eql->timer); |
| 165 | } |
| 166 | |
Stephen Hemminger | aec464b | 2009-03-20 07:14:14 +0000 | [diff] [blame] | 167 | static const char version[] __initconst = |
Joe Perches | 63f9742 | 2011-03-03 13:30:20 -0800 | [diff] [blame] | 168 | "Equalizer2002: Simon Janes (simon@ncm.com) and David S. Miller (davem@redhat.com)"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Stephen Hemminger | 99921b7 | 2008-11-21 17:37:54 -0800 | [diff] [blame] | 170 | static const struct net_device_ops eql_netdev_ops = { |
| 171 | .ndo_open = eql_open, |
| 172 | .ndo_stop = eql_close, |
| 173 | .ndo_do_ioctl = eql_ioctl, |
| 174 | .ndo_start_xmit = eql_slave_xmit, |
| 175 | }; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | static void __init eql_setup(struct net_device *dev) |
| 178 | { |
| 179 | equalizer_t *eql = netdev_priv(dev); |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | init_timer(&eql->timer); |
| 182 | eql->timer.data = (unsigned long) eql; |
| 183 | eql->timer.expires = jiffies + EQL_DEFAULT_RESCHED_IVAL; |
| 184 | eql->timer.function = eql_timer; |
| 185 | |
| 186 | spin_lock_init(&eql->queue.lock); |
| 187 | INIT_LIST_HEAD(&eql->queue.all_slaves); |
| 188 | eql->queue.master_dev = dev; |
| 189 | |
Stephen Hemminger | 99921b7 | 2008-11-21 17:37:54 -0800 | [diff] [blame] | 190 | dev->netdev_ops = &eql_netdev_ops; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Now we undo some of the things that eth_setup does |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 194 | * that we don't like |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | 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 Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 202 | netif_keep_dst(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static 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 Perches | 63f9742 | 2011-03-03 13:30:20 -0800 | [diff] [blame] | 210 | netdev_info(dev, |
| 211 | "remember to turn off Van-Jacobson compression on your slave devices\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Eric Sesterhenn | 5d9428d | 2006-04-02 13:52:48 +0200 | [diff] [blame] | 213 | BUG_ON(!list_empty(&eql->queue.all_slaves)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 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 Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 223 | static void eql_kill_one_slave(slave_queue_t *queue, slave_t *slave) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | list_del(&slave->list); |
Loic Le Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 226 | queue->num_slaves--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | slave->dev->flags &= ~IFF_SLAVE; |
| 228 | dev_put(slave->dev); |
| 229 | kfree(slave); |
| 230 | } |
| 231 | |
| 232 | static void eql_kill_slave_queue(slave_queue_t *queue) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 233 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | 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 Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 242 | eql_kill_one_slave(queue, s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | spin_unlock_bh(&queue->lock); |
| 246 | } |
| 247 | |
| 248 | static 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 Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 254 | * at the data structure it scans every so often... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | */ |
| 256 | |
| 257 | del_timer_sync(&eql->timer); |
| 258 | |
| 259 | eql_kill_slave_queue(&eql->queue); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int eql_enslave(struct net_device *dev, slaving_request_t __user *srq); |
| 265 | static int eql_emancipate(struct net_device *dev, slaving_request_t __user *srq); |
| 266 | |
| 267 | static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *sc); |
| 268 | static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *sc); |
| 269 | |
| 270 | static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mc); |
| 271 | static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mc); |
| 272 | |
| 273 | static int eql_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 274 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (cmd != EQL_GETMASTRCFG && cmd != EQL_GETSLAVECFG && |
| 276 | !capable(CAP_NET_ADMIN)) |
| 277 | return -EPERM; |
| 278 | |
| 279 | switch (cmd) { |
| 280 | case EQL_ENSLAVE: |
| 281 | return eql_enslave(dev, ifr->ifr_data); |
| 282 | case EQL_EMANCIPATE: |
| 283 | return eql_emancipate(dev, ifr->ifr_data); |
| 284 | case EQL_GETSLAVECFG: |
| 285 | return eql_g_slave_cfg(dev, ifr->ifr_data); |
| 286 | case EQL_SETSLAVECFG: |
| 287 | return eql_s_slave_cfg(dev, ifr->ifr_data); |
| 288 | case EQL_GETMASTRCFG: |
| 289 | return eql_g_master_cfg(dev, ifr->ifr_data); |
| 290 | case EQL_SETMASTRCFG: |
| 291 | return eql_s_master_cfg(dev, ifr->ifr_data); |
| 292 | default: |
| 293 | return -EOPNOTSUPP; |
Joe Perches | ee289b6 | 2010-05-17 22:47:34 -0700 | [diff] [blame] | 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* queue->lock must be held */ |
| 298 | static slave_t *__eql_schedule_slaves(slave_queue_t *queue) |
| 299 | { |
| 300 | unsigned long best_load = ~0UL; |
| 301 | struct list_head *this, *tmp, *head; |
| 302 | slave_t *best_slave; |
| 303 | |
| 304 | best_slave = NULL; |
| 305 | |
| 306 | /* Make a pass to set the best slave. */ |
| 307 | head = &queue->all_slaves; |
| 308 | list_for_each_safe(this, tmp, head) { |
| 309 | slave_t *slave = list_entry(this, slave_t, list); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 310 | unsigned long slave_load, bytes_queued, priority_Bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* Go through the slave list once, updating best_slave |
| 313 | * whenever a new best_load is found. |
| 314 | */ |
| 315 | bytes_queued = slave->bytes_queued; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 316 | priority_Bps = slave->priority_Bps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | if ((slave->dev->flags & IFF_UP) == IFF_UP) { |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 318 | slave_load = (~0UL - (~0UL / 2)) - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | (priority_Bps) + bytes_queued * 8; |
| 320 | |
| 321 | if (slave_load < best_load) { |
| 322 | best_load = slave_load; |
| 323 | best_slave = slave; |
| 324 | } |
| 325 | } else { |
| 326 | /* We found a dead slave, kill it. */ |
Loic Le Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 327 | eql_kill_one_slave(queue, slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | return best_slave; |
| 331 | } |
| 332 | |
Stephen Hemminger | 424efe9 | 2009-08-31 19:50:51 +0000 | [diff] [blame] | 333 | static netdev_tx_t eql_slave_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
| 335 | equalizer_t *eql = netdev_priv(dev); |
| 336 | slave_t *slave; |
| 337 | |
| 338 | spin_lock(&eql->queue.lock); |
| 339 | |
| 340 | slave = __eql_schedule_slaves(&eql->queue); |
| 341 | if (slave) { |
| 342 | struct net_device *slave_dev = slave->dev; |
| 343 | |
| 344 | skb->dev = slave_dev; |
Eric Dumazet | 09e79d6 | 2012-03-26 22:52:00 +0000 | [diff] [blame] | 345 | skb->priority = TC_PRIO_FILLER; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 346 | slave->bytes_queued += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | dev_queue_xmit(skb); |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 348 | dev->stats.tx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } else { |
Jeff Garzik | 09f75cd | 2007-10-03 17:41:50 -0700 | [diff] [blame] | 350 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | dev_kfree_skb(skb); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | spin_unlock(&eql->queue.lock); |
| 355 | |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 356 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Private ioctl functions |
| 361 | */ |
| 362 | |
| 363 | /* queue->lock must be held */ |
| 364 | static slave_t *__eql_find_slave_dev(slave_queue_t *queue, struct net_device *dev) |
| 365 | { |
| 366 | struct list_head *this, *head; |
| 367 | |
| 368 | head = &queue->all_slaves; |
| 369 | list_for_each(this, head) { |
| 370 | slave_t *slave = list_entry(this, slave_t, list); |
| 371 | |
| 372 | if (slave->dev == dev) |
| 373 | return slave; |
| 374 | } |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | static inline int eql_is_full(slave_queue_t *queue) |
| 380 | { |
| 381 | equalizer_t *eql = netdev_priv(queue->master_dev); |
| 382 | |
| 383 | if (queue->num_slaves >= eql->max_slaves) |
| 384 | return 1; |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | /* queue->lock must be held */ |
| 389 | static int __eql_insert_slave(slave_queue_t *queue, slave_t *slave) |
| 390 | { |
| 391 | if (!eql_is_full(queue)) { |
| 392 | slave_t *duplicate_slave = NULL; |
| 393 | |
| 394 | duplicate_slave = __eql_find_slave_dev(queue, slave->dev); |
Stephen Hemminger | 37d2e73 | 2007-08-24 22:37:49 -0700 | [diff] [blame] | 395 | if (duplicate_slave) |
Loic Le Loarer | 14a59e1 | 2005-07-21 14:16:54 -0700 | [diff] [blame] | 396 | eql_kill_one_slave(queue, duplicate_slave); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 398 | dev_hold(slave->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | list_add(&slave->list, &queue->all_slaves); |
| 400 | queue->num_slaves++; |
| 401 | slave->dev->flags |= IFF_SLAVE; |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | return -ENOSPC; |
| 407 | } |
| 408 | |
| 409 | static int eql_enslave(struct net_device *master_dev, slaving_request_t __user *srqp) |
| 410 | { |
| 411 | struct net_device *slave_dev; |
| 412 | slaving_request_t srq; |
| 413 | |
| 414 | if (copy_from_user(&srq, srqp, sizeof (slaving_request_t))) |
| 415 | return -EFAULT; |
| 416 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 417 | slave_dev = __dev_get_by_name(&init_net, srq.slave_name); |
| 418 | if (!slave_dev) |
| 419 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 421 | if ((master_dev->flags & IFF_UP) == IFF_UP) { |
| 422 | /* slave is not a master & not already a slave: */ |
| 423 | if (!eql_is_master(slave_dev) && !eql_is_slave(slave_dev)) { |
| 424 | slave_t *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 425 | equalizer_t *eql = netdev_priv(master_dev); |
| 426 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 428 | if (!s) |
| 429 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 431 | memset(s, 0, sizeof(*s)); |
| 432 | s->dev = slave_dev; |
| 433 | s->priority = srq.priority; |
| 434 | s->priority_bps = srq.priority; |
| 435 | s->priority_Bps = srq.priority / 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 437 | spin_lock_bh(&eql->queue.lock); |
| 438 | ret = __eql_insert_slave(&eql->queue, s); |
| 439 | if (ret) |
| 440 | kfree(s); |
| 441 | |
| 442 | spin_unlock_bh(&eql->queue.lock); |
| 443 | |
| 444 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return -EINVAL; |
| 449 | } |
| 450 | |
| 451 | static int eql_emancipate(struct net_device *master_dev, slaving_request_t __user *srqp) |
| 452 | { |
| 453 | equalizer_t *eql = netdev_priv(master_dev); |
| 454 | struct net_device *slave_dev; |
| 455 | slaving_request_t srq; |
| 456 | int ret; |
| 457 | |
| 458 | if (copy_from_user(&srq, srqp, sizeof (slaving_request_t))) |
| 459 | return -EFAULT; |
| 460 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 461 | slave_dev = __dev_get_by_name(&init_net, srq.slave_name); |
| 462 | if (!slave_dev) |
| 463 | return -ENODEV; |
| 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | ret = -EINVAL; |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 466 | spin_lock_bh(&eql->queue.lock); |
| 467 | if (eql_is_slave(slave_dev)) { |
| 468 | slave_t *slave = __eql_find_slave_dev(&eql->queue, slave_dev); |
| 469 | if (slave) { |
| 470 | eql_kill_one_slave(&eql->queue, slave); |
| 471 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 474 | spin_unlock_bh(&eql->queue.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | static int eql_g_slave_cfg(struct net_device *dev, slave_config_t __user *scp) |
| 480 | { |
| 481 | equalizer_t *eql = netdev_priv(dev); |
| 482 | slave_t *slave; |
| 483 | struct net_device *slave_dev; |
| 484 | slave_config_t sc; |
| 485 | int ret; |
| 486 | |
| 487 | if (copy_from_user(&sc, scp, sizeof (slave_config_t))) |
| 488 | return -EFAULT; |
| 489 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 490 | slave_dev = __dev_get_by_name(&init_net, sc.slave_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (!slave_dev) |
| 492 | return -ENODEV; |
| 493 | |
| 494 | ret = -EINVAL; |
| 495 | |
| 496 | spin_lock_bh(&eql->queue.lock); |
| 497 | if (eql_is_slave(slave_dev)) { |
| 498 | slave = __eql_find_slave_dev(&eql->queue, slave_dev); |
| 499 | if (slave) { |
| 500 | sc.priority = slave->priority; |
| 501 | ret = 0; |
| 502 | } |
| 503 | } |
| 504 | spin_unlock_bh(&eql->queue.lock); |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (!ret && copy_to_user(scp, &sc, sizeof (slave_config_t))) |
| 507 | ret = -EFAULT; |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static int eql_s_slave_cfg(struct net_device *dev, slave_config_t __user *scp) |
| 513 | { |
| 514 | slave_t *slave; |
| 515 | equalizer_t *eql; |
| 516 | struct net_device *slave_dev; |
| 517 | slave_config_t sc; |
| 518 | int ret; |
| 519 | |
| 520 | if (copy_from_user(&sc, scp, sizeof (slave_config_t))) |
| 521 | return -EFAULT; |
| 522 | |
Ying Xue | ebd93a7 | 2014-01-15 10:23:38 +0800 | [diff] [blame] | 523 | slave_dev = __dev_get_by_name(&init_net, sc.slave_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | if (!slave_dev) |
| 525 | return -ENODEV; |
| 526 | |
| 527 | ret = -EINVAL; |
| 528 | |
| 529 | eql = netdev_priv(dev); |
| 530 | spin_lock_bh(&eql->queue.lock); |
| 531 | if (eql_is_slave(slave_dev)) { |
| 532 | slave = __eql_find_slave_dev(&eql->queue, slave_dev); |
| 533 | if (slave) { |
| 534 | slave->priority = sc.priority; |
| 535 | slave->priority_bps = sc.priority; |
| 536 | slave->priority_Bps = sc.priority / 8; |
| 537 | ret = 0; |
| 538 | } |
| 539 | } |
| 540 | spin_unlock_bh(&eql->queue.lock); |
| 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static int eql_g_master_cfg(struct net_device *dev, master_config_t __user *mcp) |
| 546 | { |
| 547 | equalizer_t *eql; |
| 548 | master_config_t mc; |
| 549 | |
Dan Rosenberg | 4446718 | 2010-09-15 11:43:04 +0000 | [diff] [blame] | 550 | memset(&mc, 0, sizeof(master_config_t)); |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | if (eql_is_master(dev)) { |
| 553 | eql = netdev_priv(dev); |
| 554 | mc.max_slaves = eql->max_slaves; |
| 555 | mc.min_slaves = eql->min_slaves; |
| 556 | if (copy_to_user(mcp, &mc, sizeof (master_config_t))) |
| 557 | return -EFAULT; |
| 558 | return 0; |
| 559 | } |
| 560 | return -EINVAL; |
| 561 | } |
| 562 | |
| 563 | static int eql_s_master_cfg(struct net_device *dev, master_config_t __user *mcp) |
| 564 | { |
| 565 | equalizer_t *eql; |
| 566 | master_config_t mc; |
| 567 | |
| 568 | if (copy_from_user(&mc, mcp, sizeof (master_config_t))) |
| 569 | return -EFAULT; |
| 570 | |
| 571 | if (eql_is_master(dev)) { |
| 572 | eql = netdev_priv(dev); |
| 573 | eql->max_slaves = mc.max_slaves; |
| 574 | eql->min_slaves = mc.min_slaves; |
| 575 | return 0; |
| 576 | } |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | |
| 580 | static struct net_device *dev_eql; |
| 581 | |
| 582 | static int __init eql_init_module(void) |
| 583 | { |
| 584 | int err; |
| 585 | |
Joe Perches | 63f9742 | 2011-03-03 13:30:20 -0800 | [diff] [blame] | 586 | pr_info("%s\n", version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 588 | dev_eql = alloc_netdev(sizeof(equalizer_t), "eql", NET_NAME_UNKNOWN, |
| 589 | eql_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | if (!dev_eql) |
| 591 | return -ENOMEM; |
| 592 | |
| 593 | err = register_netdev(dev_eql); |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame] | 594 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | free_netdev(dev_eql); |
| 596 | return err; |
| 597 | } |
| 598 | |
| 599 | static void __exit eql_cleanup_module(void) |
| 600 | { |
| 601 | unregister_netdev(dev_eql); |
| 602 | free_netdev(dev_eql); |
| 603 | } |
| 604 | |
| 605 | module_init(eql_init_module); |
| 606 | module_exit(eql_cleanup_module); |
| 607 | MODULE_LICENSE("GPL"); |