Update primary code license to Apache 2.0.
[cascardo/ovs.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 datapath *dp_dev_get_dp(struct net_device *netdev)
21 {
22         return dp_dev_priv(netdev)->dp;
23 }
24 EXPORT_SYMBOL(dp_dev_get_dp);
25
26 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
27 {
28         struct dp_dev *dp_dev = dp_dev_priv(netdev);
29         return &dp_dev->stats;
30 }
31
32 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb) 
33 {
34         struct dp_dev *dp_dev = dp_dev_priv(netdev);
35         int len;
36         len = skb->len;
37         skb->pkt_type = PACKET_HOST;
38         skb->protocol = eth_type_trans(skb, netdev);
39         if (in_interrupt())
40                 netif_rx(skb);
41         else
42                 netif_rx_ni(skb);
43         netdev->last_rx = jiffies;
44         dp_dev->stats.rx_packets++;
45         dp_dev->stats.rx_bytes += len;
46         return len;
47 }
48
49 static int dp_dev_mac_addr(struct net_device *dev, void *p)
50 {
51         struct sockaddr *addr = p;
52
53         if (!is_valid_ether_addr(addr->sa_data))
54                 return -EADDRNOTAVAIL;
55         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
56         return 0;
57 }
58
59 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
60 {
61         struct dp_dev *dp_dev = dp_dev_priv(netdev);
62
63         /* By orphaning 'skb' we will screw up socket accounting slightly, but
64          * the effect is limited to the device queue length.  If we don't
65          * do this, then the sk_buff will be destructed eventually, but it is
66          * harder to predict when. */
67         skb_orphan(skb);
68
69         /* We are going to modify 'skb', by sticking it on &dp_dev->xmit_queue,
70          * so we need to have our own clone.  (At any rate, fwd_port_input()
71          * will need its own clone, so there's no benefit to queuing any other
72          * way.) */
73         skb = skb_share_check(skb, GFP_ATOMIC);
74         if (!skb)
75                 return 0;
76
77         dp_dev->stats.tx_packets++;
78         dp_dev->stats.tx_bytes += skb->len;
79
80         if (skb_queue_len(&dp_dev->xmit_queue) >= netdev->tx_queue_len) {
81                 /* Queue overflow.  Stop transmitter. */
82                 netif_stop_queue(netdev);
83
84                 /* We won't see all dropped packets individually, so overrun
85                  * error is appropriate. */
86                 dp_dev->stats.tx_fifo_errors++;
87         }
88         skb_queue_tail(&dp_dev->xmit_queue, skb);
89         netdev->trans_start = jiffies;
90
91         schedule_work(&dp_dev->xmit_work);
92
93         return 0;
94 }
95
96 static void dp_dev_do_xmit(struct work_struct *work)
97 {
98         struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
99         struct datapath *dp = dp_dev->dp;
100         struct sk_buff *skb;
101
102         while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
103                 skb_reset_mac_header(skb);
104                 rcu_read_lock_bh();
105                 dp_process_received_packet(skb, dp->ports[dp_dev->port_no]);
106                 rcu_read_unlock_bh();
107         }
108         netif_wake_queue(dp_dev->dev);
109 }
110
111 static int dp_dev_open(struct net_device *netdev)
112 {
113         netif_start_queue(netdev);
114         return 0;
115 }
116
117 static int dp_dev_stop(struct net_device *netdev)
118 {
119         netif_stop_queue(netdev);
120         return 0;
121 }
122
123 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
124 {
125         struct dp_dev *dp_dev = dp_dev_priv(netdev);
126         strcpy(info->driver, "openvswitch");
127         sprintf(info->bus_info, "%d", dp_dev->dp->dp_idx);
128 }
129
130 static struct ethtool_ops dp_ethtool_ops = {
131         .get_drvinfo = dp_getinfo,
132         .get_link = ethtool_op_get_link,
133         .get_sg = ethtool_op_get_sg,
134         .get_tx_csum = ethtool_op_get_tx_csum,
135         .get_tso = ethtool_op_get_tso,
136 };
137
138 static void
139 do_setup(struct net_device *netdev)
140 {
141         ether_setup(netdev);
142
143         netdev->do_ioctl = dp_ioctl_hook;
144         netdev->get_stats = dp_dev_get_stats;
145         netdev->hard_start_xmit = dp_dev_xmit;
146         netdev->open = dp_dev_open;
147         SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
148         netdev->stop = dp_dev_stop;
149         netdev->tx_queue_len = 100;
150         netdev->set_mac_address = dp_dev_mac_addr;
151
152         netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
153
154         random_ether_addr(netdev->dev_addr);
155
156         /* Set the OUI to the Nicira one. */
157         netdev->dev_addr[0] = 0x00;
158         netdev->dev_addr[1] = 0x23;
159         netdev->dev_addr[2] = 0x20;
160
161         /* Set the top bits to indicate random Nicira address. */
162         netdev->dev_addr[3] |= 0xc0;
163 }
164
165 /* Create a datapath device associated with 'dp'.  If 'dp_name' is null,
166  * the device name will be of the form 'of<dp_idx>'.  Returns the new device or
167  * an error code.
168  *
169  * Called with RTNL lock and dp_mutex. */
170 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
171 {
172         struct dp_dev *dp_dev;
173         struct net_device *netdev;
174         char dev_name[IFNAMSIZ];
175         int err;
176
177         if (dp_name) {
178                 if (strlen(dp_name) >= IFNAMSIZ)
179                         return ERR_PTR(-EINVAL);
180                 strncpy(dev_name, dp_name, sizeof(dev_name));
181         } else
182                 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
183
184         netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
185         if (!netdev)
186                 return ERR_PTR(-ENOMEM);
187
188         err = register_netdevice(netdev);
189         if (err) {
190                 free_netdev(netdev);
191                 return ERR_PTR(err);
192         }
193
194         dp_dev = dp_dev_priv(netdev);
195         dp_dev->dp = dp;
196         dp_dev->port_no = port_no;
197         dp_dev->dev = netdev;
198         skb_queue_head_init(&dp_dev->xmit_queue);
199         INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
200         return netdev;
201 }
202
203 /* Called with RTNL lock and dp_mutex.*/
204 void dp_dev_destroy(struct net_device *netdev)
205 {
206         struct dp_dev *dp_dev = dp_dev_priv(netdev);
207
208         netif_tx_disable(netdev);
209         synchronize_net();
210         skb_queue_purge(&dp_dev->xmit_queue);
211         unregister_netdevice(netdev);
212 }
213
214 int is_dp_dev(struct net_device *netdev) 
215 {
216         return netdev->open == dp_dev_open;
217 }
218 EXPORT_SYMBOL(is_dp_dev);