]>
Commit | Line | Data |
---|---|---|
f2459fe7 | 1 | /* |
e23775f2 | 2 | * Copyright (c) 2007-2015 Nicira, Inc. |
f2459fe7 | 3 | * |
a9a29d22 JG |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
f2459fe7 JG |
17 | */ |
18 | ||
88211dda | 19 | #include <linux/hardirq.h> |
6ce39213 | 20 | #include <linux/if_vlan.h> |
f2459fe7 JG |
21 | #include <linux/kernel.h> |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/etherdevice.h> | |
24 | #include <linux/ethtool.h> | |
f2459fe7 | 25 | #include <linux/skbuff.h> |
e23775f2 PS |
26 | #include <linux/percpu.h> |
27 | #include <linux/u64_stats_sync.h> | |
28 | #include <linux/netdev_features.h> | |
f2459fe7 | 29 | |
53e6421b JG |
30 | #include <net/dst.h> |
31 | #include <net/xfrm.h> | |
e23775f2 | 32 | #include <net/rtnetlink.h> |
53e6421b | 33 | |
f2459fe7 JG |
34 | #include "datapath.h" |
35 | #include "vport-internal_dev.h" | |
36 | #include "vport-netdev.h" | |
37 | ||
f2459fe7 | 38 | struct internal_dev { |
7237e4f4 | 39 | struct vport *vport; |
f2459fe7 JG |
40 | }; |
41 | ||
5a38795f TG |
42 | static struct vport_ops ovs_internal_vport_ops; |
43 | ||
6455100f | 44 | static struct internal_dev *internal_dev_priv(struct net_device *netdev) |
f2459fe7 JG |
45 | { |
46 | return netdev_priv(netdev); | |
47 | } | |
48 | ||
8a5d84f6 | 49 | /* Called with rcu_read_lock_bh. */ |
f2459fe7 JG |
50 | static int internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev) |
51 | { | |
e23775f2 PS |
52 | int len, err; |
53 | ||
54 | len = skb->len; | |
8a5d84f6 | 55 | rcu_read_lock(); |
e23775f2 | 56 | err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL); |
8a5d84f6 | 57 | rcu_read_unlock(); |
e23775f2 PS |
58 | |
59 | if (likely(!err)) { | |
60 | #ifdef HAVE_DEV_TSTATS | |
61 | struct pcpu_sw_netstats *tstats; | |
62 | ||
63 | tstats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)netdev->tstats); | |
64 | ||
65 | u64_stats_update_begin(&tstats->syncp); | |
66 | tstats->tx_bytes += len; | |
67 | tstats->tx_packets++; | |
68 | u64_stats_update_end(&tstats->syncp); | |
69 | #endif | |
70 | } else { | |
71 | netdev->stats.tx_errors++; | |
72 | } | |
f2459fe7 JG |
73 | return 0; |
74 | } | |
75 | ||
76 | static int internal_dev_open(struct net_device *netdev) | |
77 | { | |
78 | netif_start_queue(netdev); | |
79 | return 0; | |
80 | } | |
81 | ||
82 | static int internal_dev_stop(struct net_device *netdev) | |
83 | { | |
84 | netif_stop_queue(netdev); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | static void internal_dev_getinfo(struct net_device *netdev, | |
89 | struct ethtool_drvinfo *info) | |
90 | { | |
c58a0a6b | 91 | strlcpy(info->driver, "openvswitch", sizeof(info->driver)); |
f2459fe7 JG |
92 | } |
93 | ||
b279fccf | 94 | static const struct ethtool_ops internal_dev_ethtool_ops = { |
f4267e34 JG |
95 | .get_drvinfo = internal_dev_getinfo, |
96 | .get_link = ethtool_op_get_link, | |
6f17885b | 97 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39) |
f4267e34 JG |
98 | .get_sg = ethtool_op_get_sg, |
99 | .set_sg = ethtool_op_set_sg, | |
100 | .get_tx_csum = ethtool_op_get_tx_csum, | |
101 | .set_tx_csum = ethtool_op_set_tx_hw_csum, | |
102 | .get_tso = ethtool_op_get_tso, | |
103 | .set_tso = ethtool_op_set_tso, | |
6f17885b | 104 | #endif |
f2459fe7 JG |
105 | }; |
106 | ||
107 | static int internal_dev_change_mtu(struct net_device *netdev, int new_mtu) | |
108 | { | |
f2459fe7 JG |
109 | if (new_mtu < 68) |
110 | return -EINVAL; | |
111 | ||
f2459fe7 JG |
112 | netdev->mtu = new_mtu; |
113 | return 0; | |
114 | } | |
115 | ||
8338302d JG |
116 | static void internal_dev_destructor(struct net_device *dev) |
117 | { | |
850b6b3b | 118 | struct vport *vport = ovs_internal_dev_get_vport(dev); |
8338302d | 119 | |
850b6b3b | 120 | ovs_vport_free(vport); |
8338302d JG |
121 | free_netdev(dev); |
122 | } | |
123 | ||
e23775f2 PS |
124 | #ifdef HAVE_DEV_TSTATS |
125 | static int internal_dev_init(struct net_device *dev) | |
126 | { | |
127 | dev->tstats = (typeof(dev->tstats)) netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); | |
128 | if (!dev->tstats) | |
129 | return -ENOMEM; | |
130 | return 0; | |
131 | } | |
132 | ||
133 | static void internal_dev_uninit(struct net_device *dev) | |
134 | { | |
135 | free_percpu(dev->tstats); | |
136 | } | |
137 | #endif | |
138 | ||
f2459fe7 | 139 | static const struct net_device_ops internal_dev_netdev_ops = { |
e23775f2 PS |
140 | #ifdef HAVE_DEV_TSTATS |
141 | .ndo_init = internal_dev_init, | |
142 | .ndo_uninit = internal_dev_uninit, | |
143 | .ndo_get_stats64 = ip_tunnel_get_stats64, | |
144 | #endif | |
f2459fe7 JG |
145 | .ndo_open = internal_dev_open, |
146 | .ndo_stop = internal_dev_stop, | |
147 | .ndo_start_xmit = internal_dev_xmit, | |
faa61e25 | 148 | .ndo_set_mac_address = eth_mac_addr, |
f2459fe7 | 149 | .ndo_change_mtu = internal_dev_change_mtu, |
f2459fe7 | 150 | }; |
f2459fe7 | 151 | |
5282e284 TG |
152 | static struct rtnl_link_ops internal_dev_link_ops __read_mostly = { |
153 | .kind = "openvswitch", | |
154 | }; | |
155 | ||
fceb2a5b | 156 | static void do_setup(struct net_device *netdev) |
f2459fe7 JG |
157 | { |
158 | ether_setup(netdev); | |
159 | ||
f2459fe7 | 160 | netdev->netdev_ops = &internal_dev_netdev_ops; |
f2459fe7 | 161 | |
9c8482e9 | 162 | netdev->priv_flags &= ~IFF_TX_SKB_SHARING; |
e23775f2 | 163 | netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH; |
8338302d | 164 | netdev->destructor = internal_dev_destructor; |
f6a0c895 | 165 | netdev->ethtool_ops = &internal_dev_ethtool_ops; |
5282e284 | 166 | netdev->rtnl_link_ops = &internal_dev_link_ops; |
f2459fe7 JG |
167 | netdev->tx_queue_len = 0; |
168 | ||
8e6c8ff5 | 169 | netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST | |
c7d084d6 PS |
170 | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | |
171 | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL; | |
f2459fe7 | 172 | |
926ea16e | 173 | netdev->vlan_features = netdev->features; |
9b764edf | 174 | netdev->features |= NETIF_F_HW_VLAN_CTAG_TX; |
926ea16e | 175 | |
c7d084d6 PS |
176 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0) |
177 | netdev->hw_enc_features = netdev->features; | |
178 | #endif | |
e23775f2 PS |
179 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39) |
180 | netdev->hw_features = netdev->features & ~NETIF_F_LLTX; | |
181 | #endif | |
55053441 | 182 | eth_hw_addr_random(netdev); |
f2459fe7 JG |
183 | } |
184 | ||
94903c98 | 185 | static struct vport *internal_dev_create(const struct vport_parms *parms) |
f2459fe7 JG |
186 | { |
187 | struct vport *vport; | |
f2459fe7 JG |
188 | struct internal_dev *internal_dev; |
189 | int err; | |
190 | ||
e23775f2 | 191 | vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms); |
f2459fe7 JG |
192 | if (IS_ERR(vport)) { |
193 | err = PTR_ERR(vport); | |
194 | goto error; | |
195 | } | |
196 | ||
e23775f2 PS |
197 | vport->dev = alloc_netdev(sizeof(struct internal_dev), |
198 | parms->name, NET_NAME_UNKNOWN, do_setup); | |
199 | if (!vport->dev) { | |
f2459fe7 JG |
200 | err = -ENOMEM; |
201 | goto error_free_vport; | |
202 | } | |
203 | ||
e23775f2 PS |
204 | dev_net_set(vport->dev, ovs_dp_get_net(vport->dp)); |
205 | internal_dev = internal_dev_priv(vport->dev); | |
c052da84 | 206 | internal_dev->vport = vport; |
f2459fe7 | 207 | |
2a4999f3 PS |
208 | /* Restrict bridge port to current netns. */ |
209 | if (vport->port_no == OVSP_LOCAL) | |
e23775f2 | 210 | vport->dev->features |= NETIF_F_NETNS_LOCAL; |
2a4999f3 | 211 | |
cd2a59e9 | 212 | rtnl_lock(); |
e23775f2 | 213 | err = register_netdevice(vport->dev); |
f2459fe7 JG |
214 | if (err) |
215 | goto error_free_netdev; | |
216 | ||
e23775f2 | 217 | dev_set_promiscuity(vport->dev, 1); |
cd2a59e9 | 218 | rtnl_unlock(); |
e23775f2 | 219 | netif_start_queue(vport->dev); |
7237e4f4 | 220 | |
f2459fe7 JG |
221 | return vport; |
222 | ||
223 | error_free_netdev: | |
cd2a59e9 | 224 | rtnl_unlock(); |
e23775f2 | 225 | free_netdev(vport->dev); |
f2459fe7 | 226 | error_free_vport: |
850b6b3b | 227 | ovs_vport_free(vport); |
f2459fe7 JG |
228 | error: |
229 | return ERR_PTR(err); | |
230 | } | |
231 | ||
3544358a | 232 | static void internal_dev_destroy(struct vport *vport) |
f2459fe7 | 233 | { |
e23775f2 | 234 | netif_stop_queue(vport->dev); |
cd2a59e9 | 235 | rtnl_lock(); |
e23775f2 | 236 | dev_set_promiscuity(vport->dev, -1); |
7237e4f4 | 237 | |
057dd6d2 | 238 | /* unregister_netdevice() waits for an RCU grace period. */ |
e23775f2 | 239 | unregister_netdevice(vport->dev); |
cd2a59e9 PS |
240 | |
241 | rtnl_unlock(); | |
f2459fe7 JG |
242 | } |
243 | ||
e23775f2 | 244 | static netdev_tx_t internal_dev_recv(struct sk_buff *skb) |
f2459fe7 | 245 | { |
e23775f2 PS |
246 | struct net_device *netdev = skb->dev; |
247 | #ifdef HAVE_DEV_TSTATS | |
248 | struct pcpu_sw_netstats *stats; | |
249 | #endif | |
f2459fe7 | 250 | |
0077a780 CL |
251 | if (unlikely(!(netdev->flags & IFF_UP))) { |
252 | kfree_skb(skb); | |
e23775f2 PS |
253 | netdev->stats.rx_dropped++; |
254 | return NETDEV_TX_OK; | |
0077a780 CL |
255 | } |
256 | ||
6ce39213 | 257 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37) |
efd8a18e | 258 | if (skb_vlan_tag_present(skb)) { |
1f649f1c TG |
259 | if (unlikely(!vlan_insert_tag_set_proto(skb, |
260 | skb->vlan_proto, | |
efd8a18e | 261 | skb_vlan_tag_get(skb)))) |
e23775f2 | 262 | return NETDEV_TX_OK; |
07ac71ea PS |
263 | |
264 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
265 | skb->csum = csum_add(skb->csum, | |
266 | csum_partial(skb->data + (2 * ETH_ALEN), | |
267 | VLAN_HLEN, 0)); | |
268 | ||
269 | vlan_set_tci(skb, 0); | |
270 | } | |
6ce39213 JG |
271 | #endif |
272 | ||
53e6421b JG |
273 | skb_dst_drop(skb); |
274 | nf_reset(skb); | |
275 | secpath_reset(skb); | |
276 | ||
f2459fe7 JG |
277 | skb->pkt_type = PACKET_HOST; |
278 | skb->protocol = eth_type_trans(skb, netdev); | |
3cfede14 | 279 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
f2459fe7 | 280 | |
e23775f2 PS |
281 | #ifdef HAVE_DEV_TSTATS |
282 | stats = this_cpu_ptr((struct pcpu_sw_netstats __percpu *)netdev->tstats); | |
283 | u64_stats_update_begin(&stats->syncp); | |
284 | stats->rx_packets++; | |
285 | stats->rx_bytes += skb->len; | |
286 | u64_stats_update_end(&stats->syncp); | |
287 | #endif | |
a5b7d883 | 288 | |
e23775f2 PS |
289 | netif_rx(skb); |
290 | return NETDEV_TX_OK; | |
f2459fe7 JG |
291 | } |
292 | ||
5a38795f | 293 | static struct vport_ops ovs_internal_vport_ops = { |
df2c07f4 | 294 | .type = OVS_VPORT_TYPE_INTERNAL, |
f2459fe7 JG |
295 | .create = internal_dev_create, |
296 | .destroy = internal_dev_destroy, | |
f2459fe7 JG |
297 | .send = internal_dev_recv, |
298 | }; | |
299 | ||
850b6b3b | 300 | int ovs_is_internal_dev(const struct net_device *netdev) |
f2459fe7 | 301 | { |
f2459fe7 | 302 | return netdev->netdev_ops == &internal_dev_netdev_ops; |
f2459fe7 JG |
303 | } |
304 | ||
850b6b3b | 305 | struct vport *ovs_internal_dev_get_vport(struct net_device *netdev) |
f2459fe7 | 306 | { |
850b6b3b | 307 | if (!ovs_is_internal_dev(netdev)) |
17a07f9f | 308 | return NULL; |
7237e4f4 | 309 | |
c052da84 | 310 | return internal_dev_priv(netdev)->vport; |
f2459fe7 | 311 | } |
5282e284 TG |
312 | |
313 | int ovs_internal_dev_rtnl_link_register(void) | |
314 | { | |
5a38795f TG |
315 | int err; |
316 | ||
317 | err = rtnl_link_register(&internal_dev_link_ops); | |
318 | if (err < 0) | |
319 | return err; | |
320 | ||
321 | err = ovs_vport_ops_register(&ovs_internal_vport_ops); | |
322 | if (err < 0) | |
323 | rtnl_link_unregister(&internal_dev_link_ops); | |
324 | ||
325 | return err; | |
5282e284 TG |
326 | } |
327 | ||
328 | void ovs_internal_dev_rtnl_link_unregister(void) | |
329 | { | |
5a38795f | 330 | ovs_vport_ops_unregister(&ovs_internal_vport_ops); |
5282e284 TG |
331 | rtnl_link_unregister(&internal_dev_link_ops); |
332 | } |