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 #include <net/mpls.h> 17 18 #include "conntrack.h" 19 #include "flow.h" 20 #include "flow_table.h" 21 #include "meter.h" 22 #include "vport-internal_dev.h" 23 24 #define DP_MAX_PORTS USHRT_MAX 25 #define DP_VPORT_HASH_BUCKETS 1024 26 #define DP_MASKS_REBALANCE_INTERVAL 4000 27 28 /** 29 * struct dp_stats_percpu - per-cpu packet processing statistics for a given 30 * datapath. 31 * @n_hit: Number of received packets for which a matching flow was found in 32 * the flow table. 33 * @n_missed: Number of received packets that had no matching flow in the flow 34 * table. The sum of @n_hit and @n_missed is the number of packets that have 35 * been received by the datapath. 36 * @n_lost: Number of received packets that had no matching flow in the flow 37 * table that could not be sent to userspace (normally due to an overflow in 38 * one of the datapath's queues). 39 * @n_mask_hit: Number of masks looked up for flow match. 40 * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked 41 * up per packet. 42 * @n_cache_hit: The number of received packets that had their mask found using 43 * the mask cache. 44 * @syncp: Synchronization point for 64bit counters. 45 */ 46 struct dp_stats_percpu { 47 u64 n_hit; 48 u64 n_missed; 49 u64 n_lost; 50 u64 n_mask_hit; 51 u64 n_cache_hit; 52 struct u64_stats_sync syncp; 53 }; 54 55 /** 56 * struct dp_nlsk_pids - array of netlink portids of for a datapath. 57 * This is used when OVS_DP_F_DISPATCH_UPCALL_PER_CPU 58 * is enabled and must be protected by rcu. 59 * @rcu: RCU callback head for deferred destruction. 60 * @n_pids: Size of @pids array. 61 * @pids: Array storing the Netlink socket PIDs indexed by CPU ID for packets 62 * that miss the flow table. 63 */ 64 struct dp_nlsk_pids { 65 struct rcu_head rcu; 66 u32 n_pids; 67 u32 pids[]; 68 }; 69 70 /** 71 * struct datapath - datapath for flow-based packet switching 72 * @rcu: RCU callback head for deferred destruction. 73 * @list_node: Element in global 'dps' list. 74 * @table: flow table. 75 * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by 76 * ovs_mutex and RCU. 77 * @stats_percpu: Per-CPU datapath statistics. 78 * @net: Reference to net namespace. 79 * @user_features: Bitmap of enabled %OVS_DP_F_* features. 80 * @max_headroom: The maximum headroom of all vports in this datapath; it will 81 * be used by all the internal vports in this dp. 82 * @meter_tbl: Meter table. 83 * @upcall_portids: RCU protected 'struct dp_nlsk_pids'. 84 * 85 * Context: See the comment on locking at the top of datapath.c for additional 86 * locking information. 87 */ 88 struct datapath { 89 struct rcu_head rcu; 90 struct list_head list_node; 91 92 /* Flow table. */ 93 struct flow_table table; 94 95 /* Switch ports. */ 96 struct hlist_head *ports; 97 98 /* Stats. */ 99 struct dp_stats_percpu __percpu *stats_percpu; 100 101 /* Network namespace ref. */ 102 possible_net_t net; 103 104 u32 user_features; 105 106 u32 max_headroom; 107 108 /* Switch meters. */ 109 struct dp_meter_table meter_tbl; 110 111 struct dp_nlsk_pids __rcu *upcall_portids; 112 }; 113 114 /** 115 * struct ovs_skb_cb - OVS data in skb CB 116 * @input_vport: The original vport packet came in on. This value is cached 117 * when a packet is received by OVS. 118 * @mru: The maximum received fragement size; 0 if the packet is not 119 * fragmented. 120 * @acts_origlen: The netlink size of the flow actions applied to this skb. 121 * @cutlen: The number of bytes in the packet to preserve on output. 122 * @probability: The sampling probability that was applied to this skb; 0 means 123 * no sampling has occurred; U32_MAX means 100% probability. 124 * @upcall_pid: Netlink socket PID to use for sending this packet to userspace; 125 * 0 means "not set" and default per-CPU or per-vport dispatch should be used. 126 */ 127 struct ovs_skb_cb { 128 struct vport *input_vport; 129 u16 mru; 130 u16 acts_origlen; 131 u32 cutlen; 132 u32 probability; 133 u32 upcall_pid; 134 }; 135 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb) 136 137 /** 138 * struct dp_upcall_info - metadata to include with a packet sent to userspace 139 * @cmd: One of %OVS_PACKET_CMD_*. 140 * @userdata: If nonnull, its variable-length value is passed to userspace as 141 * %OVS_PACKET_ATTR_USERDATA. 142 * @actions: If nonnull, its variable-length value is passed to userspace as 143 * %OVS_PACKET_ATTR_ACTIONS. 144 * @actions_len: The length of the @actions. 145 * @portid: Netlink portid to which packet should be sent. If @portid is 0 146 * then no packet is sent and the packet is accounted in the datapath's @n_lost 147 * counter. 148 * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY. 149 * @mru: If not zero, Maximum received IP fragment size. 150 */ 151 struct dp_upcall_info { 152 struct ip_tunnel_info *egress_tun_info; 153 const struct nlattr *userdata; 154 const struct nlattr *actions; 155 int actions_len; 156 u32 portid; 157 u8 cmd; 158 u16 mru; 159 }; 160 161 /** 162 * struct ovs_net - Per net-namespace data for ovs. 163 * @dps: List of datapaths to enable dumping them all out. 164 * Protected by genl_mutex. 165 * @dp_notify_work: A work notifier to handle port unregistering. 166 * @masks_rebalance: A work to periodically optimize flow table caches. 167 * @ct_limit_info: Hash table of conntrack zone connection limits. Protected 168 * by RCU; updates and teardown are serialized by ovs_mutex. May be NULL during 169 * netns teardown. 170 * @ct_limit_exit_data: CT limit state detached at .pre_exit, freed at .exit. 171 * @xt_label: Whether connlables are configured for the network or not. 172 */ 173 struct ovs_net { 174 struct list_head dps; 175 struct work_struct dp_notify_work; 176 struct delayed_work masks_rebalance; 177 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) 178 struct ovs_ct_limit_info __rcu *ct_limit_info; 179 struct ovs_ct_limit_info *ct_limit_exit_data; 180 #endif 181 bool xt_label; 182 }; 183 184 #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN) 185 struct ovs_frag_data { 186 unsigned long dst; 187 struct vport *vport; 188 struct ovs_skb_cb cb; 189 __be16 inner_protocol; 190 u16 network_offset; /* valid only for MPLS */ 191 u16 vlan_tci; 192 __be16 vlan_proto; 193 unsigned int l2_len; 194 u8 mac_proto; 195 u8 l2_data[MAX_L2_LEN]; 196 }; 197 198 struct deferred_action { 199 struct sk_buff *skb; 200 const struct nlattr *actions; 201 int actions_len; 202 203 /* Store pkt_key clone when creating deferred action. */ 204 struct sw_flow_key pkt_key; 205 }; 206 207 #define DEFERRED_ACTION_FIFO_SIZE 10 208 #define OVS_RECURSION_LIMIT 5 209 #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2) 210 211 struct action_fifo { 212 int head; 213 int tail; 214 /* Deferred action fifo queue storage. */ 215 struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE]; 216 }; 217 218 struct action_flow_keys { 219 struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD]; 220 }; 221 222 struct ovs_pcpu_storage { 223 struct action_fifo action_fifos; 224 struct action_flow_keys flow_keys; 225 struct ovs_frag_data frag_data; 226 int exec_level; 227 struct task_struct *owner; 228 local_lock_t bh_lock; 229 }; 230 231 extern struct ovs_pcpu_storage __percpu *ovs_pcpu_storage; 232 233 /** 234 * enum ovs_pkt_hash_types - hash info to include with a packet 235 * to send to userspace. 236 * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack. 237 * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash 238 * over transport ports. 239 */ 240 enum ovs_pkt_hash_types { 241 OVS_PACKET_HASH_SW_BIT = (1ULL << 32), 242 OVS_PACKET_HASH_L4_BIT = (1ULL << 33), 243 }; 244 245 extern unsigned int ovs_net_id; 246 void ovs_lock(void); 247 void ovs_unlock(void); 248 249 #ifdef CONFIG_LOCKDEP 250 int lockdep_ovsl_is_held(void); 251 #else 252 #define lockdep_ovsl_is_held() 1 253 #endif 254 255 #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held()) 256 #define ovsl_dereference(p) \ 257 rcu_dereference_protected(p, lockdep_ovsl_is_held()) 258 #define rcu_dereference_ovsl(p) \ 259 rcu_dereference_check(p, lockdep_ovsl_is_held()) 260 261 static inline struct net *ovs_dp_get_net(const struct datapath *dp) 262 { 263 return read_pnet(&dp->net); 264 } 265 266 static inline void ovs_dp_set_net(struct datapath *dp, struct net *net) 267 { 268 write_pnet(&dp->net, net); 269 } 270 271 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no); 272 273 static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no) 274 { 275 WARN_ON_ONCE(!rcu_read_lock_held()); 276 return ovs_lookup_vport(dp, port_no); 277 } 278 279 static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no) 280 { 281 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); 282 return ovs_lookup_vport(dp, port_no); 283 } 284 285 static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no) 286 { 287 ASSERT_OVSL(); 288 return ovs_lookup_vport(dp, port_no); 289 } 290 291 /* Must be called with rcu_read_lock. */ 292 static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex) 293 { 294 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex); 295 296 if (dev) { 297 struct vport *vport = ovs_internal_dev_get_vport(dev); 298 299 if (vport) 300 return vport->dp; 301 } 302 303 return NULL; 304 } 305 306 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the 307 * returned dp pointer valid. 308 */ 309 static inline struct datapath *get_dp(struct net *net, int dp_ifindex) 310 { 311 struct datapath *dp; 312 313 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); 314 rcu_read_lock(); 315 dp = get_dp_rcu(net, dp_ifindex); 316 rcu_read_unlock(); 317 318 return dp; 319 } 320 321 extern struct notifier_block ovs_dp_device_notifier; 322 extern struct genl_family dp_vport_genl_family; 323 324 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key); 325 void ovs_dp_detach_port(struct vport *); 326 int ovs_dp_upcall(struct datapath *, struct sk_buff *, 327 const struct sw_flow_key *, const struct dp_upcall_info *, 328 uint32_t cutlen); 329 330 u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id); 331 332 const char *ovs_dp_name(const struct datapath *dp); 333 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, 334 u32 portid, u32 seq, u8 cmd); 335 336 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, 337 const struct sw_flow_actions *, struct sw_flow_key *); 338 339 void ovs_dp_notify_wq(struct work_struct *work); 340 341 /* 'KEY' must not have any bits set outside of the 'MASK' */ 342 #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK))) 343 #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK)) 344 345 #define OVS_NLERR(logging_allowed, fmt, ...) \ 346 do { \ 347 if (logging_allowed && net_ratelimit()) \ 348 pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \ 349 } while (0) 350 #endif /* datapath.h */ 351