/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Copyright (c) 2007-2014 Nicira, Inc.3*/45#ifndef DATAPATH_H6#define DATAPATH_H 178#include <asm/page.h>9#include <linux/kernel.h>10#include <linux/mutex.h>11#include <linux/netdevice.h>12#include <linux/skbuff.h>13#include <linux/u64_stats_sync.h>14#include <net/ip_tunnels.h>15#include <net/mpls.h>1617#include "conntrack.h"18#include "flow.h"19#include "flow_table.h"20#include "meter.h"21#include "vport-internal_dev.h"2223#define DP_MAX_PORTS USHRT_MAX24#define DP_VPORT_HASH_BUCKETS 102425#define DP_MASKS_REBALANCE_INTERVAL 40002627/**28* struct dp_stats_percpu - per-cpu packet processing statistics for a given29* datapath.30* @n_hit: Number of received packets for which a matching flow was found in31* the flow table.32* @n_missed: Number of received packets that had no matching flow in the flow33* table. The sum of @n_hit and @n_missed is the number of packets that have34* been received by the datapath.35* @n_lost: Number of received packets that had no matching flow in the flow36* table that could not be sent to userspace (normally due to an overflow in37* one of the datapath's queues).38* @n_mask_hit: Number of masks looked up for flow match.39* @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked40* up per packet.41* @n_cache_hit: The number of received packets that had their mask found using42* the mask cache.43* @syncp: Synchronization point for 64bit counters.44*/45struct dp_stats_percpu {46u64 n_hit;47u64 n_missed;48u64 n_lost;49u64 n_mask_hit;50u64 n_cache_hit;51struct u64_stats_sync syncp;52};5354/**55* struct dp_nlsk_pids - array of netlink portids of for a datapath.56* This is used when OVS_DP_F_DISPATCH_UPCALL_PER_CPU57* is enabled and must be protected by rcu.58* @rcu: RCU callback head for deferred destruction.59* @n_pids: Size of @pids array.60* @pids: Array storing the Netlink socket PIDs indexed by CPU ID for packets61* that miss the flow table.62*/63struct dp_nlsk_pids {64struct rcu_head rcu;65u32 n_pids;66u32 pids[];67};6869/**70* struct datapath - datapath for flow-based packet switching71* @rcu: RCU callback head for deferred destruction.72* @list_node: Element in global 'dps' list.73* @table: flow table.74* @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by75* ovs_mutex and RCU.76* @stats_percpu: Per-CPU datapath statistics.77* @net: Reference to net namespace.78* @user_features: Bitmap of enabled %OVS_DP_F_* features.79* @max_headroom: The maximum headroom of all vports in this datapath; it will80* be used by all the internal vports in this dp.81* @meter_tbl: Meter table.82* @upcall_portids: RCU protected 'struct dp_nlsk_pids'.83*84* Context: See the comment on locking at the top of datapath.c for additional85* locking information.86*/87struct datapath {88struct rcu_head rcu;89struct list_head list_node;9091/* Flow table. */92struct flow_table table;9394/* Switch ports. */95struct hlist_head *ports;9697/* Stats. */98struct dp_stats_percpu __percpu *stats_percpu;99100/* Network namespace ref. */101possible_net_t net;102103u32 user_features;104105u32 max_headroom;106107/* Switch meters. */108struct dp_meter_table meter_tbl;109110struct dp_nlsk_pids __rcu *upcall_portids;111};112113/**114* struct ovs_skb_cb - OVS data in skb CB115* @input_vport: The original vport packet came in on. This value is cached116* when a packet is received by OVS.117* @mru: The maximum received fragement size; 0 if the packet is not118* fragmented.119* @acts_origlen: The netlink size of the flow actions applied to this skb.120* @cutlen: The number of bytes from the packet end to be removed.121* @probability: The sampling probability that was applied to this skb; 0 means122* no sampling has occurred; U32_MAX means 100% probability.123* @upcall_pid: Netlink socket PID to use for sending this packet to userspace;124* 0 means "not set" and default per-CPU or per-vport dispatch should be used.125*/126struct ovs_skb_cb {127struct vport *input_vport;128u16 mru;129u16 acts_origlen;130u32 cutlen;131u32 probability;132u32 upcall_pid;133};134#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)135136/**137* struct dp_upcall_info - metadata to include with a packet sent to userspace138* @cmd: One of %OVS_PACKET_CMD_*.139* @userdata: If nonnull, its variable-length value is passed to userspace as140* %OVS_PACKET_ATTR_USERDATA.141* @actions: If nonnull, its variable-length value is passed to userspace as142* %OVS_PACKET_ATTR_ACTIONS.143* @actions_len: The length of the @actions.144* @portid: Netlink portid to which packet should be sent. If @portid is 0145* then no packet is sent and the packet is accounted in the datapath's @n_lost146* counter.147* @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.148* @mru: If not zero, Maximum received IP fragment size.149*/150struct dp_upcall_info {151struct ip_tunnel_info *egress_tun_info;152const struct nlattr *userdata;153const struct nlattr *actions;154int actions_len;155u32 portid;156u8 cmd;157u16 mru;158};159160/**161* struct ovs_net - Per net-namespace data for ovs.162* @dps: List of datapaths to enable dumping them all out.163* Protected by genl_mutex.164* @dp_notify_work: A work notifier to handle port unregistering.165* @masks_rebalance: A work to periodically optimize flow table caches.166* @ct_limit_info: A hash table of conntrack zone connection limits.167* @xt_label: Whether connlables are configured for the network or not.168*/169struct ovs_net {170struct list_head dps;171struct work_struct dp_notify_work;172struct delayed_work masks_rebalance;173#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)174struct ovs_ct_limit_info *ct_limit_info;175#endif176bool xt_label;177};178179#define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN)180struct ovs_frag_data {181unsigned long dst;182struct vport *vport;183struct ovs_skb_cb cb;184__be16 inner_protocol;185u16 network_offset; /* valid only for MPLS */186u16 vlan_tci;187__be16 vlan_proto;188unsigned int l2_len;189u8 mac_proto;190u8 l2_data[MAX_L2_LEN];191};192193struct deferred_action {194struct sk_buff *skb;195const struct nlattr *actions;196int actions_len;197198/* Store pkt_key clone when creating deferred action. */199struct sw_flow_key pkt_key;200};201202#define DEFERRED_ACTION_FIFO_SIZE 10203#define OVS_RECURSION_LIMIT 5204#define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2)205206struct action_fifo {207int head;208int tail;209/* Deferred action fifo queue storage. */210struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE];211};212213struct action_flow_keys {214struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD];215};216217struct ovs_pcpu_storage {218struct action_fifo action_fifos;219struct action_flow_keys flow_keys;220struct ovs_frag_data frag_data;221int exec_level;222struct task_struct *owner;223local_lock_t bh_lock;224};225226extern struct ovs_pcpu_storage __percpu *ovs_pcpu_storage;227228/**229* enum ovs_pkt_hash_types - hash info to include with a packet230* to send to userspace.231* @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.232* @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash233* over transport ports.234*/235enum ovs_pkt_hash_types {236OVS_PACKET_HASH_SW_BIT = (1ULL << 32),237OVS_PACKET_HASH_L4_BIT = (1ULL << 33),238};239240extern unsigned int ovs_net_id;241void ovs_lock(void);242void ovs_unlock(void);243244#ifdef CONFIG_LOCKDEP245int lockdep_ovsl_is_held(void);246#else247#define lockdep_ovsl_is_held() 1248#endif249250#define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held())251#define ovsl_dereference(p) \252rcu_dereference_protected(p, lockdep_ovsl_is_held())253#define rcu_dereference_ovsl(p) \254rcu_dereference_check(p, lockdep_ovsl_is_held())255256static inline struct net *ovs_dp_get_net(const struct datapath *dp)257{258return read_pnet(&dp->net);259}260261static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)262{263write_pnet(&dp->net, net);264}265266struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);267268static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)269{270WARN_ON_ONCE(!rcu_read_lock_held());271return ovs_lookup_vport(dp, port_no);272}273274static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)275{276WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());277return ovs_lookup_vport(dp, port_no);278}279280static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)281{282ASSERT_OVSL();283return ovs_lookup_vport(dp, port_no);284}285286/* Must be called with rcu_read_lock. */287static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)288{289struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);290291if (dev) {292struct vport *vport = ovs_internal_dev_get_vport(dev);293294if (vport)295return vport->dp;296}297298return NULL;299}300301/* The caller must hold either ovs_mutex or rcu_read_lock to keep the302* returned dp pointer valid.303*/304static inline struct datapath *get_dp(struct net *net, int dp_ifindex)305{306struct datapath *dp;307308WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());309rcu_read_lock();310dp = get_dp_rcu(net, dp_ifindex);311rcu_read_unlock();312313return dp;314}315316extern struct notifier_block ovs_dp_device_notifier;317extern struct genl_family dp_vport_genl_family;318319void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);320void ovs_dp_detach_port(struct vport *);321int ovs_dp_upcall(struct datapath *, struct sk_buff *,322const struct sw_flow_key *, const struct dp_upcall_info *,323uint32_t cutlen);324325u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id);326327const char *ovs_dp_name(const struct datapath *dp);328struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,329u32 portid, u32 seq, u8 cmd);330331int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,332const struct sw_flow_actions *, struct sw_flow_key *);333334void ovs_dp_notify_wq(struct work_struct *work);335336/* 'KEY' must not have any bits set outside of the 'MASK' */337#define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))338#define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))339340#define OVS_NLERR(logging_allowed, fmt, ...) \341do { \342if (logging_allowed && net_ratelimit()) \343pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \344} while (0)345#endif /* datapath.h */346347348