0284f967abcd5959e6ddc425ab116e3cd6a66731
[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 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 /* Not reentrant (because it is called with BHs disabled), but may be called
60  * simultaneously on different CPUs. */
61 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
62 {
63         struct dp_dev *dp_dev = dp_dev_priv(netdev);
64
65         /* By orphaning 'skb' we will screw up socket accounting slightly, but
66          * the effect is limited to the device queue length.  If we don't
67          * do this, then the sk_buff will be destructed eventually, but it is
68          * harder to predict when. */
69         skb_orphan(skb);
70
71         /* dp_process_received_packet() needs its own clone. */
72         skb = skb_share_check(skb, GFP_ATOMIC);
73         if (!skb)
74                 return 0;
75
76         dp_dev->stats.tx_packets++;
77         dp_dev->stats.tx_bytes += skb->len;
78
79         skb_reset_mac_header(skb);
80         rcu_read_lock_bh();
81         dp_process_received_packet(skb, dp_dev->dp->ports[dp_dev->port_no]);
82         rcu_read_unlock_bh();
83
84         return 0;
85 }
86
87 static int dp_dev_open(struct net_device *netdev)
88 {
89         netif_start_queue(netdev);
90         return 0;
91 }
92
93 static int dp_dev_stop(struct net_device *netdev)
94 {
95         netif_stop_queue(netdev);
96         return 0;
97 }
98
99 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
100 {
101         struct dp_dev *dp_dev = dp_dev_priv(netdev);
102         strcpy(info->driver, "openvswitch");
103         sprintf(info->bus_info, "%d", dp_dev->dp->dp_idx);
104 }
105
106 static struct ethtool_ops dp_ethtool_ops = {
107         .get_drvinfo = dp_getinfo,
108         .get_link = ethtool_op_get_link,
109         .get_sg = ethtool_op_get_sg,
110         .get_tx_csum = ethtool_op_get_tx_csum,
111         .get_tso = ethtool_op_get_tso,
112 };
113
114 static void
115 do_setup(struct net_device *netdev)
116 {
117         ether_setup(netdev);
118
119         netdev->do_ioctl = dp_ioctl_hook;
120         netdev->get_stats = dp_dev_get_stats;
121         netdev->hard_start_xmit = dp_dev_xmit;
122         netdev->open = dp_dev_open;
123         SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
124         netdev->stop = dp_dev_stop;
125         netdev->tx_queue_len = 0;
126         netdev->set_mac_address = dp_dev_mac_addr;
127         netdev->destructor = free_netdev;
128
129         netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
130         netdev->features = NETIF_F_LLTX; /* XXX other features? */
131
132         random_ether_addr(netdev->dev_addr);
133
134         /* Set the OUI to the Nicira one. */
135         netdev->dev_addr[0] = 0x00;
136         netdev->dev_addr[1] = 0x23;
137         netdev->dev_addr[2] = 0x20;
138
139         /* Set the top bits to indicate random Nicira address. */
140         netdev->dev_addr[3] |= 0xc0;
141 }
142
143 /* Create a datapath device associated with 'dp'.  If 'dp_name' is null,
144  * the device name will be of the form 'of<dp_idx>'.  Returns the new device or
145  * an error code.
146  *
147  * Called with RTNL lock and dp_mutex. */
148 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
149 {
150         struct dp_dev *dp_dev;
151         struct net_device *netdev;
152         char dev_name[IFNAMSIZ];
153         int err;
154
155         if (dp_name) {
156                 if (strlen(dp_name) >= IFNAMSIZ)
157                         return ERR_PTR(-EINVAL);
158                 strncpy(dev_name, dp_name, sizeof(dev_name));
159         } else
160                 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
161
162         netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
163         if (!netdev)
164                 return ERR_PTR(-ENOMEM);
165
166         err = register_netdevice(netdev);
167         if (err) {
168                 free_netdev(netdev);
169                 return ERR_PTR(err);
170         }
171
172         dp_dev = dp_dev_priv(netdev);
173         dp_dev->dp = dp;
174         dp_dev->port_no = port_no;
175         dp_dev->dev = netdev;
176         return netdev;
177 }
178
179 /* Called with RTNL lock and dp_mutex.*/
180 void dp_dev_destroy(struct net_device *netdev)
181 {
182         unregister_netdevice(netdev);
183 }
184
185 int is_dp_dev(struct net_device *netdev) 
186 {
187         return netdev->open == dp_dev_open;
188 }
189 EXPORT_SYMBOL(is_dp_dev);