Thomas Gleixner | c942299 | 2019-05-29 07:12:43 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 2 | /* |
Raju Subramanian | caf2ee1 | 2012-05-03 18:55:23 -0700 | [diff] [blame] | 3 | * Copyright (c) 2007-2012 Nicira, Inc. |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 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> |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 12 | |
Jesse Gross | 7fe99e2 | 2012-05-25 11:29:30 -0700 | [diff] [blame] | 13 | #include <net/dst.h> |
| 14 | #include <net/xfrm.h> |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 15 | #include <net/rtnetlink.h> |
Jesse Gross | 7fe99e2 | 2012-05-25 11:29:30 -0700 | [diff] [blame] | 16 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 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 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 25 | static struct vport_ops ovs_internal_vport_ops; |
| 26 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 27 | static struct internal_dev *internal_dev_priv(struct net_device *netdev) |
| 28 | { |
| 29 | return netdev_priv(netdev); |
| 30 | } |
| 31 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 32 | /* Called with rcu_read_lock_bh. */ |
YueHaibing | eddf11e | 2018-09-26 17:15:38 +0800 | [diff] [blame] | 33 | static netdev_tx_t |
| 34 | internal_dev_xmit(struct sk_buff *skb, struct net_device *netdev) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 35 | { |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 36 | int len, err; |
| 37 | |
Lev Stipakov | 865e6ae | 2020-11-13 23:53:36 +0200 | [diff] [blame] | 38 | /* store len value because skb can be freed inside ovs_vport_receive() */ |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 39 | len = skb->len; |
Lev Stipakov | 865e6ae | 2020-11-13 23:53:36 +0200 | [diff] [blame] | 40 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 41 | rcu_read_lock(); |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 42 | err = ovs_vport_receive(internal_dev_priv(netdev)->vport, skb, NULL); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 43 | rcu_read_unlock(); |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 44 | |
Lev Stipakov | 865e6ae | 2020-11-13 23:53:36 +0200 | [diff] [blame] | 45 | if (likely(!err)) |
| 46 | dev_sw_netstats_tx_add(netdev, 1, len); |
| 47 | else |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 48 | netdev->stats.tx_errors++; |
Lev Stipakov | 865e6ae | 2020-11-13 23:53:36 +0200 | [diff] [blame] | 49 | |
YueHaibing | eddf11e | 2018-09-26 17:15:38 +0800 | [diff] [blame] | 50 | return NETDEV_TX_OK; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int internal_dev_open(struct net_device *netdev) |
| 54 | { |
| 55 | netif_start_queue(netdev); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int internal_dev_stop(struct net_device *netdev) |
| 60 | { |
| 61 | netif_stop_queue(netdev); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static void internal_dev_getinfo(struct net_device *netdev, |
| 66 | struct ethtool_drvinfo *info) |
| 67 | { |
Jiri Pirko | 7826d43 | 2013-01-06 00:44:26 +0000 | [diff] [blame] | 68 | strlcpy(info->driver, "openvswitch", sizeof(info->driver)); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static const struct ethtool_ops internal_dev_ethtool_ops = { |
| 72 | .get_drvinfo = internal_dev_getinfo, |
| 73 | .get_link = ethtool_op_get_link, |
| 74 | }; |
| 75 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 76 | static 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 Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static 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 Graf | 9151991 | 2012-11-29 20:55:05 +0100 | [diff] [blame] | 87 | .ndo_set_mac_address = eth_mac_addr, |
Lev Stipakov | 865e6ae | 2020-11-13 23:53:36 +0200 | [diff] [blame] | 88 | .ndo_get_stats64 = dev_get_tstats64, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 91 | static struct rtnl_link_ops internal_dev_link_ops __read_mostly = { |
| 92 | .kind = "openvswitch", |
| 93 | }; |
| 94 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 95 | static void do_setup(struct net_device *netdev) |
| 96 | { |
| 97 | ether_setup(netdev); |
| 98 | |
Jarno Rajahalme | 425df17 | 2017-02-14 21:16:28 -0800 | [diff] [blame] | 99 | netdev->max_mtu = ETH_MAX_MTU; |
| 100 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 101 | netdev->netdev_ops = &internal_dev_netdev_ops; |
| 102 | |
| 103 | netdev->priv_flags &= ~IFF_TX_SKB_SHARING; |
Paolo Abeni | 3a927bc | 2016-02-26 10:45:39 +0100 | [diff] [blame] | 104 | netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_OPENVSWITCH | |
Paolo Abeni | 183dea5 | 2017-11-30 15:35:33 +0100 | [diff] [blame] | 105 | IFF_NO_QUEUE; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 106 | netdev->needs_free_netdev = true; |
Hillf Danton | 9464cc3 | 2019-10-21 12:01:57 +0200 | [diff] [blame] | 107 | netdev->priv_destructor = NULL; |
Wilfried Klaebe | 7ad24ea | 2014-05-11 00:12:32 +0000 | [diff] [blame] | 108 | netdev->ethtool_ops = &internal_dev_ethtool_ops; |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 109 | netdev->rtnl_link_ops = &internal_dev_link_ops; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 110 | |
| 111 | netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST | |
Pravin B Shelar | f6eec61 | 2014-07-17 15:14:15 -0700 | [diff] [blame] | 112 | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | |
| 113 | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 114 | |
| 115 | netdev->vlan_features = netdev->features; |
Pravin B Shelar | f6eec61 | 2014-07-17 15:14:15 -0700 | [diff] [blame] | 116 | netdev->hw_enc_features = netdev->features; |
Jiri Benc | 3145c03 | 2016-10-10 17:02:44 +0200 | [diff] [blame] | 117 | netdev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 118 | netdev->hw_features = netdev->features & ~NETIF_F_LLTX; |
Pravin B Shelar | f6eec61 | 2014-07-17 15:14:15 -0700 | [diff] [blame] | 119 | |
Danny Kukawka | 7ce5d22 | 2012-02-15 06:45:40 +0000 | [diff] [blame] | 120 | eth_hw_addr_random(netdev); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static struct vport *internal_dev_create(const struct vport_parms *parms) |
| 124 | { |
| 125 | struct vport *vport; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 126 | struct internal_dev *internal_dev; |
Taehee Yoo | 309b669 | 2019-06-09 23:26:21 +0900 | [diff] [blame] | 127 | struct net_device *dev; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 128 | int err; |
| 129 | |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 130 | vport = ovs_vport_alloc(0, &ovs_internal_vport_ops, parms); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 131 | if (IS_ERR(vport)) { |
| 132 | err = PTR_ERR(vport); |
| 133 | goto error; |
| 134 | } |
| 135 | |
Taehee Yoo | 309b669 | 2019-06-09 23:26:21 +0900 | [diff] [blame] | 136 | dev = alloc_netdev(sizeof(struct internal_dev), |
| 137 | parms->name, NET_NAME_USER, do_setup); |
| 138 | vport->dev = dev; |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 139 | if (!vport->dev) { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 140 | err = -ENOMEM; |
| 141 | goto error_free_vport; |
| 142 | } |
James Morse | 1241365 | 2015-10-19 16:31:55 +0100 | [diff] [blame] | 143 | 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 Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 148 | |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 149 | dev_net_set(vport->dev, ovs_dp_get_net(vport->dp)); |
| 150 | internal_dev = internal_dev_priv(vport->dev); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 151 | internal_dev->vport = vport; |
| 152 | |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 153 | /* Restrict bridge port to current netns. */ |
| 154 | if (vport->port_no == OVSP_LOCAL) |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 155 | vport->dev->features |= NETIF_F_NETNS_LOCAL; |
Pravin B Shelar | 46df7b8 | 2012-02-22 19:58:59 -0800 | [diff] [blame] | 156 | |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 157 | rtnl_lock(); |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 158 | err = register_netdevice(vport->dev); |
Hillf Danton | 9464cc3 | 2019-10-21 12:01:57 +0200 | [diff] [blame] | 159 | if (err) |
James Morse | 1241365 | 2015-10-19 16:31:55 +0100 | [diff] [blame] | 160 | goto error_unlock; |
Hillf Danton | 9464cc3 | 2019-10-21 12:01:57 +0200 | [diff] [blame] | 161 | vport->dev->priv_destructor = internal_dev_destructor; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 162 | |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 163 | dev_set_promiscuity(vport->dev, 1); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 164 | rtnl_unlock(); |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 165 | netif_start_queue(vport->dev); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 166 | |
| 167 | return vport; |
| 168 | |
James Morse | 1241365 | 2015-10-19 16:31:55 +0100 | [diff] [blame] | 169 | error_unlock: |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 170 | rtnl_unlock(); |
Taehee Yoo | 309b669 | 2019-06-09 23:26:21 +0900 | [diff] [blame] | 171 | free_percpu(dev->tstats); |
James Morse | 1241365 | 2015-10-19 16:31:55 +0100 | [diff] [blame] | 172 | error_free_netdev: |
Taehee Yoo | 309b669 | 2019-06-09 23:26:21 +0900 | [diff] [blame] | 173 | free_netdev(dev); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 174 | error_free_vport: |
Hillf Danton | 9464cc3 | 2019-10-21 12:01:57 +0200 | [diff] [blame] | 175 | ovs_vport_free(vport); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 176 | error: |
| 177 | return ERR_PTR(err); |
| 178 | } |
| 179 | |
| 180 | static void internal_dev_destroy(struct vport *vport) |
| 181 | { |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 182 | netif_stop_queue(vport->dev); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 183 | rtnl_lock(); |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 184 | dev_set_promiscuity(vport->dev, -1); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 185 | |
| 186 | /* unregister_netdevice() waits for an RCU grace period. */ |
Thomas Graf | be4ace6 | 2015-07-21 10:44:04 +0200 | [diff] [blame] | 187 | unregister_netdevice(vport->dev); |
James Morse | 1241365 | 2015-10-19 16:31:55 +0100 | [diff] [blame] | 188 | free_percpu(vport->dev->tstats); |
Pravin B Shelar | 8e4e171 | 2013-04-15 13:23:03 -0700 | [diff] [blame] | 189 | rtnl_unlock(); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Pravin B Shelar | aec1592 | 2015-10-20 23:00:10 -0700 | [diff] [blame] | 192 | static netdev_tx_t internal_dev_recv(struct sk_buff *skb) |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 193 | { |
Pravin B Shelar | aec1592 | 2015-10-20 23:00:10 -0700 | [diff] [blame] | 194 | struct net_device *netdev = skb->dev; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 195 | |
Chunhe Li | e1f9c35 | 2014-09-08 13:17:21 -0700 | [diff] [blame] | 196 | if (unlikely(!(netdev->flags & IFF_UP))) { |
| 197 | kfree_skb(skb); |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 198 | netdev->stats.rx_dropped++; |
Pravin B Shelar | aec1592 | 2015-10-20 23:00:10 -0700 | [diff] [blame] | 199 | return NETDEV_TX_OK; |
Chunhe Li | e1f9c35 | 2014-09-08 13:17:21 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Jesse Gross | 7fe99e2 | 2012-05-25 11:29:30 -0700 | [diff] [blame] | 202 | skb_dst_drop(skb); |
Florian Westphal | 895b5c9 | 2019-09-29 20:54:03 +0200 | [diff] [blame] | 203 | nf_reset_ct(skb); |
Jesse Gross | 7fe99e2 | 2012-05-25 11:29:30 -0700 | [diff] [blame] | 204 | secpath_reset(skb); |
| 205 | |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 206 | skb->pkt_type = PACKET_HOST; |
| 207 | skb->protocol = eth_type_trans(skb, netdev); |
Pravin B Shelar | b34df5e | 2013-06-13 11:11:44 -0700 | [diff] [blame] | 208 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); |
Fabian Frederick | e40b372 | 2020-10-05 22:37:03 +0200 | [diff] [blame] | 209 | dev_sw_netstats_rx_add(netdev, skb->len); |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 210 | |
Pravin B Shelar | 8c87663 | 2015-08-29 17:44:07 -0700 | [diff] [blame] | 211 | netif_rx(skb); |
Pravin B Shelar | aec1592 | 2015-10-20 23:00:10 -0700 | [diff] [blame] | 212 | return NETDEV_TX_OK; |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 215 | static struct vport_ops ovs_internal_vport_ops = { |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 216 | .type = OVS_VPORT_TYPE_INTERNAL, |
| 217 | .create = internal_dev_create, |
| 218 | .destroy = internal_dev_destroy, |
Jesse Gross | ccb1352 | 2011-10-25 19:26:31 -0700 | [diff] [blame] | 219 | .send = internal_dev_recv, |
| 220 | }; |
| 221 | |
| 222 | int ovs_is_internal_dev(const struct net_device *netdev) |
| 223 | { |
| 224 | return netdev->netdev_ops == &internal_dev_netdev_ops; |
| 225 | } |
| 226 | |
| 227 | struct 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 Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 234 | |
| 235 | int ovs_internal_dev_rtnl_link_register(void) |
| 236 | { |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 237 | 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 Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void ovs_internal_dev_rtnl_link_unregister(void) |
| 251 | { |
Thomas Graf | 62b9c8d | 2014-10-22 17:29:06 +0200 | [diff] [blame] | 252 | ovs_vport_ops_unregister(&ovs_internal_vport_ops); |
Jiri Pirko | 5b9e7e1 | 2014-06-26 09:58:26 +0200 | [diff] [blame] | 253 | rtnl_link_unregister(&internal_dev_link_ops); |
| 254 | } |