root/net/openvswitch/datapath.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ovs_must_notify
  2. ovs_notify
  3. ovs_lock
  4. ovs_unlock
  5. lockdep_ovsl_is_held
  6. ovs_dp_name
  7. get_dpifindex
  8. destroy_dp_rcu
  9. vport_hash_bucket
  10. ovs_lookup_vport
  11. new_vport
  12. ovs_dp_detach_port
  13. ovs_dp_process_packet
  14. ovs_dp_upcall
  15. queue_gso_packets
  16. upcall_msg_size
  17. pad_packet
  18. queue_userspace_packet
  19. ovs_packet_cmd_execute
  20. get_dp_stats
  21. should_fill_key
  22. should_fill_mask
  23. should_fill_actions
  24. ovs_flow_cmd_msg_size
  25. ovs_flow_cmd_fill_stats
  26. ovs_flow_cmd_fill_actions
  27. ovs_flow_cmd_fill_info
  28. ovs_flow_cmd_alloc_info
  29. ovs_flow_cmd_build_info
  30. ovs_flow_cmd_new
  31. get_flow_actions
  32. ovs_nla_init_match_and_action
  33. ovs_flow_cmd_set
  34. ovs_flow_cmd_get
  35. ovs_flow_cmd_del
  36. ovs_flow_cmd_dump
  37. ovs_dp_cmd_msg_size
  38. ovs_dp_cmd_fill_info
  39. ovs_dp_cmd_alloc_info
  40. lookup_datapath
  41. ovs_dp_reset_user_features
  42. ovs_dp_change
  43. ovs_dp_cmd_new
  44. __dp_destroy
  45. ovs_dp_cmd_del
  46. ovs_dp_cmd_set
  47. ovs_dp_cmd_get
  48. ovs_dp_cmd_dump
  49. ovs_vport_cmd_fill_info
  50. ovs_vport_cmd_alloc_info
  51. ovs_vport_cmd_build_info
  52. lookup_vport
  53. ovs_get_max_headroom
  54. ovs_update_headroom
  55. ovs_vport_cmd_new
  56. ovs_vport_cmd_set
  57. ovs_vport_cmd_del
  58. ovs_vport_cmd_get
  59. ovs_vport_cmd_dump
  60. dp_unregister_genl
  61. dp_register_genl
  62. ovs_init_net
  63. list_vports_from_net
  64. ovs_exit_net
  65. dp_init
  66. dp_cleanup

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright (c) 2007-2014 Nicira, Inc.
   4  */
   5 
   6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   7 
   8 #include <linux/init.h>
   9 #include <linux/module.h>
  10 #include <linux/if_arp.h>
  11 #include <linux/if_vlan.h>
  12 #include <linux/in.h>
  13 #include <linux/ip.h>
  14 #include <linux/jhash.h>
  15 #include <linux/delay.h>
  16 #include <linux/time.h>
  17 #include <linux/etherdevice.h>
  18 #include <linux/genetlink.h>
  19 #include <linux/kernel.h>
  20 #include <linux/kthread.h>
  21 #include <linux/mutex.h>
  22 #include <linux/percpu.h>
  23 #include <linux/rcupdate.h>
  24 #include <linux/tcp.h>
  25 #include <linux/udp.h>
  26 #include <linux/ethtool.h>
  27 #include <linux/wait.h>
  28 #include <asm/div64.h>
  29 #include <linux/highmem.h>
  30 #include <linux/netfilter_bridge.h>
  31 #include <linux/netfilter_ipv4.h>
  32 #include <linux/inetdevice.h>
  33 #include <linux/list.h>
  34 #include <linux/openvswitch.h>
  35 #include <linux/rculist.h>
  36 #include <linux/dmi.h>
  37 #include <net/genetlink.h>
  38 #include <net/net_namespace.h>
  39 #include <net/netns/generic.h>
  40 
  41 #include "datapath.h"
  42 #include "flow.h"
  43 #include "flow_table.h"
  44 #include "flow_netlink.h"
  45 #include "meter.h"
  46 #include "vport-internal_dev.h"
  47 #include "vport-netdev.h"
  48 
  49 unsigned int ovs_net_id __read_mostly;
  50 
  51 static struct genl_family dp_packet_genl_family;
  52 static struct genl_family dp_flow_genl_family;
  53 static struct genl_family dp_datapath_genl_family;
  54 
  55 static const struct nla_policy flow_policy[];
  56 
  57 static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
  58         .name = OVS_FLOW_MCGROUP,
  59 };
  60 
  61 static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  62         .name = OVS_DATAPATH_MCGROUP,
  63 };
  64 
  65 static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
  66         .name = OVS_VPORT_MCGROUP,
  67 };
  68 
  69 /* Check if need to build a reply message.
  70  * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
  71 static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
  72                             unsigned int group)
  73 {
  74         return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
  75                genl_has_listeners(family, genl_info_net(info), group);
  76 }
  77 
  78 static void ovs_notify(struct genl_family *family,
  79                        struct sk_buff *skb, struct genl_info *info)
  80 {
  81         genl_notify(family, skb, info, 0, GFP_KERNEL);
  82 }
  83 
  84 /**
  85  * DOC: Locking:
  86  *
  87  * All writes e.g. Writes to device state (add/remove datapath, port, set
  88  * operations on vports, etc.), Writes to other state (flow table
  89  * modifications, set miscellaneous datapath parameters, etc.) are protected
  90  * by ovs_lock.
  91  *
  92  * Reads are protected by RCU.
  93  *
  94  * There are a few special cases (mostly stats) that have their own
  95  * synchronization but they nest under all of above and don't interact with
  96  * each other.
  97  *
  98  * The RTNL lock nests inside ovs_mutex.
  99  */
 100 
 101 static DEFINE_MUTEX(ovs_mutex);
 102 
 103 void ovs_lock(void)
 104 {
 105         mutex_lock(&ovs_mutex);
 106 }
 107 
 108 void ovs_unlock(void)
 109 {
 110         mutex_unlock(&ovs_mutex);
 111 }
 112 
 113 #ifdef CONFIG_LOCKDEP
 114 int lockdep_ovsl_is_held(void)
 115 {
 116         if (debug_locks)
 117                 return lockdep_is_held(&ovs_mutex);
 118         else
 119                 return 1;
 120 }
 121 #endif
 122 
 123 static struct vport *new_vport(const struct vport_parms *);
 124 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
 125                              const struct sw_flow_key *,
 126                              const struct dp_upcall_info *,
 127                              uint32_t cutlen);
 128 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
 129                                   const struct sw_flow_key *,
 130                                   const struct dp_upcall_info *,
 131                                   uint32_t cutlen);
 132 
 133 /* Must be called with rcu_read_lock or ovs_mutex. */
 134 const char *ovs_dp_name(const struct datapath *dp)
 135 {
 136         struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
 137         return ovs_vport_name(vport);
 138 }
 139 
 140 static int get_dpifindex(const struct datapath *dp)
 141 {
 142         struct vport *local;
 143         int ifindex;
 144 
 145         rcu_read_lock();
 146 
 147         local = ovs_vport_rcu(dp, OVSP_LOCAL);
 148         if (local)
 149                 ifindex = local->dev->ifindex;
 150         else
 151                 ifindex = 0;
 152 
 153         rcu_read_unlock();
 154 
 155         return ifindex;
 156 }
 157 
 158 static void destroy_dp_rcu(struct rcu_head *rcu)
 159 {
 160         struct datapath *dp = container_of(rcu, struct datapath, rcu);
 161 
 162         ovs_flow_tbl_destroy(&dp->table);
 163         free_percpu(dp->stats_percpu);
 164         kfree(dp->ports);
 165         ovs_meters_exit(dp);
 166         kfree(dp);
 167 }
 168 
 169 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
 170                                             u16 port_no)
 171 {
 172         return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
 173 }
 174 
 175 /* Called with ovs_mutex or RCU read lock. */
 176 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
 177 {
 178         struct vport *vport;
 179         struct hlist_head *head;
 180 
 181         head = vport_hash_bucket(dp, port_no);
 182         hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
 183                 if (vport->port_no == port_no)
 184                         return vport;
 185         }
 186         return NULL;
 187 }
 188 
 189 /* Called with ovs_mutex. */
 190 static struct vport *new_vport(const struct vport_parms *parms)
 191 {
 192         struct vport *vport;
 193 
 194         vport = ovs_vport_add(parms);
 195         if (!IS_ERR(vport)) {
 196                 struct datapath *dp = parms->dp;
 197                 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
 198 
 199                 hlist_add_head_rcu(&vport->dp_hash_node, head);
 200         }
 201         return vport;
 202 }
 203 
 204 void ovs_dp_detach_port(struct vport *p)
 205 {
 206         ASSERT_OVSL();
 207 
 208         /* First drop references to device. */
 209         hlist_del_rcu(&p->dp_hash_node);
 210 
 211         /* Then destroy it. */
 212         ovs_vport_del(p);
 213 }
 214 
 215 /* Must be called with rcu_read_lock. */
 216 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
 217 {
 218         const struct vport *p = OVS_CB(skb)->input_vport;
 219         struct datapath *dp = p->dp;
 220         struct sw_flow *flow;
 221         struct sw_flow_actions *sf_acts;
 222         struct dp_stats_percpu *stats;
 223         u64 *stats_counter;
 224         u32 n_mask_hit;
 225         int error;
 226 
 227         stats = this_cpu_ptr(dp->stats_percpu);
 228 
 229         /* Look up flow. */
 230         flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
 231         if (unlikely(!flow)) {
 232                 struct dp_upcall_info upcall;
 233 
 234                 memset(&upcall, 0, sizeof(upcall));
 235                 upcall.cmd = OVS_PACKET_CMD_MISS;
 236                 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
 237                 upcall.mru = OVS_CB(skb)->mru;
 238                 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
 239                 if (unlikely(error))
 240                         kfree_skb(skb);
 241                 else
 242                         consume_skb(skb);
 243                 stats_counter = &stats->n_missed;
 244                 goto out;
 245         }
 246 
 247         ovs_flow_stats_update(flow, key->tp.flags, skb);
 248         sf_acts = rcu_dereference(flow->sf_acts);
 249         error = ovs_execute_actions(dp, skb, sf_acts, key);
 250         if (unlikely(error))
 251                 net_dbg_ratelimited("ovs: action execution error on datapath %s: %d\n",
 252                                                         ovs_dp_name(dp), error);
 253 
 254         stats_counter = &stats->n_hit;
 255 
 256 out:
 257         /* Update datapath statistics. */
 258         u64_stats_update_begin(&stats->syncp);
 259         (*stats_counter)++;
 260         stats->n_mask_hit += n_mask_hit;
 261         u64_stats_update_end(&stats->syncp);
 262 }
 263 
 264 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
 265                   const struct sw_flow_key *key,
 266                   const struct dp_upcall_info *upcall_info,
 267                   uint32_t cutlen)
 268 {
 269         struct dp_stats_percpu *stats;
 270         int err;
 271 
 272         if (upcall_info->portid == 0) {
 273                 err = -ENOTCONN;
 274                 goto err;
 275         }
 276 
 277         if (!skb_is_gso(skb))
 278                 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
 279         else
 280                 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
 281         if (err)
 282                 goto err;
 283 
 284         return 0;
 285 
 286 err:
 287         stats = this_cpu_ptr(dp->stats_percpu);
 288 
 289         u64_stats_update_begin(&stats->syncp);
 290         stats->n_lost++;
 291         u64_stats_update_end(&stats->syncp);
 292 
 293         return err;
 294 }
 295 
 296 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
 297                              const struct sw_flow_key *key,
 298                              const struct dp_upcall_info *upcall_info,
 299                                  uint32_t cutlen)
 300 {
 301         unsigned int gso_type = skb_shinfo(skb)->gso_type;
 302         struct sw_flow_key later_key;
 303         struct sk_buff *segs, *nskb;
 304         int err;
 305 
 306         BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
 307         segs = __skb_gso_segment(skb, NETIF_F_SG, false);
 308         if (IS_ERR(segs))
 309                 return PTR_ERR(segs);
 310         if (segs == NULL)
 311                 return -EINVAL;
 312 
 313         if (gso_type & SKB_GSO_UDP) {
 314                 /* The initial flow key extracted by ovs_flow_key_extract()
 315                  * in this case is for a first fragment, so we need to
 316                  * properly mark later fragments.
 317                  */
 318                 later_key = *key;
 319                 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
 320         }
 321 
 322         /* Queue all of the segments. */
 323         skb = segs;
 324         do {
 325                 if (gso_type & SKB_GSO_UDP && skb != segs)
 326                         key = &later_key;
 327 
 328                 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
 329                 if (err)
 330                         break;
 331 
 332         } while ((skb = skb->next));
 333 
 334         /* Free all of the segments. */
 335         skb = segs;
 336         do {
 337                 nskb = skb->next;
 338                 if (err)
 339                         kfree_skb(skb);
 340                 else
 341                         consume_skb(skb);
 342         } while ((skb = nskb));
 343         return err;
 344 }
 345 
 346 static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
 347                               unsigned int hdrlen, int actions_attrlen)
 348 {
 349         size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
 350                 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
 351                 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
 352                 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
 353 
 354         /* OVS_PACKET_ATTR_USERDATA */
 355         if (upcall_info->userdata)
 356                 size += NLA_ALIGN(upcall_info->userdata->nla_len);
 357 
 358         /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
 359         if (upcall_info->egress_tun_info)
 360                 size += nla_total_size(ovs_tun_key_attr_size());
 361 
 362         /* OVS_PACKET_ATTR_ACTIONS */
 363         if (upcall_info->actions_len)
 364                 size += nla_total_size(actions_attrlen);
 365 
 366         /* OVS_PACKET_ATTR_MRU */
 367         if (upcall_info->mru)
 368                 size += nla_total_size(sizeof(upcall_info->mru));
 369 
 370         return size;
 371 }
 372 
 373 static void pad_packet(struct datapath *dp, struct sk_buff *skb)
 374 {
 375         if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
 376                 size_t plen = NLA_ALIGN(skb->len) - skb->len;
 377 
 378                 if (plen > 0)
 379                         skb_put_zero(skb, plen);
 380         }
 381 }
 382 
 383 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
 384                                   const struct sw_flow_key *key,
 385                                   const struct dp_upcall_info *upcall_info,
 386                                   uint32_t cutlen)
 387 {
 388         struct ovs_header *upcall;
 389         struct sk_buff *nskb = NULL;
 390         struct sk_buff *user_skb = NULL; /* to be queued to userspace */
 391         struct nlattr *nla;
 392         size_t len;
 393         unsigned int hlen;
 394         int err, dp_ifindex;
 395 
 396         dp_ifindex = get_dpifindex(dp);
 397         if (!dp_ifindex)
 398                 return -ENODEV;
 399 
 400         if (skb_vlan_tag_present(skb)) {
 401                 nskb = skb_clone(skb, GFP_ATOMIC);
 402                 if (!nskb)
 403                         return -ENOMEM;
 404 
 405                 nskb = __vlan_hwaccel_push_inside(nskb);
 406                 if (!nskb)
 407                         return -ENOMEM;
 408 
 409                 skb = nskb;
 410         }
 411 
 412         if (nla_attr_size(skb->len) > USHRT_MAX) {
 413                 err = -EFBIG;
 414                 goto out;
 415         }
 416 
 417         /* Complete checksum if needed */
 418         if (skb->ip_summed == CHECKSUM_PARTIAL &&
 419             (err = skb_csum_hwoffload_help(skb, 0)))
 420                 goto out;
 421 
 422         /* Older versions of OVS user space enforce alignment of the last
 423          * Netlink attribute to NLA_ALIGNTO which would require extensive
 424          * padding logic. Only perform zerocopy if padding is not required.
 425          */
 426         if (dp->user_features & OVS_DP_F_UNALIGNED)
 427                 hlen = skb_zerocopy_headlen(skb);
 428         else
 429                 hlen = skb->len;
 430 
 431         len = upcall_msg_size(upcall_info, hlen - cutlen,
 432                               OVS_CB(skb)->acts_origlen);
 433         user_skb = genlmsg_new(len, GFP_ATOMIC);
 434         if (!user_skb) {
 435                 err = -ENOMEM;
 436                 goto out;
 437         }
 438 
 439         upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
 440                              0, upcall_info->cmd);
 441         if (!upcall) {
 442                 err = -EINVAL;
 443                 goto out;
 444         }
 445         upcall->dp_ifindex = dp_ifindex;
 446 
 447         err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
 448         if (err)
 449                 goto out;
 450 
 451         if (upcall_info->userdata)
 452                 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
 453                           nla_len(upcall_info->userdata),
 454                           nla_data(upcall_info->userdata));
 455 
 456         if (upcall_info->egress_tun_info) {
 457                 nla = nla_nest_start_noflag(user_skb,
 458                                             OVS_PACKET_ATTR_EGRESS_TUN_KEY);
 459                 if (!nla) {
 460                         err = -EMSGSIZE;
 461                         goto out;
 462                 }
 463                 err = ovs_nla_put_tunnel_info(user_skb,
 464                                               upcall_info->egress_tun_info);
 465                 if (err)
 466                         goto out;
 467 
 468                 nla_nest_end(user_skb, nla);
 469         }
 470 
 471         if (upcall_info->actions_len) {
 472                 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
 473                 if (!nla) {
 474                         err = -EMSGSIZE;
 475                         goto out;
 476                 }
 477                 err = ovs_nla_put_actions(upcall_info->actions,
 478                                           upcall_info->actions_len,
 479                                           user_skb);
 480                 if (!err)
 481                         nla_nest_end(user_skb, nla);
 482                 else
 483                         nla_nest_cancel(user_skb, nla);
 484         }
 485 
 486         /* Add OVS_PACKET_ATTR_MRU */
 487         if (upcall_info->mru) {
 488                 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
 489                                 upcall_info->mru)) {
 490                         err = -ENOBUFS;
 491                         goto out;
 492                 }
 493                 pad_packet(dp, user_skb);
 494         }
 495 
 496         /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
 497         if (cutlen > 0) {
 498                 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
 499                                 skb->len)) {
 500                         err = -ENOBUFS;
 501                         goto out;
 502                 }
 503                 pad_packet(dp, user_skb);
 504         }
 505 
 506         /* Only reserve room for attribute header, packet data is added
 507          * in skb_zerocopy() */
 508         if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
 509                 err = -ENOBUFS;
 510                 goto out;
 511         }
 512         nla->nla_len = nla_attr_size(skb->len - cutlen);
 513 
 514         err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
 515         if (err)
 516                 goto out;
 517 
 518         /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
 519         pad_packet(dp, user_skb);
 520 
 521         ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
 522 
 523         err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
 524         user_skb = NULL;
 525 out:
 526         if (err)
 527                 skb_tx_error(skb);
 528         kfree_skb(user_skb);
 529         kfree_skb(nskb);
 530         return err;
 531 }
 532 
 533 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
 534 {
 535         struct ovs_header *ovs_header = info->userhdr;
 536         struct net *net = sock_net(skb->sk);
 537         struct nlattr **a = info->attrs;
 538         struct sw_flow_actions *acts;
 539         struct sk_buff *packet;
 540         struct sw_flow *flow;
 541         struct sw_flow_actions *sf_acts;
 542         struct datapath *dp;
 543         struct vport *input_vport;
 544         u16 mru = 0;
 545         int len;
 546         int err;
 547         bool log = !a[OVS_PACKET_ATTR_PROBE];
 548 
 549         err = -EINVAL;
 550         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
 551             !a[OVS_PACKET_ATTR_ACTIONS])
 552                 goto err;
 553 
 554         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
 555         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
 556         err = -ENOMEM;
 557         if (!packet)
 558                 goto err;
 559         skb_reserve(packet, NET_IP_ALIGN);
 560 
 561         nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
 562 
 563         /* Set packet's mru */
 564         if (a[OVS_PACKET_ATTR_MRU]) {
 565                 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
 566                 packet->ignore_df = 1;
 567         }
 568         OVS_CB(packet)->mru = mru;
 569 
 570         /* Build an sw_flow for sending this packet. */
 571         flow = ovs_flow_alloc();
 572         err = PTR_ERR(flow);
 573         if (IS_ERR(flow))
 574                 goto err_kfree_skb;
 575 
 576         err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
 577                                              packet, &flow->key, log);
 578         if (err)
 579                 goto err_flow_free;
 580 
 581         err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
 582                                    &flow->key, &acts, log);
 583         if (err)
 584                 goto err_flow_free;
 585 
 586         rcu_assign_pointer(flow->sf_acts, acts);
 587         packet->priority = flow->key.phy.priority;
 588         packet->mark = flow->key.phy.skb_mark;
 589 
 590         rcu_read_lock();
 591         dp = get_dp_rcu(net, ovs_header->dp_ifindex);
 592         err = -ENODEV;
 593         if (!dp)
 594                 goto err_unlock;
 595 
 596         input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
 597         if (!input_vport)
 598                 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
 599 
 600         if (!input_vport)
 601                 goto err_unlock;
 602 
 603         packet->dev = input_vport->dev;
 604         OVS_CB(packet)->input_vport = input_vport;
 605         sf_acts = rcu_dereference(flow->sf_acts);
 606 
 607         local_bh_disable();
 608         err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
 609         local_bh_enable();
 610         rcu_read_unlock();
 611 
 612         ovs_flow_free(flow, false);
 613         return err;
 614 
 615 err_unlock:
 616         rcu_read_unlock();
 617 err_flow_free:
 618         ovs_flow_free(flow, false);
 619 err_kfree_skb:
 620         kfree_skb(packet);
 621 err:
 622         return err;
 623 }
 624 
 625 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
 626         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
 627         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
 628         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
 629         [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
 630         [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
 631 };
 632 
 633 static const struct genl_ops dp_packet_genl_ops[] = {
 634         { .cmd = OVS_PACKET_CMD_EXECUTE,
 635           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
 636           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
 637           .doit = ovs_packet_cmd_execute
 638         }
 639 };
 640 
 641 static struct genl_family dp_packet_genl_family __ro_after_init = {
 642         .hdrsize = sizeof(struct ovs_header),
 643         .name = OVS_PACKET_FAMILY,
 644         .version = OVS_PACKET_VERSION,
 645         .maxattr = OVS_PACKET_ATTR_MAX,
 646         .policy = packet_policy,
 647         .netnsok = true,
 648         .parallel_ops = true,
 649         .ops = dp_packet_genl_ops,
 650         .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
 651         .module = THIS_MODULE,
 652 };
 653 
 654 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
 655                          struct ovs_dp_megaflow_stats *mega_stats)
 656 {
 657         int i;
 658 
 659         memset(mega_stats, 0, sizeof(*mega_stats));
 660 
 661         stats->n_flows = ovs_flow_tbl_count(&dp->table);
 662         mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
 663 
 664         stats->n_hit = stats->n_missed = stats->n_lost = 0;
 665 
 666         for_each_possible_cpu(i) {
 667                 const struct dp_stats_percpu *percpu_stats;
 668                 struct dp_stats_percpu local_stats;
 669                 unsigned int start;
 670 
 671                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
 672 
 673                 do {
 674                         start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
 675                         local_stats = *percpu_stats;
 676                 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
 677 
 678                 stats->n_hit += local_stats.n_hit;
 679                 stats->n_missed += local_stats.n_missed;
 680                 stats->n_lost += local_stats.n_lost;
 681                 mega_stats->n_mask_hit += local_stats.n_mask_hit;
 682         }
 683 }
 684 
 685 static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
 686 {
 687         return ovs_identifier_is_ufid(sfid) &&
 688                !(ufid_flags & OVS_UFID_F_OMIT_KEY);
 689 }
 690 
 691 static bool should_fill_mask(uint32_t ufid_flags)
 692 {
 693         return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
 694 }
 695 
 696 static bool should_fill_actions(uint32_t ufid_flags)
 697 {
 698         return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
 699 }
 700 
 701 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
 702                                     const struct sw_flow_id *sfid,
 703                                     uint32_t ufid_flags)
 704 {
 705         size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
 706 
 707         /* OVS_FLOW_ATTR_UFID, or unmasked flow key as fallback
 708          * see ovs_nla_put_identifier()
 709          */
 710         if (sfid && ovs_identifier_is_ufid(sfid))
 711                 len += nla_total_size(sfid->ufid_len);
 712         else
 713                 len += nla_total_size(ovs_key_attr_size());
 714 
 715         /* OVS_FLOW_ATTR_KEY */
 716         if (!sfid || should_fill_key(sfid, ufid_flags))
 717                 len += nla_total_size(ovs_key_attr_size());
 718 
 719         /* OVS_FLOW_ATTR_MASK */
 720         if (should_fill_mask(ufid_flags))
 721                 len += nla_total_size(ovs_key_attr_size());
 722 
 723         /* OVS_FLOW_ATTR_ACTIONS */
 724         if (should_fill_actions(ufid_flags))
 725                 len += nla_total_size(acts->orig_len);
 726 
 727         return len
 728                 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
 729                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
 730                 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
 731 }
 732 
 733 /* Called with ovs_mutex or RCU read lock. */
 734 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
 735                                    struct sk_buff *skb)
 736 {
 737         struct ovs_flow_stats stats;
 738         __be16 tcp_flags;
 739         unsigned long used;
 740 
 741         ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
 742 
 743         if (used &&
 744             nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
 745                               OVS_FLOW_ATTR_PAD))
 746                 return -EMSGSIZE;
 747 
 748         if (stats.n_packets &&
 749             nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
 750                           sizeof(struct ovs_flow_stats), &stats,
 751                           OVS_FLOW_ATTR_PAD))
 752                 return -EMSGSIZE;
 753 
 754         if ((u8)ntohs(tcp_flags) &&
 755              nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
 756                 return -EMSGSIZE;
 757 
 758         return 0;
 759 }
 760 
 761 /* Called with ovs_mutex or RCU read lock. */
 762 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
 763                                      struct sk_buff *skb, int skb_orig_len)
 764 {
 765         struct nlattr *start;
 766         int err;
 767 
 768         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
 769          * this is the first flow to be dumped into 'skb'.  This is unusual for
 770          * Netlink but individual action lists can be longer than
 771          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
 772          * The userspace caller can always fetch the actions separately if it
 773          * really wants them.  (Most userspace callers in fact don't care.)
 774          *
 775          * This can only fail for dump operations because the skb is always
 776          * properly sized for single flows.
 777          */
 778         start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
 779         if (start) {
 780                 const struct sw_flow_actions *sf_acts;
 781 
 782                 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
 783                 err = ovs_nla_put_actions(sf_acts->actions,
 784                                           sf_acts->actions_len, skb);
 785 
 786                 if (!err)
 787                         nla_nest_end(skb, start);
 788                 else {
 789                         if (skb_orig_len)
 790                                 return err;
 791 
 792                         nla_nest_cancel(skb, start);
 793                 }
 794         } else if (skb_orig_len) {
 795                 return -EMSGSIZE;
 796         }
 797 
 798         return 0;
 799 }
 800 
 801 /* Called with ovs_mutex or RCU read lock. */
 802 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
 803                                   struct sk_buff *skb, u32 portid,
 804                                   u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
 805 {
 806         const int skb_orig_len = skb->len;
 807         struct ovs_header *ovs_header;
 808         int err;
 809 
 810         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
 811                                  flags, cmd);
 812         if (!ovs_header)
 813                 return -EMSGSIZE;
 814 
 815         ovs_header->dp_ifindex = dp_ifindex;
 816 
 817         err = ovs_nla_put_identifier(flow, skb);
 818         if (err)
 819                 goto error;
 820 
 821         if (should_fill_key(&flow->id, ufid_flags)) {
 822                 err = ovs_nla_put_masked_key(flow, skb);
 823                 if (err)
 824                         goto error;
 825         }
 826 
 827         if (should_fill_mask(ufid_flags)) {
 828                 err = ovs_nla_put_mask(flow, skb);
 829                 if (err)
 830                         goto error;
 831         }
 832 
 833         err = ovs_flow_cmd_fill_stats(flow, skb);
 834         if (err)
 835                 goto error;
 836 
 837         if (should_fill_actions(ufid_flags)) {
 838                 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
 839                 if (err)
 840                         goto error;
 841         }
 842 
 843         genlmsg_end(skb, ovs_header);
 844         return 0;
 845 
 846 error:
 847         genlmsg_cancel(skb, ovs_header);
 848         return err;
 849 }
 850 
 851 /* May not be called with RCU read lock. */
 852 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
 853                                                const struct sw_flow_id *sfid,
 854                                                struct genl_info *info,
 855                                                bool always,
 856                                                uint32_t ufid_flags)
 857 {
 858         struct sk_buff *skb;
 859         size_t len;
 860 
 861         if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
 862                 return NULL;
 863 
 864         len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
 865         skb = genlmsg_new(len, GFP_KERNEL);
 866         if (!skb)
 867                 return ERR_PTR(-ENOMEM);
 868 
 869         return skb;
 870 }
 871 
 872 /* Called with ovs_mutex. */
 873 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
 874                                                int dp_ifindex,
 875                                                struct genl_info *info, u8 cmd,
 876                                                bool always, u32 ufid_flags)
 877 {
 878         struct sk_buff *skb;
 879         int retval;
 880 
 881         skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
 882                                       &flow->id, info, always, ufid_flags);
 883         if (IS_ERR_OR_NULL(skb))
 884                 return skb;
 885 
 886         retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
 887                                         info->snd_portid, info->snd_seq, 0,
 888                                         cmd, ufid_flags);
 889         if (WARN_ON_ONCE(retval < 0)) {
 890                 kfree_skb(skb);
 891                 skb = ERR_PTR(retval);
 892         }
 893         return skb;
 894 }
 895 
 896 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
 897 {
 898         struct net *net = sock_net(skb->sk);
 899         struct nlattr **a = info->attrs;
 900         struct ovs_header *ovs_header = info->userhdr;
 901         struct sw_flow *flow = NULL, *new_flow;
 902         struct sw_flow_mask mask;
 903         struct sk_buff *reply;
 904         struct datapath *dp;
 905         struct sw_flow_actions *acts;
 906         struct sw_flow_match match;
 907         u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
 908         int error;
 909         bool log = !a[OVS_FLOW_ATTR_PROBE];
 910 
 911         /* Must have key and actions. */
 912         error = -EINVAL;
 913         if (!a[OVS_FLOW_ATTR_KEY]) {
 914                 OVS_NLERR(log, "Flow key attr not present in new flow.");
 915                 goto error;
 916         }
 917         if (!a[OVS_FLOW_ATTR_ACTIONS]) {
 918                 OVS_NLERR(log, "Flow actions attr not present in new flow.");
 919                 goto error;
 920         }
 921 
 922         /* Most of the time we need to allocate a new flow, do it before
 923          * locking.
 924          */
 925         new_flow = ovs_flow_alloc();
 926         if (IS_ERR(new_flow)) {
 927                 error = PTR_ERR(new_flow);
 928                 goto error;
 929         }
 930 
 931         /* Extract key. */
 932         ovs_match_init(&match, &new_flow->key, false, &mask);
 933         error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
 934                                   a[OVS_FLOW_ATTR_MASK], log);
 935         if (error)
 936                 goto err_kfree_flow;
 937 
 938         /* Extract flow identifier. */
 939         error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
 940                                        &new_flow->key, log);
 941         if (error)
 942                 goto err_kfree_flow;
 943 
 944         /* unmasked key is needed to match when ufid is not used. */
 945         if (ovs_identifier_is_key(&new_flow->id))
 946                 match.key = new_flow->id.unmasked_key;
 947 
 948         ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
 949 
 950         /* Validate actions. */
 951         error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
 952                                      &new_flow->key, &acts, log);
 953         if (error) {
 954                 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
 955                 goto err_kfree_flow;
 956         }
 957 
 958         reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
 959                                         ufid_flags);
 960         if (IS_ERR(reply)) {
 961                 error = PTR_ERR(reply);
 962                 goto err_kfree_acts;
 963         }
 964 
 965         ovs_lock();
 966         dp = get_dp(net, ovs_header->dp_ifindex);
 967         if (unlikely(!dp)) {
 968                 error = -ENODEV;
 969                 goto err_unlock_ovs;
 970         }
 971 
 972         /* Check if this is a duplicate flow */
 973         if (ovs_identifier_is_ufid(&new_flow->id))
 974                 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
 975         if (!flow)
 976                 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
 977         if (likely(!flow)) {
 978                 rcu_assign_pointer(new_flow->sf_acts, acts);
 979 
 980                 /* Put flow in bucket. */
 981                 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
 982                 if (unlikely(error)) {
 983                         acts = NULL;
 984                         goto err_unlock_ovs;
 985                 }
 986 
 987                 if (unlikely(reply)) {
 988                         error = ovs_flow_cmd_fill_info(new_flow,
 989                                                        ovs_header->dp_ifindex,
 990                                                        reply, info->snd_portid,
 991                                                        info->snd_seq, 0,
 992                                                        OVS_FLOW_CMD_NEW,
 993                                                        ufid_flags);
 994                         BUG_ON(error < 0);
 995                 }
 996                 ovs_unlock();
 997         } else {
 998                 struct sw_flow_actions *old_acts;
 999 
1000                 /* Bail out if we're not allowed to modify an existing flow.
1001                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1002                  * because Generic Netlink treats the latter as a dump
1003                  * request.  We also accept NLM_F_EXCL in case that bug ever
1004                  * gets fixed.
1005                  */
1006                 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1007                                                          | NLM_F_EXCL))) {
1008                         error = -EEXIST;
1009                         goto err_unlock_ovs;
1010                 }
1011                 /* The flow identifier has to be the same for flow updates.
1012                  * Look for any overlapping flow.
1013                  */
1014                 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1015                         if (ovs_identifier_is_key(&flow->id))
1016                                 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1017                                                                  &match);
1018                         else /* UFID matches but key is different */
1019                                 flow = NULL;
1020                         if (!flow) {
1021                                 error = -ENOENT;
1022                                 goto err_unlock_ovs;
1023                         }
1024                 }
1025                 /* Update actions. */
1026                 old_acts = ovsl_dereference(flow->sf_acts);
1027                 rcu_assign_pointer(flow->sf_acts, acts);
1028 
1029                 if (unlikely(reply)) {
1030                         error = ovs_flow_cmd_fill_info(flow,
1031                                                        ovs_header->dp_ifindex,
1032                                                        reply, info->snd_portid,
1033                                                        info->snd_seq, 0,
1034                                                        OVS_FLOW_CMD_NEW,
1035                                                        ufid_flags);
1036                         BUG_ON(error < 0);
1037                 }
1038                 ovs_unlock();
1039 
1040                 ovs_nla_free_flow_actions_rcu(old_acts);
1041                 ovs_flow_free(new_flow, false);
1042         }
1043 
1044         if (reply)
1045                 ovs_notify(&dp_flow_genl_family, reply, info);
1046         return 0;
1047 
1048 err_unlock_ovs:
1049         ovs_unlock();
1050         kfree_skb(reply);
1051 err_kfree_acts:
1052         ovs_nla_free_flow_actions(acts);
1053 err_kfree_flow:
1054         ovs_flow_free(new_flow, false);
1055 error:
1056         return error;
1057 }
1058 
1059 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
1060 static noinline_for_stack struct sw_flow_actions *get_flow_actions(struct net *net,
1061                                                 const struct nlattr *a,
1062                                                 const struct sw_flow_key *key,
1063                                                 const struct sw_flow_mask *mask,
1064                                                 bool log)
1065 {
1066         struct sw_flow_actions *acts;
1067         struct sw_flow_key masked_key;
1068         int error;
1069 
1070         ovs_flow_mask_key(&masked_key, key, true, mask);
1071         error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
1072         if (error) {
1073                 OVS_NLERR(log,
1074                           "Actions may not be safe on all matching packets");
1075                 return ERR_PTR(error);
1076         }
1077 
1078         return acts;
1079 }
1080 
1081 /* Factor out match-init and action-copy to avoid
1082  * "Wframe-larger-than=1024" warning. Because mask is only
1083  * used to get actions, we new a function to save some
1084  * stack space.
1085  *
1086  * If there are not key and action attrs, we return 0
1087  * directly. In the case, the caller will also not use the
1088  * match as before. If there is action attr, we try to get
1089  * actions and save them to *acts. Before returning from
1090  * the function, we reset the match->mask pointer. Because
1091  * we should not to return match object with dangling reference
1092  * to mask.
1093  * */
1094 static noinline_for_stack int
1095 ovs_nla_init_match_and_action(struct net *net,
1096                               struct sw_flow_match *match,
1097                               struct sw_flow_key *key,
1098                               struct nlattr **a,
1099                               struct sw_flow_actions **acts,
1100                               bool log)
1101 {
1102         struct sw_flow_mask mask;
1103         int error = 0;
1104 
1105         if (a[OVS_FLOW_ATTR_KEY]) {
1106                 ovs_match_init(match, key, true, &mask);
1107                 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1108                                           a[OVS_FLOW_ATTR_MASK], log);
1109                 if (error)
1110                         goto error;
1111         }
1112 
1113         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1114                 if (!a[OVS_FLOW_ATTR_KEY]) {
1115                         OVS_NLERR(log,
1116                                   "Flow key attribute not present in set flow.");
1117                         error = -EINVAL;
1118                         goto error;
1119                 }
1120 
1121                 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1122                                          &mask, log);
1123                 if (IS_ERR(*acts)) {
1124                         error = PTR_ERR(*acts);
1125                         goto error;
1126                 }
1127         }
1128 
1129         /* On success, error is 0. */
1130 error:
1131         match->mask = NULL;
1132         return error;
1133 }
1134 
1135 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1136 {
1137         struct net *net = sock_net(skb->sk);
1138         struct nlattr **a = info->attrs;
1139         struct ovs_header *ovs_header = info->userhdr;
1140         struct sw_flow_key key;
1141         struct sw_flow *flow;
1142         struct sk_buff *reply = NULL;
1143         struct datapath *dp;
1144         struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1145         struct sw_flow_match match;
1146         struct sw_flow_id sfid;
1147         u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1148         int error = 0;
1149         bool log = !a[OVS_FLOW_ATTR_PROBE];
1150         bool ufid_present;
1151 
1152         ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
1153         if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
1154                 OVS_NLERR(log,
1155                           "Flow set message rejected, Key attribute missing.");
1156                 return -EINVAL;
1157         }
1158 
1159         error = ovs_nla_init_match_and_action(net, &match, &key, a,
1160                                               &acts, log);
1161         if (error)
1162                 goto error;
1163 
1164         if (acts) {
1165                 /* Can allocate before locking if have acts. */
1166                 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1167                                                 ufid_flags);
1168                 if (IS_ERR(reply)) {
1169                         error = PTR_ERR(reply);
1170                         goto err_kfree_acts;
1171                 }
1172         }
1173 
1174         ovs_lock();
1175         dp = get_dp(net, ovs_header->dp_ifindex);
1176         if (unlikely(!dp)) {
1177                 error = -ENODEV;
1178                 goto err_unlock_ovs;
1179         }
1180         /* Check that the flow exists. */
1181         if (ufid_present)
1182                 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1183         else
1184                 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1185         if (unlikely(!flow)) {
1186                 error = -ENOENT;
1187                 goto err_unlock_ovs;
1188         }
1189 
1190         /* Update actions, if present. */
1191         if (likely(acts)) {
1192                 old_acts = ovsl_dereference(flow->sf_acts);
1193                 rcu_assign_pointer(flow->sf_acts, acts);
1194 
1195                 if (unlikely(reply)) {
1196                         error = ovs_flow_cmd_fill_info(flow,
1197                                                        ovs_header->dp_ifindex,
1198                                                        reply, info->snd_portid,
1199                                                        info->snd_seq, 0,
1200                                                        OVS_FLOW_CMD_SET,
1201                                                        ufid_flags);
1202                         BUG_ON(error < 0);
1203                 }
1204         } else {
1205                 /* Could not alloc without acts before locking. */
1206                 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1207                                                 info, OVS_FLOW_CMD_SET, false,
1208                                                 ufid_flags);
1209 
1210                 if (IS_ERR(reply)) {
1211                         error = PTR_ERR(reply);
1212                         goto err_unlock_ovs;
1213                 }
1214         }
1215 
1216         /* Clear stats. */
1217         if (a[OVS_FLOW_ATTR_CLEAR])
1218                 ovs_flow_stats_clear(flow);
1219         ovs_unlock();
1220 
1221         if (reply)
1222                 ovs_notify(&dp_flow_genl_family, reply, info);
1223         if (old_acts)
1224                 ovs_nla_free_flow_actions_rcu(old_acts);
1225 
1226         return 0;
1227 
1228 err_unlock_ovs:
1229         ovs_unlock();
1230         kfree_skb(reply);
1231 err_kfree_acts:
1232         ovs_nla_free_flow_actions(acts);
1233 error:
1234         return error;
1235 }
1236 
1237 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1238 {
1239         struct nlattr **a = info->attrs;
1240         struct ovs_header *ovs_header = info->userhdr;
1241         struct net *net = sock_net(skb->sk);
1242         struct sw_flow_key key;
1243         struct sk_buff *reply;
1244         struct sw_flow *flow;
1245         struct datapath *dp;
1246         struct sw_flow_match match;
1247         struct sw_flow_id ufid;
1248         u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1249         int err = 0;
1250         bool log = !a[OVS_FLOW_ATTR_PROBE];
1251         bool ufid_present;
1252 
1253         ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1254         if (a[OVS_FLOW_ATTR_KEY]) {
1255                 ovs_match_init(&match, &key, true, NULL);
1256                 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
1257                                         log);
1258         } else if (!ufid_present) {
1259                 OVS_NLERR(log,
1260                           "Flow get message rejected, Key attribute missing.");
1261                 err = -EINVAL;
1262         }
1263         if (err)
1264                 return err;
1265 
1266         ovs_lock();
1267         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1268         if (!dp) {
1269                 err = -ENODEV;
1270                 goto unlock;
1271         }
1272 
1273         if (ufid_present)
1274                 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1275         else
1276                 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1277         if (!flow) {
1278                 err = -ENOENT;
1279                 goto unlock;
1280         }
1281 
1282         reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1283                                         OVS_FLOW_CMD_GET, true, ufid_flags);
1284         if (IS_ERR(reply)) {
1285                 err = PTR_ERR(reply);
1286                 goto unlock;
1287         }
1288 
1289         ovs_unlock();
1290         return genlmsg_reply(reply, info);
1291 unlock:
1292         ovs_unlock();
1293         return err;
1294 }
1295 
1296 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1297 {
1298         struct nlattr **a = info->attrs;
1299         struct ovs_header *ovs_header = info->userhdr;
1300         struct net *net = sock_net(skb->sk);
1301         struct sw_flow_key key;
1302         struct sk_buff *reply;
1303         struct sw_flow *flow = NULL;
1304         struct datapath *dp;
1305         struct sw_flow_match match;
1306         struct sw_flow_id ufid;
1307         u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1308         int err;
1309         bool log = !a[OVS_FLOW_ATTR_PROBE];
1310         bool ufid_present;
1311 
1312         ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1313         if (a[OVS_FLOW_ATTR_KEY]) {
1314                 ovs_match_init(&match, &key, true, NULL);
1315                 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1316                                         NULL, log);
1317                 if (unlikely(err))
1318                         return err;
1319         }
1320 
1321         ovs_lock();
1322         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1323         if (unlikely(!dp)) {
1324                 err = -ENODEV;
1325                 goto unlock;
1326         }
1327 
1328         if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
1329                 err = ovs_flow_tbl_flush(&dp->table);
1330                 goto unlock;
1331         }
1332 
1333         if (ufid_present)
1334                 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1335         else
1336                 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1337         if (unlikely(!flow)) {
1338                 err = -ENOENT;
1339                 goto unlock;
1340         }
1341 
1342         ovs_flow_tbl_remove(&dp->table, flow);
1343         ovs_unlock();
1344 
1345         reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1346                                         &flow->id, info, false, ufid_flags);
1347         if (likely(reply)) {
1348                 if (!IS_ERR(reply)) {
1349                         rcu_read_lock();        /*To keep RCU checker happy. */
1350                         err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1351                                                      reply, info->snd_portid,
1352                                                      info->snd_seq, 0,
1353                                                      OVS_FLOW_CMD_DEL,
1354                                                      ufid_flags);
1355                         rcu_read_unlock();
1356                         if (WARN_ON_ONCE(err < 0)) {
1357                                 kfree_skb(reply);
1358                                 goto out_free;
1359                         }
1360 
1361                         ovs_notify(&dp_flow_genl_family, reply, info);
1362                 } else {
1363                         netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1364                 }
1365         }
1366 
1367 out_free:
1368         ovs_flow_free(flow, true);
1369         return 0;
1370 unlock:
1371         ovs_unlock();
1372         return err;
1373 }
1374 
1375 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1376 {
1377         struct nlattr *a[__OVS_FLOW_ATTR_MAX];
1378         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1379         struct table_instance *ti;
1380         struct datapath *dp;
1381         u32 ufid_flags;
1382         int err;
1383 
1384         err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1385                                        OVS_FLOW_ATTR_MAX, flow_policy, NULL);
1386         if (err)
1387                 return err;
1388         ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1389 
1390         rcu_read_lock();
1391         dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1392         if (!dp) {
1393                 rcu_read_unlock();
1394                 return -ENODEV;
1395         }
1396 
1397         ti = rcu_dereference(dp->table.ti);
1398         for (;;) {
1399                 struct sw_flow *flow;
1400                 u32 bucket, obj;
1401 
1402                 bucket = cb->args[0];
1403                 obj = cb->args[1];
1404                 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1405                 if (!flow)
1406                         break;
1407 
1408                 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1409                                            NETLINK_CB(cb->skb).portid,
1410                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1411                                            OVS_FLOW_CMD_GET, ufid_flags) < 0)
1412                         break;
1413 
1414                 cb->args[0] = bucket;
1415                 cb->args[1] = obj;
1416         }
1417         rcu_read_unlock();
1418         return skb->len;
1419 }
1420 
1421 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1422         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1423         [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
1424         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1425         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1426         [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
1427         [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1428         [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
1429 };
1430 
1431 static const struct genl_ops dp_flow_genl_ops[] = {
1432         { .cmd = OVS_FLOW_CMD_NEW,
1433           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1434           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1435           .doit = ovs_flow_cmd_new
1436         },
1437         { .cmd = OVS_FLOW_CMD_DEL,
1438           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1439           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1440           .doit = ovs_flow_cmd_del
1441         },
1442         { .cmd = OVS_FLOW_CMD_GET,
1443           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1444           .flags = 0,               /* OK for unprivileged users. */
1445           .doit = ovs_flow_cmd_get,
1446           .dumpit = ovs_flow_cmd_dump
1447         },
1448         { .cmd = OVS_FLOW_CMD_SET,
1449           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1450           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1451           .doit = ovs_flow_cmd_set,
1452         },
1453 };
1454 
1455 static struct genl_family dp_flow_genl_family __ro_after_init = {
1456         .hdrsize = sizeof(struct ovs_header),
1457         .name = OVS_FLOW_FAMILY,
1458         .version = OVS_FLOW_VERSION,
1459         .maxattr = OVS_FLOW_ATTR_MAX,
1460         .policy = flow_policy,
1461         .netnsok = true,
1462         .parallel_ops = true,
1463         .ops = dp_flow_genl_ops,
1464         .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1465         .mcgrps = &ovs_dp_flow_multicast_group,
1466         .n_mcgrps = 1,
1467         .module = THIS_MODULE,
1468 };
1469 
1470 static size_t ovs_dp_cmd_msg_size(void)
1471 {
1472         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1473 
1474         msgsize += nla_total_size(IFNAMSIZ);
1475         msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1476         msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
1477         msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1478 
1479         return msgsize;
1480 }
1481 
1482 /* Called with ovs_mutex. */
1483 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1484                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1485 {
1486         struct ovs_header *ovs_header;
1487         struct ovs_dp_stats dp_stats;
1488         struct ovs_dp_megaflow_stats dp_megaflow_stats;
1489         int err;
1490 
1491         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1492                                    flags, cmd);
1493         if (!ovs_header)
1494                 goto error;
1495 
1496         ovs_header->dp_ifindex = get_dpifindex(dp);
1497 
1498         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1499         if (err)
1500                 goto nla_put_failure;
1501 
1502         get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1503         if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1504                           &dp_stats, OVS_DP_ATTR_PAD))
1505                 goto nla_put_failure;
1506 
1507         if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1508                           sizeof(struct ovs_dp_megaflow_stats),
1509                           &dp_megaflow_stats, OVS_DP_ATTR_PAD))
1510                 goto nla_put_failure;
1511 
1512         if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1513                 goto nla_put_failure;
1514 
1515         genlmsg_end(skb, ovs_header);
1516         return 0;
1517 
1518 nla_put_failure:
1519         genlmsg_cancel(skb, ovs_header);
1520 error:
1521         return -EMSGSIZE;
1522 }
1523 
1524 static struct sk_buff *ovs_dp_cmd_alloc_info(void)
1525 {
1526         return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
1527 }
1528 
1529 /* Called with rcu_read_lock or ovs_mutex. */
1530 static struct datapath *lookup_datapath(struct net *net,
1531                                         const struct ovs_header *ovs_header,
1532                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1533 {
1534         struct datapath *dp;
1535 
1536         if (!a[OVS_DP_ATTR_NAME])
1537                 dp = get_dp(net, ovs_header->dp_ifindex);
1538         else {
1539                 struct vport *vport;
1540 
1541                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1542                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1543         }
1544         return dp ? dp : ERR_PTR(-ENODEV);
1545 }
1546 
1547 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1548 {
1549         struct datapath *dp;
1550 
1551         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1552         if (IS_ERR(dp))
1553                 return;
1554 
1555         WARN(dp->user_features, "Dropping previously announced user features\n");
1556         dp->user_features = 0;
1557 }
1558 
1559 DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1560 
1561 static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1562 {
1563         u32 user_features = 0;
1564 
1565         if (a[OVS_DP_ATTR_USER_FEATURES]) {
1566                 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1567 
1568                 if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1569                                       OVS_DP_F_UNALIGNED |
1570                                       OVS_DP_F_TC_RECIRC_SHARING))
1571                         return -EOPNOTSUPP;
1572 
1573 #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1574                 if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1575                         return -EOPNOTSUPP;
1576 #endif
1577         }
1578 
1579         dp->user_features = user_features;
1580 
1581         if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1582                 static_branch_enable(&tc_recirc_sharing_support);
1583         else
1584                 static_branch_disable(&tc_recirc_sharing_support);
1585 
1586         return 0;
1587 }
1588 
1589 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1590 {
1591         struct nlattr **a = info->attrs;
1592         struct vport_parms parms;
1593         struct sk_buff *reply;
1594         struct datapath *dp;
1595         struct vport *vport;
1596         struct ovs_net *ovs_net;
1597         int err, i;
1598 
1599         err = -EINVAL;
1600         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1601                 goto err;
1602 
1603         reply = ovs_dp_cmd_alloc_info();
1604         if (!reply)
1605                 return -ENOMEM;
1606 
1607         err = -ENOMEM;
1608         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1609         if (dp == NULL)
1610                 goto err_free_reply;
1611 
1612         ovs_dp_set_net(dp, sock_net(skb->sk));
1613 
1614         /* Allocate table. */
1615         err = ovs_flow_tbl_init(&dp->table);
1616         if (err)
1617                 goto err_free_dp;
1618 
1619         dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1620         if (!dp->stats_percpu) {
1621                 err = -ENOMEM;
1622                 goto err_destroy_table;
1623         }
1624 
1625         dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1626                                   sizeof(struct hlist_head),
1627                                   GFP_KERNEL);
1628         if (!dp->ports) {
1629                 err = -ENOMEM;
1630                 goto err_destroy_percpu;
1631         }
1632 
1633         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1634                 INIT_HLIST_HEAD(&dp->ports[i]);
1635 
1636         err = ovs_meters_init(dp);
1637         if (err)
1638                 goto err_destroy_ports_array;
1639 
1640         /* Set up our datapath device. */
1641         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1642         parms.type = OVS_VPORT_TYPE_INTERNAL;
1643         parms.options = NULL;
1644         parms.dp = dp;
1645         parms.port_no = OVSP_LOCAL;
1646         parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1647 
1648         err = ovs_dp_change(dp, a);
1649         if (err)
1650                 goto err_destroy_meters;
1651 
1652         /* So far only local changes have been made, now need the lock. */
1653         ovs_lock();
1654 
1655         vport = new_vport(&parms);
1656         if (IS_ERR(vport)) {
1657                 err = PTR_ERR(vport);
1658                 if (err == -EBUSY)
1659                         err = -EEXIST;
1660 
1661                 if (err == -EEXIST) {
1662                         /* An outdated user space instance that does not understand
1663                          * the concept of user_features has attempted to create a new
1664                          * datapath and is likely to reuse it. Drop all user features.
1665                          */
1666                         if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1667                                 ovs_dp_reset_user_features(skb, info);
1668                 }
1669 
1670                 ovs_unlock();
1671                 goto err_destroy_meters;
1672         }
1673 
1674         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1675                                    info->snd_seq, 0, OVS_DP_CMD_NEW);
1676         BUG_ON(err < 0);
1677 
1678         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1679         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1680 
1681         ovs_unlock();
1682 
1683         ovs_notify(&dp_datapath_genl_family, reply, info);
1684         return 0;
1685 
1686 err_destroy_meters:
1687         ovs_meters_exit(dp);
1688 err_destroy_ports_array:
1689         kfree(dp->ports);
1690 err_destroy_percpu:
1691         free_percpu(dp->stats_percpu);
1692 err_destroy_table:
1693         ovs_flow_tbl_destroy(&dp->table);
1694 err_free_dp:
1695         kfree(dp);
1696 err_free_reply:
1697         kfree_skb(reply);
1698 err:
1699         return err;
1700 }
1701 
1702 /* Called with ovs_mutex. */
1703 static void __dp_destroy(struct datapath *dp)
1704 {
1705         int i;
1706 
1707         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1708                 struct vport *vport;
1709                 struct hlist_node *n;
1710 
1711                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1712                         if (vport->port_no != OVSP_LOCAL)
1713                                 ovs_dp_detach_port(vport);
1714         }
1715 
1716         list_del_rcu(&dp->list_node);
1717 
1718         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1719          * all ports in datapath are destroyed first before freeing datapath.
1720          */
1721         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1722 
1723         /* RCU destroy the flow table */
1724         call_rcu(&dp->rcu, destroy_dp_rcu);
1725 }
1726 
1727 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1728 {
1729         struct sk_buff *reply;
1730         struct datapath *dp;
1731         int err;
1732 
1733         reply = ovs_dp_cmd_alloc_info();
1734         if (!reply)
1735                 return -ENOMEM;
1736 
1737         ovs_lock();
1738         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1739         err = PTR_ERR(dp);
1740         if (IS_ERR(dp))
1741                 goto err_unlock_free;
1742 
1743         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1744                                    info->snd_seq, 0, OVS_DP_CMD_DEL);
1745         BUG_ON(err < 0);
1746 
1747         __dp_destroy(dp);
1748         ovs_unlock();
1749 
1750         ovs_notify(&dp_datapath_genl_family, reply, info);
1751 
1752         return 0;
1753 
1754 err_unlock_free:
1755         ovs_unlock();
1756         kfree_skb(reply);
1757         return err;
1758 }
1759 
1760 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1761 {
1762         struct sk_buff *reply;
1763         struct datapath *dp;
1764         int err;
1765 
1766         reply = ovs_dp_cmd_alloc_info();
1767         if (!reply)
1768                 return -ENOMEM;
1769 
1770         ovs_lock();
1771         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1772         err = PTR_ERR(dp);
1773         if (IS_ERR(dp))
1774                 goto err_unlock_free;
1775 
1776         err = ovs_dp_change(dp, info->attrs);
1777         if (err)
1778                 goto err_unlock_free;
1779 
1780         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1781                                    info->snd_seq, 0, OVS_DP_CMD_SET);
1782         BUG_ON(err < 0);
1783 
1784         ovs_unlock();
1785         ovs_notify(&dp_datapath_genl_family, reply, info);
1786 
1787         return 0;
1788 
1789 err_unlock_free:
1790         ovs_unlock();
1791         kfree_skb(reply);
1792         return err;
1793 }
1794 
1795 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1796 {
1797         struct sk_buff *reply;
1798         struct datapath *dp;
1799         int err;
1800 
1801         reply = ovs_dp_cmd_alloc_info();
1802         if (!reply)
1803                 return -ENOMEM;
1804 
1805         ovs_lock();
1806         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1807         if (IS_ERR(dp)) {
1808                 err = PTR_ERR(dp);
1809                 goto err_unlock_free;
1810         }
1811         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1812                                    info->snd_seq, 0, OVS_DP_CMD_GET);
1813         BUG_ON(err < 0);
1814         ovs_unlock();
1815 
1816         return genlmsg_reply(reply, info);
1817 
1818 err_unlock_free:
1819         ovs_unlock();
1820         kfree_skb(reply);
1821         return err;
1822 }
1823 
1824 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1825 {
1826         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1827         struct datapath *dp;
1828         int skip = cb->args[0];
1829         int i = 0;
1830 
1831         ovs_lock();
1832         list_for_each_entry(dp, &ovs_net->dps, list_node) {
1833                 if (i >= skip &&
1834                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1835                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1836                                          OVS_DP_CMD_GET) < 0)
1837                         break;
1838                 i++;
1839         }
1840         ovs_unlock();
1841 
1842         cb->args[0] = i;
1843 
1844         return skb->len;
1845 }
1846 
1847 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1848         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1849         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1850         [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1851 };
1852 
1853 static const struct genl_ops dp_datapath_genl_ops[] = {
1854         { .cmd = OVS_DP_CMD_NEW,
1855           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1856           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1857           .doit = ovs_dp_cmd_new
1858         },
1859         { .cmd = OVS_DP_CMD_DEL,
1860           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1861           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1862           .doit = ovs_dp_cmd_del
1863         },
1864         { .cmd = OVS_DP_CMD_GET,
1865           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1866           .flags = 0,               /* OK for unprivileged users. */
1867           .doit = ovs_dp_cmd_get,
1868           .dumpit = ovs_dp_cmd_dump
1869         },
1870         { .cmd = OVS_DP_CMD_SET,
1871           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1872           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1873           .doit = ovs_dp_cmd_set,
1874         },
1875 };
1876 
1877 static struct genl_family dp_datapath_genl_family __ro_after_init = {
1878         .hdrsize = sizeof(struct ovs_header),
1879         .name = OVS_DATAPATH_FAMILY,
1880         .version = OVS_DATAPATH_VERSION,
1881         .maxattr = OVS_DP_ATTR_MAX,
1882         .policy = datapath_policy,
1883         .netnsok = true,
1884         .parallel_ops = true,
1885         .ops = dp_datapath_genl_ops,
1886         .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1887         .mcgrps = &ovs_dp_datapath_multicast_group,
1888         .n_mcgrps = 1,
1889         .module = THIS_MODULE,
1890 };
1891 
1892 /* Called with ovs_mutex or RCU read lock. */
1893 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1894                                    struct net *net, u32 portid, u32 seq,
1895                                    u32 flags, u8 cmd, gfp_t gfp)
1896 {
1897         struct ovs_header *ovs_header;
1898         struct ovs_vport_stats vport_stats;
1899         int err;
1900 
1901         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1902                                  flags, cmd);
1903         if (!ovs_header)
1904                 return -EMSGSIZE;
1905 
1906         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1907 
1908         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1909             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1910             nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1911                            ovs_vport_name(vport)) ||
1912             nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
1913                 goto nla_put_failure;
1914 
1915         if (!net_eq(net, dev_net(vport->dev))) {
1916                 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
1917 
1918                 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1919                         goto nla_put_failure;
1920         }
1921 
1922         ovs_vport_get_stats(vport, &vport_stats);
1923         if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1924                           sizeof(struct ovs_vport_stats), &vport_stats,
1925                           OVS_VPORT_ATTR_PAD))
1926                 goto nla_put_failure;
1927 
1928         if (ovs_vport_get_upcall_portids(vport, skb))
1929                 goto nla_put_failure;
1930 
1931         err = ovs_vport_get_options(vport, skb);
1932         if (err == -EMSGSIZE)
1933                 goto error;
1934 
1935         genlmsg_end(skb, ovs_header);
1936         return 0;
1937 
1938 nla_put_failure:
1939         err = -EMSGSIZE;
1940 error:
1941         genlmsg_cancel(skb, ovs_header);
1942         return err;
1943 }
1944 
1945 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1946 {
1947         return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1948 }
1949 
1950 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1951 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1952                                          u32 portid, u32 seq, u8 cmd)
1953 {
1954         struct sk_buff *skb;
1955         int retval;
1956 
1957         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1958         if (!skb)
1959                 return ERR_PTR(-ENOMEM);
1960 
1961         retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
1962                                          GFP_KERNEL);
1963         BUG_ON(retval < 0);
1964 
1965         return skb;
1966 }
1967 
1968 /* Called with ovs_mutex or RCU read lock. */
1969 static struct vport *lookup_vport(struct net *net,
1970                                   const struct ovs_header *ovs_header,
1971                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1972 {
1973         struct datapath *dp;
1974         struct vport *vport;
1975 
1976         if (a[OVS_VPORT_ATTR_IFINDEX])
1977                 return ERR_PTR(-EOPNOTSUPP);
1978         if (a[OVS_VPORT_ATTR_NAME]) {
1979                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1980                 if (!vport)
1981                         return ERR_PTR(-ENODEV);
1982                 if (ovs_header->dp_ifindex &&
1983                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1984                         return ERR_PTR(-ENODEV);
1985                 return vport;
1986         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1987                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1988 
1989                 if (port_no >= DP_MAX_PORTS)
1990                         return ERR_PTR(-EFBIG);
1991 
1992                 dp = get_dp(net, ovs_header->dp_ifindex);
1993                 if (!dp)
1994                         return ERR_PTR(-ENODEV);
1995 
1996                 vport = ovs_vport_ovsl_rcu(dp, port_no);
1997                 if (!vport)
1998                         return ERR_PTR(-ENODEV);
1999                 return vport;
2000         } else
2001                 return ERR_PTR(-EINVAL);
2002 
2003 }
2004 
2005 static unsigned int ovs_get_max_headroom(struct datapath *dp)
2006 {
2007         unsigned int dev_headroom, max_headroom = 0;
2008         struct net_device *dev;
2009         struct vport *vport;
2010         int i;
2011 
2012         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2013                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2014                         dev = vport->dev;
2015                         dev_headroom = netdev_get_fwd_headroom(dev);
2016                         if (dev_headroom > max_headroom)
2017                                 max_headroom = dev_headroom;
2018                 }
2019         }
2020 
2021         return max_headroom;
2022 }
2023 
2024 /* Called with ovs_mutex */
2025 static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2026 {
2027         struct vport *vport;
2028         int i;
2029 
2030         dp->max_headroom = new_headroom;
2031         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
2032                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
2033                         netdev_set_rx_headroom(vport->dev, new_headroom);
2034 }
2035 
2036 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2037 {
2038         struct nlattr **a = info->attrs;
2039         struct ovs_header *ovs_header = info->userhdr;
2040         struct vport_parms parms;
2041         struct sk_buff *reply;
2042         struct vport *vport;
2043         struct datapath *dp;
2044         unsigned int new_headroom;
2045         u32 port_no;
2046         int err;
2047 
2048         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2049             !a[OVS_VPORT_ATTR_UPCALL_PID])
2050                 return -EINVAL;
2051         if (a[OVS_VPORT_ATTR_IFINDEX])
2052                 return -EOPNOTSUPP;
2053 
2054         port_no = a[OVS_VPORT_ATTR_PORT_NO]
2055                 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2056         if (port_no >= DP_MAX_PORTS)
2057                 return -EFBIG;
2058 
2059         reply = ovs_vport_cmd_alloc_info();
2060         if (!reply)
2061                 return -ENOMEM;
2062 
2063         ovs_lock();
2064 restart:
2065         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2066         err = -ENODEV;
2067         if (!dp)
2068                 goto exit_unlock_free;
2069 
2070         if (port_no) {
2071                 vport = ovs_vport_ovsl(dp, port_no);
2072                 err = -EBUSY;
2073                 if (vport)
2074                         goto exit_unlock_free;
2075         } else {
2076                 for (port_no = 1; ; port_no++) {
2077                         if (port_no >= DP_MAX_PORTS) {
2078                                 err = -EFBIG;
2079                                 goto exit_unlock_free;
2080                         }
2081                         vport = ovs_vport_ovsl(dp, port_no);
2082                         if (!vport)
2083                                 break;
2084                 }
2085         }
2086 
2087         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2088         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2089         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2090         parms.dp = dp;
2091         parms.port_no = port_no;
2092         parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
2093 
2094         vport = new_vport(&parms);
2095         err = PTR_ERR(vport);
2096         if (IS_ERR(vport)) {
2097                 if (err == -EAGAIN)
2098                         goto restart;
2099                 goto exit_unlock_free;
2100         }
2101 
2102         err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2103                                       info->snd_portid, info->snd_seq, 0,
2104                                       OVS_VPORT_CMD_NEW, GFP_KERNEL);
2105 
2106         new_headroom = netdev_get_fwd_headroom(vport->dev);
2107 
2108         if (new_headroom > dp->max_headroom)
2109                 ovs_update_headroom(dp, new_headroom);
2110         else
2111                 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2112 
2113         BUG_ON(err < 0);
2114         ovs_unlock();
2115 
2116         ovs_notify(&dp_vport_genl_family, reply, info);
2117         return 0;
2118 
2119 exit_unlock_free:
2120         ovs_unlock();
2121         kfree_skb(reply);
2122         return err;
2123 }
2124 
2125 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2126 {
2127         struct nlattr **a = info->attrs;
2128         struct sk_buff *reply;
2129         struct vport *vport;
2130         int err;
2131 
2132         reply = ovs_vport_cmd_alloc_info();
2133         if (!reply)
2134                 return -ENOMEM;
2135 
2136         ovs_lock();
2137         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2138         err = PTR_ERR(vport);
2139         if (IS_ERR(vport))
2140                 goto exit_unlock_free;
2141 
2142         if (a[OVS_VPORT_ATTR_TYPE] &&
2143             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
2144                 err = -EINVAL;
2145                 goto exit_unlock_free;
2146         }
2147 
2148         if (a[OVS_VPORT_ATTR_OPTIONS]) {
2149                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2150                 if (err)
2151                         goto exit_unlock_free;
2152         }
2153 
2154 
2155         if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2156                 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2157 
2158                 err = ovs_vport_set_upcall_portids(vport, ids);
2159                 if (err)
2160                         goto exit_unlock_free;
2161         }
2162 
2163         err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2164                                       info->snd_portid, info->snd_seq, 0,
2165                                       OVS_VPORT_CMD_SET, GFP_KERNEL);
2166         BUG_ON(err < 0);
2167 
2168         ovs_unlock();
2169         ovs_notify(&dp_vport_genl_family, reply, info);
2170         return 0;
2171 
2172 exit_unlock_free:
2173         ovs_unlock();
2174         kfree_skb(reply);
2175         return err;
2176 }
2177 
2178 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2179 {
2180         bool update_headroom = false;
2181         struct nlattr **a = info->attrs;
2182         struct sk_buff *reply;
2183         struct datapath *dp;
2184         struct vport *vport;
2185         unsigned int new_headroom;
2186         int err;
2187 
2188         reply = ovs_vport_cmd_alloc_info();
2189         if (!reply)
2190                 return -ENOMEM;
2191 
2192         ovs_lock();
2193         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2194         err = PTR_ERR(vport);
2195         if (IS_ERR(vport))
2196                 goto exit_unlock_free;
2197 
2198         if (vport->port_no == OVSP_LOCAL) {
2199                 err = -EINVAL;
2200                 goto exit_unlock_free;
2201         }
2202 
2203         err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2204                                       info->snd_portid, info->snd_seq, 0,
2205                                       OVS_VPORT_CMD_DEL, GFP_KERNEL);
2206         BUG_ON(err < 0);
2207 
2208         /* the vport deletion may trigger dp headroom update */
2209         dp = vport->dp;
2210         if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2211                 update_headroom = true;
2212 
2213         netdev_reset_rx_headroom(vport->dev);
2214         ovs_dp_detach_port(vport);
2215 
2216         if (update_headroom) {
2217                 new_headroom = ovs_get_max_headroom(dp);
2218 
2219                 if (new_headroom < dp->max_headroom)
2220                         ovs_update_headroom(dp, new_headroom);
2221         }
2222         ovs_unlock();
2223 
2224         ovs_notify(&dp_vport_genl_family, reply, info);
2225         return 0;
2226 
2227 exit_unlock_free:
2228         ovs_unlock();
2229         kfree_skb(reply);
2230         return err;
2231 }
2232 
2233 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2234 {
2235         struct nlattr **a = info->attrs;
2236         struct ovs_header *ovs_header = info->userhdr;
2237         struct sk_buff *reply;
2238         struct vport *vport;
2239         int err;
2240 
2241         reply = ovs_vport_cmd_alloc_info();
2242         if (!reply)
2243                 return -ENOMEM;
2244 
2245         rcu_read_lock();
2246         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2247         err = PTR_ERR(vport);
2248         if (IS_ERR(vport))
2249                 goto exit_unlock_free;
2250         err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2251                                       info->snd_portid, info->snd_seq, 0,
2252                                       OVS_VPORT_CMD_GET, GFP_ATOMIC);
2253         BUG_ON(err < 0);
2254         rcu_read_unlock();
2255 
2256         return genlmsg_reply(reply, info);
2257 
2258 exit_unlock_free:
2259         rcu_read_unlock();
2260         kfree_skb(reply);
2261         return err;
2262 }
2263 
2264 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2265 {
2266         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2267         struct datapath *dp;
2268         int bucket = cb->args[0], skip = cb->args[1];
2269         int i, j = 0;
2270 
2271         rcu_read_lock();
2272         dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
2273         if (!dp) {
2274                 rcu_read_unlock();
2275                 return -ENODEV;
2276         }
2277         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2278                 struct vport *vport;
2279 
2280                 j = 0;
2281                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2282                         if (j >= skip &&
2283                             ovs_vport_cmd_fill_info(vport, skb,
2284                                                     sock_net(skb->sk),
2285                                                     NETLINK_CB(cb->skb).portid,
2286                                                     cb->nlh->nlmsg_seq,
2287                                                     NLM_F_MULTI,
2288                                                     OVS_VPORT_CMD_GET,
2289                                                     GFP_ATOMIC) < 0)
2290                                 goto out;
2291 
2292                         j++;
2293                 }
2294                 skip = 0;
2295         }
2296 out:
2297         rcu_read_unlock();
2298 
2299         cb->args[0] = i;
2300         cb->args[1] = j;
2301 
2302         return skb->len;
2303 }
2304 
2305 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2306         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2307         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2308         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2309         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2310         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
2311         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2312         [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2313         [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
2314 };
2315 
2316 static const struct genl_ops dp_vport_genl_ops[] = {
2317         { .cmd = OVS_VPORT_CMD_NEW,
2318           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2319           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2320           .doit = ovs_vport_cmd_new
2321         },
2322         { .cmd = OVS_VPORT_CMD_DEL,
2323           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2324           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2325           .doit = ovs_vport_cmd_del
2326         },
2327         { .cmd = OVS_VPORT_CMD_GET,
2328           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2329           .flags = 0,               /* OK for unprivileged users. */
2330           .doit = ovs_vport_cmd_get,
2331           .dumpit = ovs_vport_cmd_dump
2332         },
2333         { .cmd = OVS_VPORT_CMD_SET,
2334           .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2335           .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2336           .doit = ovs_vport_cmd_set,
2337         },
2338 };
2339 
2340 struct genl_family dp_vport_genl_family __ro_after_init = {
2341         .hdrsize = sizeof(struct ovs_header),
2342         .name = OVS_VPORT_FAMILY,
2343         .version = OVS_VPORT_VERSION,
2344         .maxattr = OVS_VPORT_ATTR_MAX,
2345         .policy = vport_policy,
2346         .netnsok = true,
2347         .parallel_ops = true,
2348         .ops = dp_vport_genl_ops,
2349         .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2350         .mcgrps = &ovs_dp_vport_multicast_group,
2351         .n_mcgrps = 1,
2352         .module = THIS_MODULE,
2353 };
2354 
2355 static struct genl_family * const dp_genl_families[] = {
2356         &dp_datapath_genl_family,
2357         &dp_vport_genl_family,
2358         &dp_flow_genl_family,
2359         &dp_packet_genl_family,
2360         &dp_meter_genl_family,
2361 #if     IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2362         &dp_ct_limit_genl_family,
2363 #endif
2364 };
2365 
2366 static void dp_unregister_genl(int n_families)
2367 {
2368         int i;
2369 
2370         for (i = 0; i < n_families; i++)
2371                 genl_unregister_family(dp_genl_families[i]);
2372 }
2373 
2374 static int __init dp_register_genl(void)
2375 {
2376         int err;
2377         int i;
2378 
2379         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2380 
2381                 err = genl_register_family(dp_genl_families[i]);
2382                 if (err)
2383                         goto error;
2384         }
2385 
2386         return 0;
2387 
2388 error:
2389         dp_unregister_genl(i);
2390         return err;
2391 }
2392 
2393 static int __net_init ovs_init_net(struct net *net)
2394 {
2395         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2396 
2397         INIT_LIST_HEAD(&ovs_net->dps);
2398         INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2399         return ovs_ct_init(net);
2400 }
2401 
2402 static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2403                                             struct list_head *head)
2404 {
2405         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2406         struct datapath *dp;
2407 
2408         list_for_each_entry(dp, &ovs_net->dps, list_node) {
2409                 int i;
2410 
2411                 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2412                         struct vport *vport;
2413 
2414                         hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2415                                 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2416                                         continue;
2417 
2418                                 if (dev_net(vport->dev) == dnet)
2419                                         list_add(&vport->detach_list, head);
2420                         }
2421                 }
2422         }
2423 }
2424 
2425 static void __net_exit ovs_exit_net(struct net *dnet)
2426 {
2427         struct datapath *dp, *dp_next;
2428         struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2429         struct vport *vport, *vport_next;
2430         struct net *net;
2431         LIST_HEAD(head);
2432 
2433         ovs_lock();
2434 
2435         ovs_ct_exit(dnet);
2436 
2437         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2438                 __dp_destroy(dp);
2439 
2440         down_read(&net_rwsem);
2441         for_each_net(net)
2442                 list_vports_from_net(net, dnet, &head);
2443         up_read(&net_rwsem);
2444 
2445         /* Detach all vports from given namespace. */
2446         list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2447                 list_del(&vport->detach_list);
2448                 ovs_dp_detach_port(vport);
2449         }
2450 
2451         ovs_unlock();
2452 
2453         cancel_work_sync(&ovs_net->dp_notify_work);
2454 }
2455 
2456 static struct pernet_operations ovs_net_ops = {
2457         .init = ovs_init_net,
2458         .exit = ovs_exit_net,
2459         .id   = &ovs_net_id,
2460         .size = sizeof(struct ovs_net),
2461 };
2462 
2463 static int __init dp_init(void)
2464 {
2465         int err;
2466 
2467         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2468 
2469         pr_info("Open vSwitch switching datapath\n");
2470 
2471         err = action_fifos_init();
2472         if (err)
2473                 goto error;
2474 
2475         err = ovs_internal_dev_rtnl_link_register();
2476         if (err)
2477                 goto error_action_fifos_exit;
2478 
2479         err = ovs_flow_init();
2480         if (err)
2481                 goto error_unreg_rtnl_link;
2482 
2483         err = ovs_vport_init();
2484         if (err)
2485                 goto error_flow_exit;
2486 
2487         err = register_pernet_device(&ovs_net_ops);
2488         if (err)
2489                 goto error_vport_exit;
2490 
2491         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2492         if (err)
2493                 goto error_netns_exit;
2494 
2495         err = ovs_netdev_init();
2496         if (err)
2497                 goto error_unreg_notifier;
2498 
2499         err = dp_register_genl();
2500         if (err < 0)
2501                 goto error_unreg_netdev;
2502 
2503         return 0;
2504 
2505 error_unreg_netdev:
2506         ovs_netdev_exit();
2507 error_unreg_notifier:
2508         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2509 error_netns_exit:
2510         unregister_pernet_device(&ovs_net_ops);
2511 error_vport_exit:
2512         ovs_vport_exit();
2513 error_flow_exit:
2514         ovs_flow_exit();
2515 error_unreg_rtnl_link:
2516         ovs_internal_dev_rtnl_link_unregister();
2517 error_action_fifos_exit:
2518         action_fifos_exit();
2519 error:
2520         return err;
2521 }
2522 
2523 static void dp_cleanup(void)
2524 {
2525         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2526         ovs_netdev_exit();
2527         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2528         unregister_pernet_device(&ovs_net_ops);
2529         rcu_barrier();
2530         ovs_vport_exit();
2531         ovs_flow_exit();
2532         ovs_internal_dev_rtnl_link_unregister();
2533         action_fifos_exit();
2534 }
2535 
2536 module_init(dp_init);
2537 module_exit(dp_cleanup);
2538 
2539 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2540 MODULE_LICENSE("GPL");
2541 MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2542 MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2543 MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2544 MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
2545 MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
2546 MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);

/* [<][>][^][v][top][bottom][index][help] */