| 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 from the packet end to be removed. |
| 122 | * @probability: The sampling probability that was applied to this skb; 0 means |
| 123 | * no sampling has occurred; U32_MAX means 100% probability. |
| 124 | */ |
| 125 | struct ovs_skb_cb { |
| 126 | struct vport *input_vport; |
| 127 | u16 mru; |
| 128 | u16 acts_origlen; |
| 129 | u32 cutlen; |
| 130 | u32 probability; |
| 131 | }; |
| 132 | #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb) |
| 133 | |
| 134 | /** |
| 135 | * struct dp_upcall_info - metadata to include with a packet sent to userspace |
| 136 | * @cmd: One of %OVS_PACKET_CMD_*. |
| 137 | * @userdata: If nonnull, its variable-length value is passed to userspace as |
| 138 | * %OVS_PACKET_ATTR_USERDATA. |
| 139 | * @actions: If nonnull, its variable-length value is passed to userspace as |
| 140 | * %OVS_PACKET_ATTR_ACTIONS. |
| 141 | * @actions_len: The length of the @actions. |
| 142 | * @portid: Netlink portid to which packet should be sent. If @portid is 0 |
| 143 | * then no packet is sent and the packet is accounted in the datapath's @n_lost |
| 144 | * counter. |
| 145 | * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY. |
| 146 | * @mru: If not zero, Maximum received IP fragment size. |
| 147 | */ |
| 148 | struct dp_upcall_info { |
| 149 | struct ip_tunnel_info *egress_tun_info; |
| 150 | const struct nlattr *userdata; |
| 151 | const struct nlattr *actions; |
| 152 | int actions_len; |
| 153 | u32 portid; |
| 154 | u8 cmd; |
| 155 | u16 mru; |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * struct ovs_net - Per net-namespace data for ovs. |
| 160 | * @dps: List of datapaths to enable dumping them all out. |
| 161 | * Protected by genl_mutex. |
| 162 | * @dp_notify_work: A work notifier to handle port unregistering. |
| 163 | * @masks_rebalance: A work to periodically optimize flow table caches. |
| 164 | * @ct_limit_info: A hash table of conntrack zone connection limits. |
| 165 | * @xt_label: Whether connlables are configured for the network or not. |
| 166 | */ |
| 167 | struct ovs_net { |
| 168 | struct list_head dps; |
| 169 | struct work_struct dp_notify_work; |
| 170 | struct delayed_work masks_rebalance; |
| 171 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
| 172 | struct ovs_ct_limit_info *ct_limit_info; |
| 173 | #endif |
| 174 | bool xt_label; |
| 175 | }; |
| 176 | |
| 177 | #define MAX_L2_LEN (VLAN_ETH_HLEN + 3 * MPLS_HLEN) |
| 178 | struct ovs_frag_data { |
| 179 | unsigned long dst; |
| 180 | struct vport *vport; |
| 181 | struct ovs_skb_cb cb; |
| 182 | __be16 inner_protocol; |
| 183 | u16 network_offset; /* valid only for MPLS */ |
| 184 | u16 vlan_tci; |
| 185 | __be16 vlan_proto; |
| 186 | unsigned int l2_len; |
| 187 | u8 mac_proto; |
| 188 | u8 l2_data[MAX_L2_LEN]; |
| 189 | }; |
| 190 | |
| 191 | struct deferred_action { |
| 192 | struct sk_buff *skb; |
| 193 | const struct nlattr *actions; |
| 194 | int actions_len; |
| 195 | |
| 196 | /* Store pkt_key clone when creating deferred action. */ |
| 197 | struct sw_flow_key pkt_key; |
| 198 | }; |
| 199 | |
| 200 | #define DEFERRED_ACTION_FIFO_SIZE 10 |
| 201 | #define OVS_RECURSION_LIMIT 5 |
| 202 | #define OVS_DEFERRED_ACTION_THRESHOLD (OVS_RECURSION_LIMIT - 2) |
| 203 | |
| 204 | struct action_fifo { |
| 205 | int head; |
| 206 | int tail; |
| 207 | /* Deferred action fifo queue storage. */ |
| 208 | struct deferred_action fifo[DEFERRED_ACTION_FIFO_SIZE]; |
| 209 | }; |
| 210 | |
| 211 | struct action_flow_keys { |
| 212 | struct sw_flow_key key[OVS_DEFERRED_ACTION_THRESHOLD]; |
| 213 | }; |
| 214 | |
| 215 | struct ovs_pcpu_storage { |
| 216 | struct action_fifo action_fifos; |
| 217 | struct action_flow_keys flow_keys; |
| 218 | struct ovs_frag_data frag_data; |
| 219 | int exec_level; |
| 220 | struct task_struct *owner; |
| 221 | local_lock_t bh_lock; |
| 222 | }; |
| 223 | |
| 224 | extern struct ovs_pcpu_storage __percpu *ovs_pcpu_storage; |
| 225 | |
| 226 | /** |
| 227 | * enum ovs_pkt_hash_types - hash info to include with a packet |
| 228 | * to send to userspace. |
| 229 | * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack. |
| 230 | * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash |
| 231 | * over transport ports. |
| 232 | */ |
| 233 | enum ovs_pkt_hash_types { |
| 234 | OVS_PACKET_HASH_SW_BIT = (1ULL << 32), |
| 235 | OVS_PACKET_HASH_L4_BIT = (1ULL << 33), |
| 236 | }; |
| 237 | |
| 238 | extern unsigned int ovs_net_id; |
| 239 | void ovs_lock(void); |
| 240 | void ovs_unlock(void); |
| 241 | |
| 242 | #ifdef CONFIG_LOCKDEP |
| 243 | int lockdep_ovsl_is_held(void); |
| 244 | #else |
| 245 | #define lockdep_ovsl_is_held() 1 |
| 246 | #endif |
| 247 | |
| 248 | #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held()) |
| 249 | #define ovsl_dereference(p) \ |
| 250 | rcu_dereference_protected(p, lockdep_ovsl_is_held()) |
| 251 | #define rcu_dereference_ovsl(p) \ |
| 252 | rcu_dereference_check(p, lockdep_ovsl_is_held()) |
| 253 | |
| 254 | static inline struct net *ovs_dp_get_net(const struct datapath *dp) |
| 255 | { |
| 256 | return read_pnet(&dp->net); |
| 257 | } |
| 258 | |
| 259 | static inline void ovs_dp_set_net(struct datapath *dp, struct net *net) |
| 260 | { |
| 261 | write_pnet(&dp->net, net); |
| 262 | } |
| 263 | |
| 264 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no); |
| 265 | |
| 266 | static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no) |
| 267 | { |
| 268 | WARN_ON_ONCE(!rcu_read_lock_held()); |
| 269 | return ovs_lookup_vport(dp, port_no); |
| 270 | } |
| 271 | |
| 272 | static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no) |
| 273 | { |
| 274 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); |
| 275 | return ovs_lookup_vport(dp, port_no); |
| 276 | } |
| 277 | |
| 278 | static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no) |
| 279 | { |
| 280 | ASSERT_OVSL(); |
| 281 | return ovs_lookup_vport(dp, port_no); |
| 282 | } |
| 283 | |
| 284 | /* Must be called with rcu_read_lock. */ |
| 285 | static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex) |
| 286 | { |
| 287 | struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex); |
| 288 | |
| 289 | if (dev) { |
| 290 | struct vport *vport = ovs_internal_dev_get_vport(dev); |
| 291 | |
| 292 | if (vport) |
| 293 | return vport->dp; |
| 294 | } |
| 295 | |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | /* The caller must hold either ovs_mutex or rcu_read_lock to keep the |
| 300 | * returned dp pointer valid. |
| 301 | */ |
| 302 | static inline struct datapath *get_dp(struct net *net, int dp_ifindex) |
| 303 | { |
| 304 | struct datapath *dp; |
| 305 | |
| 306 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); |
| 307 | rcu_read_lock(); |
| 308 | dp = get_dp_rcu(net, dp_ifindex); |
| 309 | rcu_read_unlock(); |
| 310 | |
| 311 | return dp; |
| 312 | } |
| 313 | |
| 314 | extern struct notifier_block ovs_dp_device_notifier; |
| 315 | extern struct genl_family dp_vport_genl_family; |
| 316 | |
| 317 | void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key); |
| 318 | void ovs_dp_detach_port(struct vport *); |
| 319 | int ovs_dp_upcall(struct datapath *, struct sk_buff *, |
| 320 | const struct sw_flow_key *, const struct dp_upcall_info *, |
| 321 | uint32_t cutlen); |
| 322 | |
| 323 | u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id); |
| 324 | |
| 325 | const char *ovs_dp_name(const struct datapath *dp); |
| 326 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net, |
| 327 | u32 portid, u32 seq, u8 cmd); |
| 328 | |
| 329 | int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, |
| 330 | const struct sw_flow_actions *, struct sw_flow_key *); |
| 331 | |
| 332 | void ovs_dp_notify_wq(struct work_struct *work); |
| 333 | |
| 334 | /* 'KEY' must not have any bits set outside of the 'MASK' */ |
| 335 | #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK))) |
| 336 | #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK)) |
| 337 | |
| 338 | #define OVS_NLERR(logging_allowed, fmt, ...) \ |
| 339 | do { \ |
| 340 | if (logging_allowed && net_ratelimit()) \ |
| 341 | pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \ |
| 342 | } while (0) |
| 343 | #endif /* datapath.h */ |