blob: 669ee1a1b284853c50b7736119e5b60302cd97d4 [file] [log] [blame]
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001/* drivers/net/ifb.c:
Jamal Hadi Salim253af422006-01-08 22:34:25 -08002
3 The purpose of this driver is to provide a device that allows
4 for sharing of resources:
5
6 1) qdiscs/policies that are per device as opposed to system wide.
7 ifb allows for a device which can be redirected to thus providing
8 an impression of sharing.
9
10 2) Allows for queueing incoming traffic for shaping instead of
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011 dropping.
12
Jamal Hadi Salim253af422006-01-08 22:34:25 -080013 The original concept is based on what is known as the IMQ
14 driver initially written by Martin Devera, later rewritten
15 by Patrick McHardy and then maintained by Andre Correa.
16
17 You need the tc action mirror or redirect to feed this device
18 packets.
19
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License
22 as published by the Free Software Foundation; either version
23 2 of the License, or (at your option) any later version.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040024
Jamal Hadi Salim253af422006-01-08 22:34:25 -080025 Authors: Jamal Hadi Salim (2005)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040026
Jamal Hadi Salim253af422006-01-08 22:34:25 -080027*/
28
29
Jamal Hadi Salim253af422006-01-08 22:34:25 -080030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/init.h>
35#include <linux/moduleparam.h>
Patrick McHardy62b7ffc2007-06-13 12:04:51 -070036#include <linux/list.h>
Jeff Garzik6aa20a22006-09-13 13:24:59 -040037#include <net/pkt_sched.h>
Jamal Hadi Salim253af422006-01-08 22:34:25 -080038
39#define TX_TIMEOUT (2*HZ)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040040
Jamal Hadi Salim253af422006-01-08 22:34:25 -080041#define TX_Q_LIMIT 32
42struct ifb_private {
Patrick McHardy62b7ffc2007-06-13 12:04:51 -070043 struct list_head list;
44 struct net_device *dev;
Jamal Hadi Salim253af422006-01-08 22:34:25 -080045 struct net_device_stats stats;
46 struct tasklet_struct ifb_tasklet;
47 int tasklet_pending;
48 /* mostly debug stats leave in for now */
49 unsigned long st_task_enter; /* tasklet entered */
50 unsigned long st_txq_refl_try; /* transmit queue refill attempt */
51 unsigned long st_rxq_enter; /* receive queue entered */
52 unsigned long st_rx2tx_tran; /* receive to trasmit transfers */
53 unsigned long st_rxq_notenter; /*receiveQ not entered, resched */
54 unsigned long st_rx_frm_egr; /* received from egress path */
55 unsigned long st_rx_frm_ing; /* received from ingress path */
56 unsigned long st_rxq_check;
57 unsigned long st_rxq_rsch;
58 struct sk_buff_head rq;
59 struct sk_buff_head tq;
60};
61
Richard Lucassen35eaa312006-02-23 16:23:51 -080062static int numifbs = 2;
Jamal Hadi Salim253af422006-01-08 22:34:25 -080063
64static void ri_tasklet(unsigned long dev);
65static int ifb_xmit(struct sk_buff *skb, struct net_device *dev);
66static struct net_device_stats *ifb_get_stats(struct net_device *dev);
67static int ifb_open(struct net_device *dev);
68static int ifb_close(struct net_device *dev);
69
Jeff Garzik6aa20a22006-09-13 13:24:59 -040070static void ri_tasklet(unsigned long dev)
Jamal Hadi Salim253af422006-01-08 22:34:25 -080071{
72
73 struct net_device *_dev = (struct net_device *)dev;
74 struct ifb_private *dp = netdev_priv(_dev);
75 struct net_device_stats *stats = &dp->stats;
76 struct sk_buff *skb;
77
78 dp->st_task_enter++;
79 if ((skb = skb_peek(&dp->tq)) == NULL) {
80 dp->st_txq_refl_try++;
Herbert Xu932ff272006-06-09 12:20:56 -070081 if (netif_tx_trylock(_dev)) {
Jamal Hadi Salim253af422006-01-08 22:34:25 -080082 dp->st_rxq_enter++;
83 while ((skb = skb_dequeue(&dp->rq)) != NULL) {
84 skb_queue_tail(&dp->tq, skb);
85 dp->st_rx2tx_tran++;
86 }
Herbert Xu932ff272006-06-09 12:20:56 -070087 netif_tx_unlock(_dev);
Jamal Hadi Salim253af422006-01-08 22:34:25 -080088 } else {
89 /* reschedule */
90 dp->st_rxq_notenter++;
91 goto resched;
92 }
93 }
94
95 while ((skb = skb_dequeue(&dp->tq)) != NULL) {
96 u32 from = G_TC_FROM(skb->tc_verd);
97
98 skb->tc_verd = 0;
99 skb->tc_verd = SET_TC_NCLS(skb->tc_verd);
100 stats->tx_packets++;
101 stats->tx_bytes +=skb->len;
Patrick McHardyc01003c2007-03-29 11:46:52 -0700102
103 skb->dev = __dev_get_by_index(skb->iif);
104 if (!skb->dev) {
105 dev_kfree_skb(skb);
106 stats->tx_dropped++;
107 break;
108 }
109 skb->iif = _dev->ifindex;
110
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800111 if (from & AT_EGRESS) {
112 dp->st_rx_frm_egr++;
113 dev_queue_xmit(skb);
114 } else if (from & AT_INGRESS) {
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800115 dp->st_rx_frm_ing++;
Patrick McHardyc01003c2007-03-29 11:46:52 -0700116 skb_pull(skb, skb->dev->hard_header_len);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800117 netif_rx(skb);
Patrick McHardyc01003c2007-03-29 11:46:52 -0700118 } else
119 BUG();
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800120 }
121
Herbert Xu932ff272006-06-09 12:20:56 -0700122 if (netif_tx_trylock(_dev)) {
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800123 dp->st_rxq_check++;
124 if ((skb = skb_peek(&dp->rq)) == NULL) {
125 dp->tasklet_pending = 0;
126 if (netif_queue_stopped(_dev))
127 netif_wake_queue(_dev);
128 } else {
129 dp->st_rxq_rsch++;
Herbert Xu932ff272006-06-09 12:20:56 -0700130 netif_tx_unlock(_dev);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800131 goto resched;
132 }
Herbert Xu932ff272006-06-09 12:20:56 -0700133 netif_tx_unlock(_dev);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800134 } else {
135resched:
136 dp->tasklet_pending = 1;
137 tasklet_schedule(&dp->ifb_tasklet);
138 }
139
140}
141
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700142static void ifb_setup(struct net_device *dev)
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800143{
144 /* Initialize the device structure. */
145 dev->get_stats = ifb_get_stats;
146 dev->hard_start_xmit = ifb_xmit;
147 dev->open = &ifb_open;
148 dev->stop = &ifb_close;
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700149 dev->destructor = free_netdev;
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800150
151 /* Fill in device structure with ethernet-generic values. */
152 ether_setup(dev);
153 dev->tx_queue_len = TX_Q_LIMIT;
154 dev->change_mtu = NULL;
155 dev->flags |= IFF_NOARP;
156 dev->flags &= ~IFF_MULTICAST;
157 SET_MODULE_OWNER(dev);
158 random_ether_addr(dev->dev_addr);
159}
160
161static int ifb_xmit(struct sk_buff *skb, struct net_device *dev)
162{
163 struct ifb_private *dp = netdev_priv(dev);
164 struct net_device_stats *stats = &dp->stats;
165 int ret = 0;
166 u32 from = G_TC_FROM(skb->tc_verd);
167
dean gaudet3136dcb2007-01-01 19:39:09 -0800168 stats->rx_packets++;
169 stats->rx_bytes+=skb->len;
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800170
Patrick McHardyc01003c2007-03-29 11:46:52 -0700171 if (!(from & (AT_INGRESS|AT_EGRESS)) || !skb->iif) {
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800172 dev_kfree_skb(skb);
173 stats->rx_dropped++;
174 return ret;
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800175 }
176
177 if (skb_queue_len(&dp->rq) >= dev->tx_queue_len) {
178 netif_stop_queue(dev);
179 }
180
181 dev->trans_start = jiffies;
182 skb_queue_tail(&dp->rq, skb);
183 if (!dp->tasklet_pending) {
184 dp->tasklet_pending = 1;
185 tasklet_schedule(&dp->ifb_tasklet);
186 }
187
188 return ret;
189}
190
191static struct net_device_stats *ifb_get_stats(struct net_device *dev)
192{
193 struct ifb_private *dp = netdev_priv(dev);
194 struct net_device_stats *stats = &dp->stats;
195
196 pr_debug("tasklets stats %ld:%ld:%ld:%ld:%ld:%ld:%ld:%ld:%ld \n",
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400197 dp->st_task_enter, dp->st_txq_refl_try, dp->st_rxq_enter,
Zach Brown8057de62006-10-03 01:16:10 -0700198 dp->st_rx2tx_tran, dp->st_rxq_notenter, dp->st_rx_frm_egr,
199 dp->st_rx_frm_ing, dp->st_rxq_check, dp->st_rxq_rsch);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800200
201 return stats;
202}
203
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700204static LIST_HEAD(ifbs);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800205
206/* Number of ifb devices to be set up by this module. */
207module_param(numifbs, int, 0);
208MODULE_PARM_DESC(numifbs, "Number of ifb devices");
209
210static int ifb_close(struct net_device *dev)
211{
212 struct ifb_private *dp = netdev_priv(dev);
213
214 tasklet_kill(&dp->ifb_tasklet);
215 netif_stop_queue(dev);
216 skb_queue_purge(&dp->rq);
217 skb_queue_purge(&dp->tq);
218 return 0;
219}
220
221static int ifb_open(struct net_device *dev)
222{
223 struct ifb_private *dp = netdev_priv(dev);
224
225 tasklet_init(&dp->ifb_tasklet, ri_tasklet, (unsigned long)dev);
226 skb_queue_head_init(&dp->rq);
227 skb_queue_head_init(&dp->tq);
228 netif_start_queue(dev);
229
230 return 0;
231}
232
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700233static int ifb_newlink(struct net_device *dev,
234 struct nlattr *tb[], struct nlattr *data[])
235{
236 struct ifb_private *priv = netdev_priv(dev);
237 int err;
238
239 err = register_netdevice(dev);
240 if (err < 0)
241 return err;
242
243 priv->dev = dev;
244 list_add_tail(&priv->list, &ifbs);
245 return 0;
246}
247
248static void ifb_dellink(struct net_device *dev)
249{
250 struct ifb_private *priv = netdev_priv(dev);
251
252 list_del(&priv->list);
253 unregister_netdevice(dev);
254}
255
256static struct rtnl_link_ops ifb_link_ops __read_mostly = {
257 .kind = "ifb",
258 .priv_size = sizeof(struct ifb_private),
259 .setup = ifb_setup,
260 .newlink = ifb_newlink,
261 .dellink = ifb_dellink,
262};
263
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800264static int __init ifb_init_one(int index)
265{
266 struct net_device *dev_ifb;
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700267 struct ifb_private *priv;
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800268 int err;
269
270 dev_ifb = alloc_netdev(sizeof(struct ifb_private),
271 "ifb%d", ifb_setup);
272
273 if (!dev_ifb)
274 return -ENOMEM;
275
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700276 err = dev_alloc_name(dev_ifb, dev_ifb->name);
277 if (err < 0)
278 goto err;
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800279
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700280 dev_ifb->rtnl_link_ops = &ifb_link_ops;
281 err = register_netdevice(dev_ifb);
282 if (err < 0)
283 goto err;
284
285 priv = netdev_priv(dev_ifb);
286 priv->dev = dev_ifb;
287 list_add_tail(&priv->list, &ifbs);
288 return 0;
289
290err:
291 free_netdev(dev_ifb);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800292 return err;
293}
294
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800295static int __init ifb_init_module(void)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400296{
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700297 struct ifb_private *priv, *next;
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700298 int i, err;
299
300 rtnl_lock();
301 err = __rtnl_link_register(&ifb_link_ops);
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700302
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800303 for (i = 0; i < numifbs && !err; i++)
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400304 err = ifb_init_one(i);
305 if (err) {
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700306 list_for_each_entry_safe(priv, next, &ifbs, list)
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700307 ifb_dellink(priv->dev);
308 __rtnl_link_unregister(&ifb_link_ops);
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800309 }
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700310 rtnl_unlock();
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800311
312 return err;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400313}
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800314
315static void __exit ifb_cleanup_module(void)
316{
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700317 struct ifb_private *priv, *next;
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800318
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700319 rtnl_lock();
Patrick McHardy62b7ffc2007-06-13 12:04:51 -0700320 list_for_each_entry_safe(priv, next, &ifbs, list)
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700321 ifb_dellink(priv->dev);
322
323 __rtnl_link_unregister(&ifb_link_ops);
324 rtnl_unlock();
Jamal Hadi Salim253af422006-01-08 22:34:25 -0800325}
326
327module_init(ifb_init_module);
328module_exit(ifb_cleanup_module);
329MODULE_LICENSE("GPL");
330MODULE_AUTHOR("Jamal Hadi Salim");
Patrick McHardy9ba2cd62007-06-13 12:05:06 -0700331MODULE_ALIAS_RTNL_LINK("ifb");