• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007-2012 Nicira, Inc.
4  */
5 
6 #include <linux/if_vlan.h>
7 #include <linux/kernel.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/ethtool.h>
11 #include <linux/skbuff.h>
12 
13 #include <net/dst.h>
14 #include <net/xfrm.h>
15 #include <net/rtnetlink.h>
16 
17 #include "datapath.h"
18 #include "vport-internal_dev.h"
19 #include "vport-netdev.h"
20 
21 struct internal_dev {
22 	struct vport *vport;
23 };
24 
25 static struct vport_ops ovs_internal_vport_ops;
26 
internal_dev_priv(struct net_device * netdev)27 static struct internal_dev *internal_dev_priv(struct net_device *netdev)
28 {
29 	return netdev_priv(netdev);
30 }
31 
32 /* Called with rcu_read_lock_bh. */
33 static netdev_tx_t
internal_dev_xmit(struct sk_buff * skb,struct net_device * netdev)34 internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
35 {
36 	int len, err;
37 
38 	len = skb->len;
39 	rcu_read_lock();
40 	err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL);
41 	rcu_read_unlock();
42 
43 	if (likely(!err)) {
44 		struct pcpu_sw_netstats *tstats = this_cpu_ptr(netdev->tstats);
45 
46 		u64_stats_update_begin(&tstats->syncp);
47 		tstats->tx_bytes += len;
48 		tstats->tx_packets++;
49 		u64_stats_update_end(&tstats->syncp);
50 	} else {
51 		netdev->stats.tx_errors++;
52 	}
53 	return NETDEV_TX_OK;
54 }
55 
internal_dev_open(struct net_device * netdev)56 static int internal_dev_open(struct net_device *netdev)
57 {
58 	netif_start_queue(netdev);
59 	return 0;
60 }
61 
internal_dev_stop(struct net_device * netdev)62 static int internal_dev_stop(struct net_device *netdev)
63 {
64 	netif_stop_queue(netdev);
65 	return 0;
66 }
67 
internal_dev_getinfo(struct net_device * netdev,struct ethtool_drvinfo * info)68 static void internal_dev_getinfo(struct net_device *netdev,
69 				 struct ethtool_drvinfo *info)
70 {
71 	strlcpy(info->driver, "openvswitch", sizeof(info->driver));
72 }
73 
74 static const struct ethtool_ops internal_dev_ethtool_ops = {
75 	.get_drvinfo	= internal_dev_getinfo,
76 	.get_link	= ethtool_op_get_link,
77 };
78 
internal_dev_destructor(struct net_device * dev)79 static void internal_dev_destructor(struct net_device *dev)
80 {
81 	struct vport *vport = ovs_internal_dev_get_vport(dev);
82 
83 	ovs_vport_free(vport);
84 }
85 
86 static void
internal_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)87 internal_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
88 {
89 	memset(stats, 0, sizeof(*stats));
90 	stats->rx_errors  = dev->stats.rx_errors;
91 	stats->tx_errors  = dev->stats.tx_errors;
92 	stats->tx_dropped = dev->stats.tx_dropped;
93 	stats->rx_dropped = dev->stats.rx_dropped;
94 
95 	dev_fetch_sw_netstats(stats, dev->tstats);
96 }
97 
98 static const struct net_device_ops internal_dev_netdev_ops = {
99 	.ndo_open = internal_dev_open,
100 	.ndo_stop = internal_dev_stop,
101 	.ndo_start_xmit = internal_dev_xmit,
102 	.ndo_set_mac_address = eth_mac_addr,
103 	.ndo_get_stats64 = internal_get_stats,
104 };
105 
106 static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
107 	.kind = "openvswitch",
108 };
109 
do_setup(struct net_device * netdev)110 static void do_setup(struct net_device *netdev)
111 {
112 	ether_setup(netdev);
113 
114 	netdev->max_mtu = ETH_MAX_MTU;
115 
116 	netdev->netdev_ops = &internal_dev_netdev_ops;
117 
118 	netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
119 	netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH |
120 			      IFF_NO_QUEUE;
121 	netdev->needs_free_netdev = true;
122 	netdev->priv_destructor = NULL;
123 	netdev->ethtool_ops = &internal_dev_ethtool_ops;
124 	netdev->rtnl_link_ops = &internal_dev_link_ops;
125 
126 	netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
127 			   NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
128 			   NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
129 
130 	netdev->vlan_features = netdev->features;
131 	netdev->hw_enc_features = netdev->features;
132 	netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
133 	netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
134 
135 	eth_hw_addr_random(netdev);
136 }
137 
internal_dev_create(const struct vport_parms * parms)138 static struct vport *internal_dev_create(const struct vport_parms *parms)
139 {
140 	struct vport *vport;
141 	struct internal_dev *internal_dev;
142 	struct net_device *dev;
143 	int err;
144 
145 	vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms);
146 	if (IS_ERR(vport)) {
147 		err = PTR_ERR(vport);
148 		goto error;
149 	}
150 
151 	dev = alloc_netdev(sizeof(struct internal_dev),
152 			   parms->name, NET_NAME_USER, do_setup);
153 	vport->dev = dev;
154 	if (!vport->dev) {
155 		err = -ENOMEM;
156 		goto error_free_vport;
157 	}
158 	vport->dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
159 	if (!vport->dev->tstats) {
160 		err = -ENOMEM;
161 		goto error_free_netdev;
162 	}
163 
164 	dev_net_set(vport->dev, ovs_dp_get_net(vport->dp));
165 	internal_dev = internal_dev_priv(vport->dev);
166 	internal_dev->vport = vport;
167 
168 	/* Restrict bridge port to current netns. */
169 	if (vport->port_no == OVSP_LOCAL)
170 		vport->dev->features |= NETIF_F_NETNS_LOCAL;
171 
172 	rtnl_lock();
173 	err = register_netdevice(vport->dev);
174 	if (err)
175 		goto error_unlock;
176 	vport->dev->priv_destructor = internal_dev_destructor;
177 
178 	dev_set_promiscuity(vport->dev, 1);
179 	rtnl_unlock();
180 	netif_start_queue(vport->dev);
181 
182 	return vport;
183 
184 error_unlock:
185 	rtnl_unlock();
186 	free_percpu(dev->tstats);
187 error_free_netdev:
188 	free_netdev(dev);
189 error_free_vport:
190 	ovs_vport_free(vport);
191 error:
192 	return ERR_PTR(err);
193 }
194 
internal_dev_destroy(struct vport * vport)195 static void internal_dev_destroy(struct vport *vport)
196 {
197 	netif_stop_queue(vport->dev);
198 	rtnl_lock();
199 	dev_set_promiscuity(vport->dev, -1);
200 
201 	/* unregister_netdevice() waits for an RCU grace period. */
202 	unregister_netdevice(vport->dev);
203 	free_percpu(vport->dev->tstats);
204 	rtnl_unlock();
205 }
206 
internal_dev_recv(struct sk_buff * skb)207 static netdev_tx_t internal_dev_recv(struct sk_buff *skb)
208 {
209 	struct net_device *netdev = skb->dev;
210 
211 	if (unlikely(!(netdev->flags & IFF_UP))) {
212 		kfree_skb(skb);
213 		netdev->stats.rx_dropped++;
214 		return NETDEV_TX_OK;
215 	}
216 
217 	skb_dst_drop(skb);
218 	nf_reset_ct(skb);
219 	secpath_reset(skb);
220 
221 	skb->pkt_type = PACKET_HOST;
222 	skb->protocol = eth_type_trans(skb, netdev);
223 	skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
224 	dev_sw_netstats_rx_add(netdev, skb->len);
225 
226 	netif_rx(skb);
227 	return NETDEV_TX_OK;
228 }
229 
230 static struct vport_ops ovs_internal_vport_ops = {
231 	.type		= OVS_VPORT_TYPE_INTERNAL,
232 	.create		= internal_dev_create,
233 	.destroy	= internal_dev_destroy,
234 	.send		= internal_dev_recv,
235 };
236 
ovs_is_internal_dev(const struct net_device * netdev)237 int ovs_is_internal_dev(const struct net_device *netdev)
238 {
239 	return netdev->netdev_ops == &internal_dev_netdev_ops;
240 }
241 
ovs_internal_dev_get_vport(struct net_device * netdev)242 struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
243 {
244 	if (!ovs_is_internal_dev(netdev))
245 		return NULL;
246 
247 	return internal_dev_priv(netdev)->vport;
248 }
249 
ovs_internal_dev_rtnl_link_register(void)250 int ovs_internal_dev_rtnl_link_register(void)
251 {
252 	int err;
253 
254 	err = rtnl_link_register(&internal_dev_link_ops);
255 	if (err < 0)
256 		return err;
257 
258 	err = ovs_vport_ops_register(&ovs_internal_vport_ops);
259 	if (err < 0)
260 		rtnl_link_unregister(&internal_dev_link_ops);
261 
262 	return err;
263 }
264 
ovs_internal_dev_rtnl_link_unregister(void)265 void ovs_internal_dev_rtnl_link_unregister(void)
266 {
267 	ovs_vport_ops_unregister(&ovs_internal_vport_ops);
268 	rtnl_link_unregister(&internal_dev_link_ops);
269 }
270