1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (c) 2007-2014 Nicira, Inc. 4 */ 5 6 #ifndef DATAPATH_H 7 #define DATAPATH_H 1 8 9 #include <asm/page.h> 10 #include <linux/kernel.h> 11 #include <linux/mutex.h> 12 #include <linux/netdevice.h> 13 #include <linux/skbuff.h> 14 #include <linux/u64_stats_sync.h> 15 #include <net/ip_tunnels.h> 16 17 #include "conntrack.h" 18 #include "flow.h" 19 #include "flow_table.h" 20 #include "meter.h" 21 #include "vport-internal_dev.h" 22 23 #define DP_MAX_PORTS USHRT_MAX 24 #define DP_VPORT_HASH_BUCKETS 1024 25 #define DP_MASKS_REBALANCE_INTERVAL 4000 26 27 /** 28 * struct dp_stats_percpu - per-cpu packet processing statistics for a given 29 * datapath. 30 * @n_hit: Number of received packets for which a matching flow was found in 31 * the flow table. 32 * @n_miss: Number of received packets that had no matching flow in the flow 33 * table. The sum of @n_hit and @n_miss is the number of packets that have 34 * been received by the datapath. 35 * @n_lost: Number of received packets that had no matching flow in the flow 36 * table that could not be sent to userspace (normally due to an overflow in 37 * 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 looked 40 * up per packet. 41 */ 42 struct dp_stats_percpu { 43 u64 n_hit; 44 u64 n_missed; 45 u64 n_lost; 46 u64 n_mask_hit; 47 struct u64_stats_sync syncp; 48 }; 49 50 /** 51 * struct datapath - datapath for flow-based packet switching 52 * @rcu: RCU callback head for deferred destruction. 53 * @list_node: Element in global 'dps' list. 54 * @table: flow table. 55 * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by 56 * ovs_mutex and RCU. 57 * @stats_percpu: Per-CPU datapath statistics. 58 * @net: Reference to net namespace. 59 * @max_headroom: the maximum headroom of all vports in this datapath; it will 60 * be used by all the internal vports in this dp. 61 * 62 * Context: See the comment on locking at the top of datapath.c for additional 63 * locking information. 64 */ 65 struct datapath { 66 struct rcu_head rcu; 67 struct list_head list_node; 68 69 /* Flow table. */ 70 struct flow_table table; 71 72 /* Switch ports. */ 73 struct hlist_head *ports; 74 75 /* Stats. */ 76 struct dp_stats_percpu __percpu *stats_percpu; 77 78 /* Network namespace ref. */ 79 possible_net_t net; 80 81 u32 user_features; 82 83 u32 max_headroom; 84 85 /* Switch meters. */ 86 struct dp_meter_table meter_tbl; 87 }; 88 89 /** 90 * struct ovs_skb_cb - OVS data in skb CB 91 * @input_vport: The original vport packet came in on. This value is cached 92 * when a packet is received by OVS. 93 * @mru: The maximum received fragement size; 0 if the packet is not 94 * fragmented. 95 * @acts_origlen: The netlink size of the flow actions applied to this skb. 96 * @cutlen: The number of bytes from the packet end to be removed. 97 */ 98 struct ovs_skb_cb { 99 struct vport *input_vport; 100 u16 mru; 101 u16 acts_origlen; 102 u32 cutlen; 103 }; 104 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb) 105 106 /** 107 * struct dp_upcall - metadata to include with a packet to send to userspace 108 * @cmd: One of %OVS_PACKET_CMD_*. 109 * @userdata: If nonnull, its variable-length value is passed to userspace as 110 * %OVS_PACKET_ATTR_USERDATA. 111 * @portid: Netlink portid to which packet should be sent. If @portid is 0 112 * then no packet is sent and the packet is accounted in the datapath's @n_lost 113 * counter. 114 * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY. 115 * @mru: If not zero, Maximum received IP fragment size. 116 */ 117 struct dp_upcall_info { 118 struct ip_tunnel_info *egress_tun_info; 119 const struct nlattr *userdata; 120 const struct nlattr *actions; 121 int actions_len; 122 u32 portid; 123 u8 cmd; 124 u16 mru; 125 }; 126 127 /** 128 * struct ovs_net - Per net-namespace data for ovs. 129 * @dps: List of datapaths to enable dumping them all out. 130 * Protected by genl_mutex. 131 */ 132 struct ovs_net { 133 struct list_head dps; 134 struct work_struct dp_notify_work; 135 struct delayed_work masks_rebalance; 136 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 137 struct ovs_ct_limit_info *ct_limit_info; 138 #endif 139 140 /* Module reference for configuring conntrack. */ 141 bool xt_label; 142 }; 143 144 /** 145 * enum ovs_pkt_hash_types - hash info to include with a packet 146 * to send to userspace. 147 * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack. 148 * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash 149 * over transport ports. 150 */ 151 enum ovs_pkt_hash_types { 152 OVS_PACKET_HASH_SW_BIT = (1ULL << 32), 153 OVS_PACKET_HASH_L4_BIT = (1ULL << 33), 154 }; 155 156 extern unsigned int ovs_net_id; 157 void ovs_lock(void); 158 void ovs_unlock(void); 159 160 #ifdef CONFIG_LOCKDEP 161 int lockdep_ovsl_is_held(void); 162 #else 163 #define lockdep_ovsl_is_held() 1 164 #endif 165 166 #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held()) 167 #define ovsl_dereference(p) \ 168 rcu_dereference_protected(p, lockdep_ovsl_is_held()) 169 #define rcu_dereference_ovsl(p) \ 170 rcu_dereference_check(p, lockdep_ovsl_is_held()) 171 172 static inline struct net *ovs_dp_get_net(const struct datapath *dp) 173 { 174 return read_pnet(&dp->net); 175 } 176 177 static inline void ovs_dp_set_net(struct datapath *dp, struct net *net) 178 { 179 write_pnet(&dp->net, net); 180 } 181 182 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no); 183 184 static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no) 185 { 186 WARN_ON_ONCE(!rcu_read_lock_held()); 187 return ovs_lookup_vport(dp, port_no); 188 } 189 190 static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no) 191 { 192 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); 193 return ovs_lookup_vport(dp, port_no); 194 } 195 196 static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no) 197 { 198 ASSERT_OVSL(); 199 return ovs_lookup_vport(dp, port_no); 200 } 201 202 /* Must be called with rcu_read_lock. */ 203 static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex) 204 { 205 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex); 206 207 if (dev) { 208 struct vport *vport = ovs_internal_dev_get_vport(dev); 209 210 if (vport) 211 return vport->dp; 212 } 213 214 return NULL; 215 } 216 217 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the 218 * returned dp pointer valid. 219 */ 220 static inline struct datapath *get_dp(struct net *net, int dp_ifindex) 221 { 222 struct datapath *dp; 223 224 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); 225 rcu_read_lock(); 226 dp = get_dp_rcu(net, dp_ifindex); 227 rcu_read_unlock(); 228 229 return dp; 230 } 231 232 extern struct notifier_block ovs_dp_device_notifier; 233 extern struct genl_family dp_vport_genl_family; 234 235 DECLARE_STATIC_KEY_FALSE(tc_recirc_sharing_support); 236 237 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key); 238 void ovs_dp_detach_port(struct vport *); 239 int ovs_dp_upcall(struct datapath *, struct sk_buff *, 240 const struct sw_flow_key *, const struct dp_upcall_info *, 241 uint32_t cutlen); 242 243 const char *ovs_dp_name(const struct datapath *dp); 244 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, 245 u32 portid, u32 seq, u8 cmd); 246 247 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, 248 const struct sw_flow_actions *, struct sw_flow_key *); 249 250 void ovs_dp_notify_wq(struct work_struct *work); 251 252 int action_fifos_init(void); 253 void action_fifos_exit(void); 254 255 /* 'KEY' must not have any bits set outside of the 'MASK' */ 256 #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK))) 257 #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK)) 258 259 #define OVS_NLERR(logging_allowed, fmt, ...) \ 260 do { \ 261 if (logging_allowed && net_ratelimit()) \ 262 pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \ 263 } while (0) 264 #endif /* datapath.h */ 265