2 * Copyright (c) 2009 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
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>
21 unsigned long rx_packets;
22 unsigned long rx_bytes;
23 unsigned long tx_packets;
24 unsigned long tx_bytes;
27 struct datapath *dp_dev_get_dp(struct net_device *netdev)
29 return dp_dev_priv(netdev)->dp;
31 EXPORT_SYMBOL(dp_dev_get_dp);
33 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
35 struct dp_dev *dp_dev = dp_dev_priv(netdev);
36 struct net_device_stats *stats;
39 stats = &dp_dev->stats;
40 memset(stats, 0, sizeof *stats);
41 for_each_possible_cpu(i) {
42 const struct pcpu_lstats *lb_stats;
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;
53 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb)
55 struct dp_dev *dp_dev = dp_dev_priv(netdev);
56 struct pcpu_lstats *lb_stats;
59 skb->pkt_type = PACKET_HOST;
60 skb->protocol = eth_type_trans(skb, netdev);
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;
72 static int dp_dev_mac_addr(struct net_device *dev, void *p)
74 struct sockaddr *addr = p;
76 if (!is_valid_ether_addr(addr->sa_data))
77 return -EADDRNOTAVAIL;
78 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
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)
86 struct dp_dev *dp_dev = dp_dev_priv(netdev);
87 struct pcpu_lstats *lb_stats;
89 /* dp_process_received_packet() needs its own clone. */
90 skb = skb_share_check(skb, GFP_ATOMIC);
94 lb_stats = per_cpu_ptr(dp_dev->lstats, smp_processor_id());
95 lb_stats->tx_packets++;
96 lb_stats->tx_bytes += skb->len;
98 skb_reset_mac_header(skb);
100 dp_process_received_packet(skb, dp_dev->dp->ports[dp_dev->port_no]);
101 rcu_read_unlock_bh();
106 static int dp_dev_open(struct net_device *netdev)
108 netif_start_queue(netdev);
112 static int dp_dev_stop(struct net_device *netdev)
114 netif_stop_queue(netdev);
118 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
120 struct dp_dev *dp_dev = dp_dev_priv(netdev);
121 strcpy(info->driver, "openvswitch");
122 sprintf(info->bus_info, "%d", dp_dev->dp->dp_idx);
125 static struct ethtool_ops dp_ethtool_ops = {
126 .get_drvinfo = dp_getinfo,
127 .get_link = ethtool_op_get_link,
128 .get_sg = ethtool_op_get_sg,
129 .get_tx_csum = ethtool_op_get_tx_csum,
130 .get_tso = ethtool_op_get_tso,
133 static int dp_dev_change_mtu(struct net_device *dev, int new_mtu)
135 if (new_mtu < 68 || new_mtu > dp_min_mtu(dp_dev_get_dp(dev)))
142 static int dp_dev_init(struct net_device *netdev)
144 struct dp_dev *dp_dev = dp_dev_priv(netdev);
146 dp_dev->lstats = alloc_percpu(struct pcpu_lstats);
153 static void dp_dev_free(struct net_device *netdev)
155 struct dp_dev *dp_dev = dp_dev_priv(netdev);
157 free_percpu(dp_dev->lstats);
162 do_setup(struct net_device *netdev)
166 netdev->do_ioctl = dp_ioctl_hook;
167 netdev->get_stats = dp_dev_get_stats;
168 netdev->hard_start_xmit = dp_dev_xmit;
169 netdev->open = dp_dev_open;
170 SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
171 netdev->stop = dp_dev_stop;
172 netdev->tx_queue_len = 0;
173 netdev->set_mac_address = dp_dev_mac_addr;
174 netdev->change_mtu = dp_dev_change_mtu;
175 netdev->init = dp_dev_init;
176 netdev->destructor = dp_dev_free;
178 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
179 netdev->features = NETIF_F_LLTX; /* XXX other features? */
181 random_ether_addr(netdev->dev_addr);
183 /* Set the OUI to the Nicira one. */
184 netdev->dev_addr[0] = 0x00;
185 netdev->dev_addr[1] = 0x23;
186 netdev->dev_addr[2] = 0x20;
188 /* Set the top bits to indicate random Nicira address. */
189 netdev->dev_addr[3] |= 0xc0;
192 /* Create a datapath device associated with 'dp'. If 'dp_name' is null,
193 * the device name will be of the form 'of<dp_idx>'. Returns the new device or
196 * Called with RTNL lock and dp_mutex. */
197 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
199 struct dp_dev *dp_dev;
200 struct net_device *netdev;
201 char dev_name[IFNAMSIZ];
205 if (strlen(dp_name) >= IFNAMSIZ)
206 return ERR_PTR(-EINVAL);
207 strncpy(dev_name, dp_name, sizeof(dev_name));
209 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
211 netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
213 return ERR_PTR(-ENOMEM);
215 err = register_netdevice(netdev);
221 dp_dev = dp_dev_priv(netdev);
223 dp_dev->port_no = port_no;
224 dp_dev->dev = netdev;
228 /* Called with RTNL lock and dp_mutex.*/
229 void dp_dev_destroy(struct net_device *netdev)
231 unregister_netdevice(netdev);
234 int is_dp_dev(struct net_device *netdev)
236 return netdev->open == dp_dev_open;
238 EXPORT_SYMBOL(is_dp_dev);