blob: 5b2ee9c1c00b11675a770c8e0829d0cbb4c012eb [file] [log] [blame]
Thomas Gleixnerc9422992019-05-29 07:12:43 -07001// SPDX-License-Identifier: GPL-2.0-only
Jesse Grossccb13522011-10-25 19:26:31 -07002/*
Raju Subramaniancaf2ee12012-05-03 18:55:23 -07003 * Copyright (c) 2007-2012 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07004 */
5
Jesse Grossccb13522011-10-25 19:26:31 -07006#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>
Jesse Grossccb13522011-10-25 19:26:31 -070012
Jesse Gross7fe99e22012-05-25 11:29:30 -070013#include <net/dst.h>
14#include <net/xfrm.h>
Jiri Pirko5b9e7e12014-06-26 09:58:26 +020015#include <net/rtnetlink.h>
Jesse Gross7fe99e22012-05-25 11:29:30 -070016
Jesse Grossccb13522011-10-25 19:26:31 -070017#include "datapath.h"
18#include "vport-internal_dev.h"
19#include "vport-netdev.h"
20
21struct internal_dev {
22 struct vport *vport;
23};
24
Thomas Graf62b9c8d2014-10-22 17:29:06 +020025static struct vport_ops ovs_internal_vport_ops;
26
Jesse Grossccb13522011-10-25 19:26:31 -070027static struct internal_dev *internal_dev_priv(struct net_device *netdev)
28{
29 return netdev_priv(netdev);
30}
31
Jesse Grossccb13522011-10-25 19:26:31 -070032/* Called with rcu_read_lock_bh. */
YueHaibingeddf11e2018-09-26 17:15:38 +080033static netdev_tx_t
34internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev)
Jesse Grossccb13522011-10-25 19:26:31 -070035{
Pravin B Shelar8c876632015-08-29 17:44:07 -070036 int len, err;
37
Lev Stipakov865e6ae2020-11-13 23:53:36 +020038 /* store len value because skb can be freed inside ovs_vport_receive() */
Pravin B Shelar8c876632015-08-29 17:44:07 -070039 len = skb->len;
Lev Stipakov865e6ae2020-11-13 23:53:36 +020040
Jesse Grossccb13522011-10-25 19:26:31 -070041 rcu_read_lock();
Pravin B Shelar8c876632015-08-29 17:44:07 -070042 err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL);
Jesse Grossccb13522011-10-25 19:26:31 -070043 rcu_read_unlock();
Pravin B Shelar8c876632015-08-29 17:44:07 -070044
Lev Stipakov865e6ae2020-11-13 23:53:36 +020045 if (likely(!err))
46 dev_sw_netstats_tx_add(netdev, 1, len);
47 else
Pravin B Shelar8c876632015-08-29 17:44:07 -070048 netdev->stats.tx_errors++;
Lev Stipakov865e6ae2020-11-13 23:53:36 +020049
YueHaibingeddf11e2018-09-26 17:15:38 +080050 return NETDEV_TX_OK;
Jesse Grossccb13522011-10-25 19:26:31 -070051}
52
53static int internal_dev_open(struct net_device *netdev)
54{
55 netif_start_queue(netdev);
56 return 0;
57}
58
59static int internal_dev_stop(struct net_device *netdev)
60{
61 netif_stop_queue(netdev);
62 return 0;
63}
64
65static void internal_dev_getinfo(struct net_device *netdev,
66 struct ethtool_drvinfo *info)
67{
Jiri Pirko7826d432013-01-06 00:44:26 +000068 strlcpy(info->driver, "openvswitch", sizeof(info->driver));
Jesse Grossccb13522011-10-25 19:26:31 -070069}
70
71static const struct ethtool_ops internal_dev_ethtool_ops = {
72 .get_drvinfo = internal_dev_getinfo,
73 .get_link = ethtool_op_get_link,
74};
75
Jesse Grossccb13522011-10-25 19:26:31 -070076static void internal_dev_destructor(struct net_device *dev)
77{
78 struct vport *vport = ovs_internal_dev_get_vport(dev);
79
80 ovs_vport_free(vport);
Jesse Grossccb13522011-10-25 19:26:31 -070081}
82
83static const struct net_device_ops internal_dev_netdev_ops = {
84 .ndo_open = internal_dev_open,
85 .ndo_stop = internal_dev_stop,
86 .ndo_start_xmit = internal_dev_xmit,
Thomas Graf91519912012-11-29 20:55:05 +010087 .ndo_set_mac_address = eth_mac_addr,
Lev Stipakov865e6ae2020-11-13 23:53:36 +020088 .ndo_get_stats64 = dev_get_tstats64,
Jesse Grossccb13522011-10-25 19:26:31 -070089};
90
Jiri Pirko5b9e7e12014-06-26 09:58:26 +020091static struct rtnl_link_ops internal_dev_link_ops __read_mostly = {
92 .kind = "openvswitch",
93};
94
Jesse Grossccb13522011-10-25 19:26:31 -070095static void do_setup(struct net_device *netdev)
96{
97 ether_setup(netdev);
98
Jarno Rajahalme425df172017-02-14 21:16:28 -080099 netdev->max_mtu = ETH_MAX_MTU;
100
Jesse Grossccb13522011-10-25 19:26:31 -0700101 netdev->netdev_ops = &internal_dev_netdev_ops;
102
103 netdev->priv_flags &= ~IFF_TX_SKB_SHARING;
Paolo Abeni3a927bc2016-02-26 10:45:39 +0100104 netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH |
Paolo Abeni183dea52017-11-30 15:35:33 +0100105 IFF_NO_QUEUE;
David S. Millercf124db2017-05-08 12:52:56 -0400106 netdev->needs_free_netdev = true;
Hillf Danton9464cc32019-10-21 12:01:57 +0200107 netdev->priv_destructor = NULL;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000108 netdev->ethtool_ops = &internal_dev_ethtool_ops;
Jiri Pirko5b9e7e12014-06-26 09:58:26 +0200109 netdev->rtnl_link_ops = &internal_dev_link_ops;
Jesse Grossccb13522011-10-25 19:26:31 -0700110
111 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
Pravin B Shelarf6eec612014-07-17 15:14:15 -0700112 NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
113 NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL;
Jesse Grossccb13522011-10-25 19:26:31 -0700114
115 netdev->vlan_features = netdev->features;
Pravin B Shelarf6eec612014-07-17 15:14:15 -0700116 netdev->hw_enc_features = netdev->features;
Jiri Benc3145c032016-10-10 17:02:44 +0200117 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
Jesse Grossccb13522011-10-25 19:26:31 -0700118 netdev->hw_features = netdev->features & ~NETIF_F_LLTX;
Pravin B Shelarf6eec612014-07-17 15:14:15 -0700119
Danny Kukawka7ce5d222012-02-15 06:45:40 +0000120 eth_hw_addr_random(netdev);
Jesse Grossccb13522011-10-25 19:26:31 -0700121}
122
123static struct vport *internal_dev_create(const struct vport_parms *parms)
124{
125 struct vport *vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700126 struct internal_dev *internal_dev;
Taehee Yoo309b6692019-06-09 23:26:21 +0900127 struct net_device *dev;
Jesse Grossccb13522011-10-25 19:26:31 -0700128 int err;
129
Thomas Grafbe4ace62015-07-21 10:44:04 +0200130 vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms);
Jesse Grossccb13522011-10-25 19:26:31 -0700131 if (IS_ERR(vport)) {
132 err = PTR_ERR(vport);
133 goto error;
134 }
135
Taehee Yoo309b6692019-06-09 23:26:21 +0900136 dev = alloc_netdev(sizeof(struct internal_dev),
137 parms->name, NET_NAME_USER, do_setup);
138 vport->dev = dev;
Thomas Grafbe4ace62015-07-21 10:44:04 +0200139 if (!vport->dev) {
Jesse Grossccb13522011-10-25 19:26:31 -0700140 err = -ENOMEM;
141 goto error_free_vport;
142 }
James Morse12413652015-10-19 16:31:55 +0100143 vport->dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
144 if (!vport->dev->tstats) {
145 err = -ENOMEM;
146 goto error_free_netdev;
147 }
Jesse Grossccb13522011-10-25 19:26:31 -0700148
Thomas Grafbe4ace62015-07-21 10:44:04 +0200149 dev_net_set(vport->dev, ovs_dp_get_net(vport->dp));
150 internal_dev = internal_dev_priv(vport->dev);
Jesse Grossccb13522011-10-25 19:26:31 -0700151 internal_dev->vport = vport;
152
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800153 /* Restrict bridge port to current netns. */
154 if (vport->port_no == OVSP_LOCAL)
Thomas Grafbe4ace62015-07-21 10:44:04 +0200155 vport->dev->features |= NETIF_F_NETNS_LOCAL;
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800156
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700157 rtnl_lock();
Thomas Grafbe4ace62015-07-21 10:44:04 +0200158 err = register_netdevice(vport->dev);
Hillf Danton9464cc32019-10-21 12:01:57 +0200159 if (err)
James Morse12413652015-10-19 16:31:55 +0100160 goto error_unlock;
Hillf Danton9464cc32019-10-21 12:01:57 +0200161 vport->dev->priv_destructor = internal_dev_destructor;
Jesse Grossccb13522011-10-25 19:26:31 -0700162
Thomas Grafbe4ace62015-07-21 10:44:04 +0200163 dev_set_promiscuity(vport->dev, 1);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700164 rtnl_unlock();
Thomas Grafbe4ace62015-07-21 10:44:04 +0200165 netif_start_queue(vport->dev);
Jesse Grossccb13522011-10-25 19:26:31 -0700166
167 return vport;
168
James Morse12413652015-10-19 16:31:55 +0100169error_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700170 rtnl_unlock();
Taehee Yoo309b6692019-06-09 23:26:21 +0900171 free_percpu(dev->tstats);
James Morse12413652015-10-19 16:31:55 +0100172error_free_netdev:
Taehee Yoo309b6692019-06-09 23:26:21 +0900173 free_netdev(dev);
Jesse Grossccb13522011-10-25 19:26:31 -0700174error_free_vport:
Hillf Danton9464cc32019-10-21 12:01:57 +0200175 ovs_vport_free(vport);
Jesse Grossccb13522011-10-25 19:26:31 -0700176error:
177 return ERR_PTR(err);
178}
179
180static void internal_dev_destroy(struct vport *vport)
181{
Thomas Grafbe4ace62015-07-21 10:44:04 +0200182 netif_stop_queue(vport->dev);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700183 rtnl_lock();
Thomas Grafbe4ace62015-07-21 10:44:04 +0200184 dev_set_promiscuity(vport->dev, -1);
Jesse Grossccb13522011-10-25 19:26:31 -0700185
186 /* unregister_netdevice() waits for an RCU grace period. */
Thomas Grafbe4ace62015-07-21 10:44:04 +0200187 unregister_netdevice(vport->dev);
James Morse12413652015-10-19 16:31:55 +0100188 free_percpu(vport->dev->tstats);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700189 rtnl_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700190}
191
Pravin B Shelaraec15922015-10-20 23:00:10 -0700192static netdev_tx_t internal_dev_recv(struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700193{
Pravin B Shelaraec15922015-10-20 23:00:10 -0700194 struct net_device *netdev = skb->dev;
Jesse Grossccb13522011-10-25 19:26:31 -0700195
Chunhe Lie1f9c352014-09-08 13:17:21 -0700196 if (unlikely(!(netdev->flags & IFF_UP))) {
197 kfree_skb(skb);
Pravin B Shelar8c876632015-08-29 17:44:07 -0700198 netdev->stats.rx_dropped++;
Pravin B Shelaraec15922015-10-20 23:00:10 -0700199 return NETDEV_TX_OK;
Chunhe Lie1f9c352014-09-08 13:17:21 -0700200 }
201
Jesse Gross7fe99e22012-05-25 11:29:30 -0700202 skb_dst_drop(skb);
Florian Westphal895b5c92019-09-29 20:54:03 +0200203 nf_reset_ct(skb);
Jesse Gross7fe99e22012-05-25 11:29:30 -0700204 secpath_reset(skb);
205
Jesse Grossccb13522011-10-25 19:26:31 -0700206 skb->pkt_type = PACKET_HOST;
207 skb->protocol = eth_type_trans(skb, netdev);
Pravin B Shelarb34df5e2013-06-13 11:11:44 -0700208 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
Fabian Fredericke40b3722020-10-05 22:37:03 +0200209 dev_sw_netstats_rx_add(netdev, skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700210
Pravin B Shelar8c876632015-08-29 17:44:07 -0700211 netif_rx(skb);
Pravin B Shelaraec15922015-10-20 23:00:10 -0700212 return NETDEV_TX_OK;
Jesse Grossccb13522011-10-25 19:26:31 -0700213}
214
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200215static struct vport_ops ovs_internal_vport_ops = {
Jesse Grossccb13522011-10-25 19:26:31 -0700216 .type = OVS_VPORT_TYPE_INTERNAL,
217 .create = internal_dev_create,
218 .destroy = internal_dev_destroy,
Jesse Grossccb13522011-10-25 19:26:31 -0700219 .send = internal_dev_recv,
220};
221
222int ovs_is_internal_dev(const struct net_device *netdev)
223{
224 return netdev->netdev_ops == &internal_dev_netdev_ops;
225}
226
227struct vport *ovs_internal_dev_get_vport(struct net_device *netdev)
228{
229 if (!ovs_is_internal_dev(netdev))
230 return NULL;
231
232 return internal_dev_priv(netdev)->vport;
233}
Jiri Pirko5b9e7e12014-06-26 09:58:26 +0200234
235int ovs_internal_dev_rtnl_link_register(void)
236{
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200237 int err;
238
239 err = rtnl_link_register(&internal_dev_link_ops);
240 if (err < 0)
241 return err;
242
243 err = ovs_vport_ops_register(&ovs_internal_vport_ops);
244 if (err < 0)
245 rtnl_link_unregister(&internal_dev_link_ops);
246
247 return err;
Jiri Pirko5b9e7e12014-06-26 09:58:26 +0200248}
249
250void ovs_internal_dev_rtnl_link_unregister(void)
251{
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200252 ovs_vport_ops_unregister(&ovs_internal_vport_ops);
Jiri Pirko5b9e7e12014-06-26 09:58:26 +0200253 rtnl_link_unregister(&internal_dev_link_ops);
254}