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>
20 struct datapath *dp_dev_get_dp(struct net_device *netdev)
22 return dp_dev_priv(netdev)->dp;
24 EXPORT_SYMBOL(dp_dev_get_dp);
26 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
28 struct dp_dev *dp_dev = dp_dev_priv(netdev);
29 return &dp_dev->stats;
32 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb)
34 struct dp_dev *dp_dev = dp_dev_priv(netdev);
37 skb->pkt_type = PACKET_HOST;
38 skb->protocol = eth_type_trans(skb, netdev);
43 netdev->last_rx = jiffies;
44 dp_dev->stats.rx_packets++;
45 dp_dev->stats.rx_bytes += len;
49 static int dp_dev_mac_addr(struct net_device *dev, void *p)
51 struct sockaddr *addr = p;
53 if (!is_valid_ether_addr(addr->sa_data))
54 return -EADDRNOTAVAIL;
55 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
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)
63 struct dp_dev *dp_dev = dp_dev_priv(netdev);
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. */
71 /* dp_process_received_packet() needs its own clone. */
72 skb = skb_share_check(skb, GFP_ATOMIC);
76 dp_dev->stats.tx_packets++;
77 dp_dev->stats.tx_bytes += skb->len;
79 skb_reset_mac_header(skb);
81 dp_process_received_packet(skb, dp_dev->dp->ports[dp_dev->port_no]);
87 static int dp_dev_open(struct net_device *netdev)
89 netif_start_queue(netdev);
93 static int dp_dev_stop(struct net_device *netdev)
95 netif_stop_queue(netdev);
99 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
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);
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,
115 do_setup(struct net_device *netdev)
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;
129 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
130 netdev->features = NETIF_F_LLTX; /* XXX other features? */
132 random_ether_addr(netdev->dev_addr);
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;
139 /* Set the top bits to indicate random Nicira address. */
140 netdev->dev_addr[3] |= 0xc0;
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
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)
150 struct dp_dev *dp_dev;
151 struct net_device *netdev;
152 char dev_name[IFNAMSIZ];
156 if (strlen(dp_name) >= IFNAMSIZ)
157 return ERR_PTR(-EINVAL);
158 strncpy(dev_name, dp_name, sizeof(dev_name));
160 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
162 netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
164 return ERR_PTR(-ENOMEM);
166 err = register_netdevice(netdev);
172 dp_dev = dp_dev_priv(netdev);
174 dp_dev->port_no = port_no;
175 dp_dev->dev = netdev;
179 /* Called with RTNL lock and dp_mutex.*/
180 void dp_dev_destroy(struct net_device *netdev)
182 unregister_netdevice(netdev);
185 int is_dp_dev(struct net_device *netdev)
187 return netdev->open == dp_dev_open;
189 EXPORT_SYMBOL(is_dp_dev);