1 #include <linux/kernel.h>
2 #include <linux/netdevice.h>
3 #include <linux/etherdevice.h>
4 #include <linux/ethtool.h>
5 #include <linux/rcupdate.h>
6 #include <linux/skbuff.h>
7 #include <linux/workqueue.h>
12 struct datapath *dp_dev_get_dp(struct net_device *netdev)
14 return dp_dev_priv(netdev)->dp;
16 EXPORT_SYMBOL(dp_dev_get_dp);
18 static struct net_device_stats *dp_dev_get_stats(struct net_device *netdev)
20 struct dp_dev *dp_dev = dp_dev_priv(netdev);
21 return &dp_dev->stats;
24 int dp_dev_recv(struct net_device *netdev, struct sk_buff *skb)
26 struct dp_dev *dp_dev = dp_dev_priv(netdev);
29 skb->pkt_type = PACKET_HOST;
30 skb->protocol = eth_type_trans(skb, netdev);
35 netdev->last_rx = jiffies;
36 dp_dev->stats.rx_packets++;
37 dp_dev->stats.rx_bytes += len;
41 static int dp_dev_mac_addr(struct net_device *dev, void *p)
43 struct sockaddr *addr = p;
45 if (!is_valid_ether_addr(addr->sa_data))
46 return -EADDRNOTAVAIL;
47 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
51 static int dp_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
53 struct dp_dev *dp_dev = dp_dev_priv(netdev);
55 /* By orphaning 'skb' we will screw up socket accounting slightly, but
56 * the effect is limited to the device queue length. If we don't
57 * do this, then the sk_buff will be destructed eventually, but it is
58 * harder to predict when. */
61 /* We are going to modify 'skb', by sticking it on &dp_dev->xmit_queue,
62 * so we need to have our own clone. (At any rate, fwd_port_input()
63 * will need its own clone, so there's no benefit to queuing any other
65 skb = skb_share_check(skb, GFP_ATOMIC);
69 dp_dev->stats.tx_packets++;
70 dp_dev->stats.tx_bytes += skb->len;
72 if (skb_queue_len(&dp_dev->xmit_queue) >= netdev->tx_queue_len) {
73 /* Queue overflow. Stop transmitter. */
74 netif_stop_queue(netdev);
76 /* We won't see all dropped packets individually, so overrun
77 * error is appropriate. */
78 dp_dev->stats.tx_fifo_errors++;
80 skb_queue_tail(&dp_dev->xmit_queue, skb);
81 netdev->trans_start = jiffies;
83 schedule_work(&dp_dev->xmit_work);
88 static void dp_dev_do_xmit(struct work_struct *work)
90 struct dp_dev *dp_dev = container_of(work, struct dp_dev, xmit_work);
91 struct datapath *dp = dp_dev->dp;
94 while ((skb = skb_dequeue(&dp_dev->xmit_queue)) != NULL) {
95 skb_reset_mac_header(skb);
97 dp_process_received_packet(skb, dp->ports[dp_dev->port_no]);
100 netif_wake_queue(dp_dev->dev);
103 static int dp_dev_open(struct net_device *netdev)
105 netif_start_queue(netdev);
109 static int dp_dev_stop(struct net_device *netdev)
111 netif_stop_queue(netdev);
115 static void dp_getinfo(struct net_device *netdev, struct ethtool_drvinfo *info)
117 struct dp_dev *dp_dev = dp_dev_priv(netdev);
118 strcpy(info->driver, "openvswitch");
119 sprintf(info->bus_info, "%d", dp_dev->dp->dp_idx);
122 static struct ethtool_ops dp_ethtool_ops = {
123 .get_drvinfo = dp_getinfo,
124 .get_link = ethtool_op_get_link,
125 .get_sg = ethtool_op_get_sg,
126 .get_tx_csum = ethtool_op_get_tx_csum,
127 .get_tso = ethtool_op_get_tso,
131 do_setup(struct net_device *netdev)
135 netdev->do_ioctl = dp_ioctl_hook;
136 netdev->get_stats = dp_dev_get_stats;
137 netdev->hard_start_xmit = dp_dev_xmit;
138 netdev->open = dp_dev_open;
139 SET_ETHTOOL_OPS(netdev, &dp_ethtool_ops);
140 netdev->stop = dp_dev_stop;
141 netdev->tx_queue_len = 100;
142 netdev->set_mac_address = dp_dev_mac_addr;
144 netdev->flags = IFF_BROADCAST | IFF_MULTICAST;
146 random_ether_addr(netdev->dev_addr);
148 /* Set the OUI to the Nicira one. */
149 netdev->dev_addr[0] = 0x00;
150 netdev->dev_addr[1] = 0x23;
151 netdev->dev_addr[2] = 0x20;
153 /* Set the top bits to indicate random Nicira address. */
154 netdev->dev_addr[3] |= 0xc0;
157 /* Create a datapath device associated with 'dp'. If 'dp_name' is null,
158 * the device name will be of the form 'of<dp_idx>'. Returns the new device or
161 * Called with RTNL lock and dp_mutex. */
162 struct net_device *dp_dev_create(struct datapath *dp, const char *dp_name, int port_no)
164 struct dp_dev *dp_dev;
165 struct net_device *netdev;
166 char dev_name[IFNAMSIZ];
170 if (strlen(dp_name) >= IFNAMSIZ)
171 return ERR_PTR(-EINVAL);
172 strncpy(dev_name, dp_name, sizeof(dev_name));
174 snprintf(dev_name, sizeof dev_name, "of%d", dp->dp_idx);
176 netdev = alloc_netdev(sizeof(struct dp_dev), dev_name, do_setup);
178 return ERR_PTR(-ENOMEM);
180 err = register_netdevice(netdev);
186 dp_dev = dp_dev_priv(netdev);
188 dp_dev->port_no = port_no;
189 dp_dev->dev = netdev;
190 skb_queue_head_init(&dp_dev->xmit_queue);
191 INIT_WORK(&dp_dev->xmit_work, dp_dev_do_xmit);
195 /* Called with RTNL lock and dp_mutex.*/
196 void dp_dev_destroy(struct net_device *netdev)
198 struct dp_dev *dp_dev = dp_dev_priv(netdev);
200 netif_tx_disable(netdev);
202 skb_queue_purge(&dp_dev->xmit_queue);
203 unregister_netdevice(netdev);
206 int is_dp_dev(struct net_device *netdev)
208 return netdev->open == dp_dev_open;
210 EXPORT_SYMBOL(is_dp_dev);