Revert "datapath: Don't orphan packets in dp_dev transmit path."
[sliver-openvswitch.git] / datapath / dp_dev.c
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ethtool.h>
13 #include <linux/rcupdate.h>
14 #include <linux/skbuff.h>
15 #include <linux/workqueue.h>
16
17 #include "datapath.h"
18 #include "dp_dev.h"
19
20 struct pcpu_lstats {
21         unsigned long rx_packets;
22         unsigned long rx_bytes;
23         unsigned long tx_packets;
24         unsigned long tx_bytes;
25 };
26
27 struct datapath *dp_dev_get_dp(struct net_device *netdev)
28 {
29         return dp_dev_priv(netdev)->dp;
30 }
31 EXPORT_SYMBOL(dp_dev_get_dp);
32
33 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
34 {
35         struct dp_dev *dp_dev = dp_dev_priv(netdev);
36         struct net_device_stats *stats;
37         int i;
38
39         stats = &dp_dev->stats;
40         memset(stats, 0, sizeof *stats);
41         for_each_possible_cpu(i) {
42                 const struct pcpu_lstats *lb_stats;
43
44                 lb_stats = per_cpu_ptr(dp_dev->lstats, i);
45                 stats->rx_bytes   += lb_stats->rx_bytes;
46                 stats->rx_packets += lb_stats->rx_packets;
47                 stats->tx_bytes   += lb_stats->tx_bytes;
48                 stats->tx_packets += lb_stats->tx_packets;
49         }
50         return stats;
51 }
52
53 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb) 
54 {
55         struct dp_dev *dp_dev = dp_dev_priv(netdev);
56         struct pcpu_lstats *lb_stats;
57         int len;
58         len = skb->len;
59         skb->pkt_type = PACKET_HOST;
60         skb->protocol = eth_type_trans(skb, netdev);
61         if (in_interrupt())
62                 netif_rx(skb);
63         else
64                 netif_rx_ni(skb);
65         netdev->last_rx = jiffies;
66         lb_stats = per_cpu_ptr(dp_dev->lstats, smp_processor_id());
67         lb_stats->rx_packets++;
68         lb_stats->rx_bytes += len;
69         return len;
70 }
71
72 static int dp_dev_mac_addr(struct net_device *dev, void *p)
73 {
74         struct sockaddr *addr = p;
75
76         if (!is_valid_ether_addr(addr->sa_data))
77                 return -EADDRNOTAVAIL;
78         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
79         return 0;
80 }
81
82 /* Not reentrant (because it is called with BHs disabled), but may be called
83  * simultaneously on different CPUs. */
84 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
85 {
86         struct dp_dev *dp_dev = dp_dev_priv(netdev);
87         struct pcpu_lstats *lb_stats;
88
89         /* By orphaning 'skb' we will screw up socket accounting slightly, but
90          * the effect is limited to the device queue length.  If we don't
91          * do this, then the sk_buff will be destructed eventually, but it is
92          * harder to predict when. */
93         skb_orphan(skb);
94
95         /* dp_process_received_packet() needs its own clone. */
96         skb = skb_share_check(skb, GFP_ATOMIC);
97         if (!skb)
98                 return 0;
99
100         lb_stats = per_cpu_ptr(dp_dev->lstats, smp_processor_id());
101         lb_stats->tx_packets++;
102         lb_stats->tx_bytes += skb->len;
103
104         skb_reset_mac_header(skb);
105         rcu_read_lock_bh();
106         dp_process_received_packet(skb, dp_dev->dp->ports[dp_dev->port_no]);
107         rcu_read_unlock_bh();
108
109         return 0;
110 }
111
112 static int dp_dev_open(struct net_device *netdev)
113 {
114         netif_start_queue(netdev);
115         return 0;
116 }
117
118 static int dp_dev_stop(struct net_device *netdev)
119 {
120         netif_stop_queue(netdev);
121         return 0;
122 }
123
124 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
125 {
126         struct dp_dev *dp_dev = dp_dev_priv(netdev);
127         strcpy(info->driver, "openvswitch");
128         sprintf(info->bus_info, "%d", dp_dev->dp->dp_idx);
129 }
130
131 static struct ethtool_ops dp_ethtool_ops = {
132         .get_drvinfo = dp_getinfo,
133         .get_link = ethtool_op_get_link,
134         .get_sg = ethtool_op_get_sg,
135         .get_tx_csum = ethtool_op_get_tx_csum,
136         .get_tso = ethtool_op_get_tso,
137 };
138
139 static int dp_dev_init(struct net_device *netdev)
140 {
141         struct dp_dev *dp_dev = dp_dev_priv(netdev);
142
143         dp_dev->lstats = alloc_percpu(struct pcpu_lstats);
144         if (!dp_dev->lstats)
145                 return -ENOMEM;
146
147         return 0;
148 }
149
150 static void dp_dev_free(struct net_device *netdev)
151 {
152         struct dp_dev *dp_dev = dp_dev_priv(netdev);
153
154         free_percpu(dp_dev->lstats);
155         free_netdev(netdev);
156 }
157
158 static void
159 do_setup(struct net_device *netdev)
160 {
161         ether_setup(netdev);
162
163         netdev->do_ioctl = dp_ioctl_hook;
164         netdev->get_stats = dp_dev_get_stats;
165         netdev->hard_start_xmit = dp_dev_xmit;
166         netdev->open = dp_dev_open;
167         SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
168         netdev->stop = dp_dev_stop;
169         netdev->tx_queue_len = 0;
170         netdev->set_mac_address = dp_dev_mac_addr;
171         netdev->init = dp_dev_init;
172         netdev->destructor = dp_dev_free;
173
174         netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
175         netdev->features = NETIF_F_LLTX; /* XXX other features? */
176
177         random_ether_addr(netdev->dev_addr);
178
179         /* Set the OUI to the Nicira one. */
180         netdev->dev_addr[0] = 0x00;
181         netdev->dev_addr[1] = 0x23;
182         netdev->dev_addr[2] = 0x20;
183
184         /* Set the top bits to indicate random Nicira address. */
185         netdev->dev_addr[3] |= 0xc0;
186 }
187
188 /* Create a datapath device associated with 'dp'.  If 'dp_name' is null,
189  * the device name will be of the form 'of<dp_idx>'.  Returns the new device or
190  * an error code.
191  *
192  * Called with RTNL lock and dp_mutex. */
193 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
194 {
195         struct dp_dev *dp_dev;
196         struct net_device *netdev;
197         char dev_name[IFNAMSIZ];
198         int err;
199
200         if (dp_name) {
201                 if (strlen(dp_name) >= IFNAMSIZ)
202                         return ERR_PTR(-EINVAL);
203                 strncpy(dev_name, dp_name, sizeof(dev_name));
204         } else
205                 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
206
207         netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
208         if (!netdev)
209                 return ERR_PTR(-ENOMEM);
210
211         err = register_netdevice(netdev);
212         if (err) {
213                 free_netdev(netdev);
214                 return ERR_PTR(err);
215         }
216
217         dp_dev = dp_dev_priv(netdev);
218         dp_dev->dp = dp;
219         dp_dev->port_no = port_no;
220         dp_dev->dev = netdev;
221         return netdev;
222 }
223
224 /* Called with RTNL lock and dp_mutex.*/
225 void dp_dev_destroy(struct net_device *netdev)
226 {
227         unregister_netdevice(netdev);
228 }
229
230 int is_dp_dev(struct net_device *netdev) 
231 {
232         return netdev->open == dp_dev_open;
233 }
234 EXPORT_SYMBOL(is_dp_dev);