162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2007-2017 Nicira, Inc. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#ifndef FLOW_H 762306a36Sopenharmony_ci#define FLOW_H 1 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/cache.h> 1062306a36Sopenharmony_ci#include <linux/kernel.h> 1162306a36Sopenharmony_ci#include <linux/netlink.h> 1262306a36Sopenharmony_ci#include <linux/openvswitch.h> 1362306a36Sopenharmony_ci#include <linux/spinlock.h> 1462306a36Sopenharmony_ci#include <linux/types.h> 1562306a36Sopenharmony_ci#include <linux/rcupdate.h> 1662306a36Sopenharmony_ci#include <linux/if_ether.h> 1762306a36Sopenharmony_ci#include <linux/in6.h> 1862306a36Sopenharmony_ci#include <linux/jiffies.h> 1962306a36Sopenharmony_ci#include <linux/time.h> 2062306a36Sopenharmony_ci#include <linux/cpumask.h> 2162306a36Sopenharmony_ci#include <net/inet_ecn.h> 2262306a36Sopenharmony_ci#include <net/ip_tunnels.h> 2362306a36Sopenharmony_ci#include <net/dst_metadata.h> 2462306a36Sopenharmony_ci#include <net/nsh.h> 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_cistruct sk_buff; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_cienum sw_flow_mac_proto { 2962306a36Sopenharmony_ci MAC_PROTO_NONE = 0, 3062306a36Sopenharmony_ci MAC_PROTO_ETHERNET, 3162306a36Sopenharmony_ci}; 3262306a36Sopenharmony_ci#define SW_FLOW_KEY_INVALID 0x80 3362306a36Sopenharmony_ci#define MPLS_LABEL_DEPTH 3 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* Bit definitions for IPv6 Extension Header pseudo-field. */ 3662306a36Sopenharmony_cienum ofp12_ipv6exthdr_flags { 3762306a36Sopenharmony_ci OFPIEH12_NONEXT = 1 << 0, /* "No next header" encountered. */ 3862306a36Sopenharmony_ci OFPIEH12_ESP = 1 << 1, /* Encrypted Sec Payload header present. */ 3962306a36Sopenharmony_ci OFPIEH12_AUTH = 1 << 2, /* Authentication header present. */ 4062306a36Sopenharmony_ci OFPIEH12_DEST = 1 << 3, /* 1 or 2 dest headers present. */ 4162306a36Sopenharmony_ci OFPIEH12_FRAG = 1 << 4, /* Fragment header present. */ 4262306a36Sopenharmony_ci OFPIEH12_ROUTER = 1 << 5, /* Router header present. */ 4362306a36Sopenharmony_ci OFPIEH12_HOP = 1 << 6, /* Hop-by-hop header present. */ 4462306a36Sopenharmony_ci OFPIEH12_UNREP = 1 << 7, /* Unexpected repeats encountered. */ 4562306a36Sopenharmony_ci OFPIEH12_UNSEQ = 1 << 8 /* Unexpected sequencing encountered. */ 4662306a36Sopenharmony_ci}; 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci/* Store options at the end of the array if they are less than the 4962306a36Sopenharmony_ci * maximum size. This allows us to get the benefits of variable length 5062306a36Sopenharmony_ci * matching for small options. 5162306a36Sopenharmony_ci */ 5262306a36Sopenharmony_ci#define TUN_METADATA_OFFSET(opt_len) \ 5362306a36Sopenharmony_ci (sizeof_field(struct sw_flow_key, tun_opts) - opt_len) 5462306a36Sopenharmony_ci#define TUN_METADATA_OPTS(flow_key, opt_len) \ 5562306a36Sopenharmony_ci ((void *)((flow_key)->tun_opts + TUN_METADATA_OFFSET(opt_len))) 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistruct ovs_tunnel_info { 5862306a36Sopenharmony_ci struct metadata_dst *tun_dst; 5962306a36Sopenharmony_ci}; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_cistruct vlan_head { 6262306a36Sopenharmony_ci __be16 tpid; /* Vlan type. Generally 802.1q or 802.1ad.*/ 6362306a36Sopenharmony_ci __be16 tci; /* 0 if no VLAN, VLAN_CFI_MASK set otherwise. */ 6462306a36Sopenharmony_ci}; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci#define OVS_SW_FLOW_KEY_METADATA_SIZE \ 6762306a36Sopenharmony_ci (offsetof(struct sw_flow_key, recirc_id) + \ 6862306a36Sopenharmony_ci sizeof_field(struct sw_flow_key, recirc_id)) 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistruct ovs_key_nsh { 7162306a36Sopenharmony_ci struct ovs_nsh_key_base base; 7262306a36Sopenharmony_ci __be32 context[NSH_MD1_CONTEXT_SIZE]; 7362306a36Sopenharmony_ci}; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistruct sw_flow_key { 7662306a36Sopenharmony_ci u8 tun_opts[IP_TUNNEL_OPTS_MAX]; 7762306a36Sopenharmony_ci u8 tun_opts_len; 7862306a36Sopenharmony_ci struct ip_tunnel_key tun_key; /* Encapsulating tunnel key. */ 7962306a36Sopenharmony_ci struct { 8062306a36Sopenharmony_ci u32 priority; /* Packet QoS priority. */ 8162306a36Sopenharmony_ci u32 skb_mark; /* SKB mark. */ 8262306a36Sopenharmony_ci u16 in_port; /* Input switch port (or DP_MAX_PORTS). */ 8362306a36Sopenharmony_ci } __packed phy; /* Safe when right after 'tun_key'. */ 8462306a36Sopenharmony_ci u8 mac_proto; /* MAC layer protocol (e.g. Ethernet). */ 8562306a36Sopenharmony_ci u8 tun_proto; /* Protocol of encapsulating tunnel. */ 8662306a36Sopenharmony_ci u32 ovs_flow_hash; /* Datapath computed hash value. */ 8762306a36Sopenharmony_ci u32 recirc_id; /* Recirculation ID. */ 8862306a36Sopenharmony_ci struct { 8962306a36Sopenharmony_ci u8 src[ETH_ALEN]; /* Ethernet source address. */ 9062306a36Sopenharmony_ci u8 dst[ETH_ALEN]; /* Ethernet destination address. */ 9162306a36Sopenharmony_ci struct vlan_head vlan; 9262306a36Sopenharmony_ci struct vlan_head cvlan; 9362306a36Sopenharmony_ci __be16 type; /* Ethernet frame type. */ 9462306a36Sopenharmony_ci } eth; 9562306a36Sopenharmony_ci /* Filling a hole of two bytes. */ 9662306a36Sopenharmony_ci u8 ct_state; 9762306a36Sopenharmony_ci u8 ct_orig_proto; /* CT original direction tuple IP 9862306a36Sopenharmony_ci * protocol. 9962306a36Sopenharmony_ci */ 10062306a36Sopenharmony_ci union { 10162306a36Sopenharmony_ci struct { 10262306a36Sopenharmony_ci u8 proto; /* IP protocol or lower 8 bits of ARP opcode. */ 10362306a36Sopenharmony_ci u8 tos; /* IP ToS. */ 10462306a36Sopenharmony_ci u8 ttl; /* IP TTL/hop limit. */ 10562306a36Sopenharmony_ci u8 frag; /* One of OVS_FRAG_TYPE_*. */ 10662306a36Sopenharmony_ci } ip; 10762306a36Sopenharmony_ci }; 10862306a36Sopenharmony_ci u16 ct_zone; /* Conntrack zone. */ 10962306a36Sopenharmony_ci struct { 11062306a36Sopenharmony_ci __be16 src; /* TCP/UDP/SCTP source port. */ 11162306a36Sopenharmony_ci __be16 dst; /* TCP/UDP/SCTP destination port. */ 11262306a36Sopenharmony_ci __be16 flags; /* TCP flags. */ 11362306a36Sopenharmony_ci } tp; 11462306a36Sopenharmony_ci union { 11562306a36Sopenharmony_ci struct { 11662306a36Sopenharmony_ci struct { 11762306a36Sopenharmony_ci __be32 src; /* IP source address. */ 11862306a36Sopenharmony_ci __be32 dst; /* IP destination address. */ 11962306a36Sopenharmony_ci } addr; 12062306a36Sopenharmony_ci union { 12162306a36Sopenharmony_ci struct { 12262306a36Sopenharmony_ci __be32 src; 12362306a36Sopenharmony_ci __be32 dst; 12462306a36Sopenharmony_ci } ct_orig; /* Conntrack original direction fields. */ 12562306a36Sopenharmony_ci struct { 12662306a36Sopenharmony_ci u8 sha[ETH_ALEN]; /* ARP source hardware address. */ 12762306a36Sopenharmony_ci u8 tha[ETH_ALEN]; /* ARP target hardware address. */ 12862306a36Sopenharmony_ci } arp; 12962306a36Sopenharmony_ci }; 13062306a36Sopenharmony_ci } ipv4; 13162306a36Sopenharmony_ci struct { 13262306a36Sopenharmony_ci struct { 13362306a36Sopenharmony_ci struct in6_addr src; /* IPv6 source address. */ 13462306a36Sopenharmony_ci struct in6_addr dst; /* IPv6 destination address. */ 13562306a36Sopenharmony_ci } addr; 13662306a36Sopenharmony_ci __be32 label; /* IPv6 flow label. */ 13762306a36Sopenharmony_ci u16 exthdrs; /* IPv6 extension header flags */ 13862306a36Sopenharmony_ci union { 13962306a36Sopenharmony_ci struct { 14062306a36Sopenharmony_ci struct in6_addr src; 14162306a36Sopenharmony_ci struct in6_addr dst; 14262306a36Sopenharmony_ci } ct_orig; /* Conntrack original direction fields. */ 14362306a36Sopenharmony_ci struct { 14462306a36Sopenharmony_ci struct in6_addr target; /* ND target address. */ 14562306a36Sopenharmony_ci u8 sll[ETH_ALEN]; /* ND source link layer address. */ 14662306a36Sopenharmony_ci u8 tll[ETH_ALEN]; /* ND target link layer address. */ 14762306a36Sopenharmony_ci } nd; 14862306a36Sopenharmony_ci }; 14962306a36Sopenharmony_ci } ipv6; 15062306a36Sopenharmony_ci struct { 15162306a36Sopenharmony_ci u32 num_labels_mask; /* labels present bitmap of effective length MPLS_LABEL_DEPTH */ 15262306a36Sopenharmony_ci __be32 lse[MPLS_LABEL_DEPTH]; /* label stack entry */ 15362306a36Sopenharmony_ci } mpls; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci struct ovs_key_nsh nsh; /* network service header */ 15662306a36Sopenharmony_ci }; 15762306a36Sopenharmony_ci struct { 15862306a36Sopenharmony_ci /* Connection tracking fields not packed above. */ 15962306a36Sopenharmony_ci struct { 16062306a36Sopenharmony_ci __be16 src; /* CT orig tuple tp src port. */ 16162306a36Sopenharmony_ci __be16 dst; /* CT orig tuple tp dst port. */ 16262306a36Sopenharmony_ci } orig_tp; 16362306a36Sopenharmony_ci u32 mark; 16462306a36Sopenharmony_ci struct ovs_key_ct_labels labels; 16562306a36Sopenharmony_ci } ct; 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci} __aligned(BITS_PER_LONG/8); /* Ensure that we can do comparisons as longs. */ 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_cistatic inline bool sw_flow_key_is_nd(const struct sw_flow_key *key) 17062306a36Sopenharmony_ci{ 17162306a36Sopenharmony_ci return key->eth.type == htons(ETH_P_IPV6) && 17262306a36Sopenharmony_ci key->ip.proto == NEXTHDR_ICMP && 17362306a36Sopenharmony_ci key->tp.dst == 0 && 17462306a36Sopenharmony_ci (key->tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) || 17562306a36Sopenharmony_ci key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)); 17662306a36Sopenharmony_ci} 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_cistruct sw_flow_key_range { 17962306a36Sopenharmony_ci unsigned short int start; 18062306a36Sopenharmony_ci unsigned short int end; 18162306a36Sopenharmony_ci}; 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_cistruct sw_flow_mask { 18462306a36Sopenharmony_ci int ref_count; 18562306a36Sopenharmony_ci struct rcu_head rcu; 18662306a36Sopenharmony_ci struct sw_flow_key_range range; 18762306a36Sopenharmony_ci struct sw_flow_key key; 18862306a36Sopenharmony_ci}; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_cistruct sw_flow_match { 19162306a36Sopenharmony_ci struct sw_flow_key *key; 19262306a36Sopenharmony_ci struct sw_flow_key_range range; 19362306a36Sopenharmony_ci struct sw_flow_mask *mask; 19462306a36Sopenharmony_ci}; 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci#define MAX_UFID_LENGTH 16 /* 128 bits */ 19762306a36Sopenharmony_ci 19862306a36Sopenharmony_cistruct sw_flow_id { 19962306a36Sopenharmony_ci u32 ufid_len; 20062306a36Sopenharmony_ci union { 20162306a36Sopenharmony_ci u32 ufid[MAX_UFID_LENGTH / 4]; 20262306a36Sopenharmony_ci struct sw_flow_key *unmasked_key; 20362306a36Sopenharmony_ci }; 20462306a36Sopenharmony_ci}; 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_cistruct sw_flow_actions { 20762306a36Sopenharmony_ci struct rcu_head rcu; 20862306a36Sopenharmony_ci size_t orig_len; /* From flow_cmd_new netlink actions size */ 20962306a36Sopenharmony_ci u32 actions_len; 21062306a36Sopenharmony_ci struct nlattr actions[]; 21162306a36Sopenharmony_ci}; 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_cistruct sw_flow_stats { 21462306a36Sopenharmony_ci u64 packet_count; /* Number of packets matched. */ 21562306a36Sopenharmony_ci u64 byte_count; /* Number of bytes matched. */ 21662306a36Sopenharmony_ci unsigned long used; /* Last used time (in jiffies). */ 21762306a36Sopenharmony_ci spinlock_t lock; /* Lock for atomic stats update. */ 21862306a36Sopenharmony_ci __be16 tcp_flags; /* Union of seen TCP flags. */ 21962306a36Sopenharmony_ci}; 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_cistruct sw_flow { 22262306a36Sopenharmony_ci struct rcu_head rcu; 22362306a36Sopenharmony_ci struct { 22462306a36Sopenharmony_ci struct hlist_node node[2]; 22562306a36Sopenharmony_ci u32 hash; 22662306a36Sopenharmony_ci } flow_table, ufid_table; 22762306a36Sopenharmony_ci int stats_last_writer; /* CPU id of the last writer on 22862306a36Sopenharmony_ci * 'stats[0]'. 22962306a36Sopenharmony_ci */ 23062306a36Sopenharmony_ci struct sw_flow_key key; 23162306a36Sopenharmony_ci struct sw_flow_id id; 23262306a36Sopenharmony_ci struct cpumask *cpu_used_mask; 23362306a36Sopenharmony_ci struct sw_flow_mask *mask; 23462306a36Sopenharmony_ci struct sw_flow_actions __rcu *sf_acts; 23562306a36Sopenharmony_ci struct sw_flow_stats __rcu *stats[]; /* One for each CPU. First one 23662306a36Sopenharmony_ci * is allocated at flow creation time, 23762306a36Sopenharmony_ci * the rest are allocated on demand 23862306a36Sopenharmony_ci * while holding the 'stats[0].lock'. 23962306a36Sopenharmony_ci */ 24062306a36Sopenharmony_ci}; 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_cistruct arp_eth_header { 24362306a36Sopenharmony_ci __be16 ar_hrd; /* format of hardware address */ 24462306a36Sopenharmony_ci __be16 ar_pro; /* format of protocol address */ 24562306a36Sopenharmony_ci unsigned char ar_hln; /* length of hardware address */ 24662306a36Sopenharmony_ci unsigned char ar_pln; /* length of protocol address */ 24762306a36Sopenharmony_ci __be16 ar_op; /* ARP opcode (command) */ 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci /* Ethernet+IPv4 specific members. */ 25062306a36Sopenharmony_ci unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */ 25162306a36Sopenharmony_ci unsigned char ar_sip[4]; /* sender IP address */ 25262306a36Sopenharmony_ci unsigned char ar_tha[ETH_ALEN]; /* target hardware address */ 25362306a36Sopenharmony_ci unsigned char ar_tip[4]; /* target IP address */ 25462306a36Sopenharmony_ci} __packed; 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_cistatic inline u8 ovs_key_mac_proto(const struct sw_flow_key *key) 25762306a36Sopenharmony_ci{ 25862306a36Sopenharmony_ci return key->mac_proto & ~SW_FLOW_KEY_INVALID; 25962306a36Sopenharmony_ci} 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_cistatic inline u16 __ovs_mac_header_len(u8 mac_proto) 26262306a36Sopenharmony_ci{ 26362306a36Sopenharmony_ci return mac_proto == MAC_PROTO_ETHERNET ? ETH_HLEN : 0; 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_cistatic inline u16 ovs_mac_header_len(const struct sw_flow_key *key) 26762306a36Sopenharmony_ci{ 26862306a36Sopenharmony_ci return __ovs_mac_header_len(ovs_key_mac_proto(key)); 26962306a36Sopenharmony_ci} 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_cistatic inline bool ovs_identifier_is_ufid(const struct sw_flow_id *sfid) 27262306a36Sopenharmony_ci{ 27362306a36Sopenharmony_ci return sfid->ufid_len; 27462306a36Sopenharmony_ci} 27562306a36Sopenharmony_ci 27662306a36Sopenharmony_cistatic inline bool ovs_identifier_is_key(const struct sw_flow_id *sfid) 27762306a36Sopenharmony_ci{ 27862306a36Sopenharmony_ci return !ovs_identifier_is_ufid(sfid); 27962306a36Sopenharmony_ci} 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_civoid ovs_flow_stats_update(struct sw_flow *, __be16 tcp_flags, 28262306a36Sopenharmony_ci const struct sk_buff *); 28362306a36Sopenharmony_civoid ovs_flow_stats_get(const struct sw_flow *, struct ovs_flow_stats *, 28462306a36Sopenharmony_ci unsigned long *used, __be16 *tcp_flags); 28562306a36Sopenharmony_civoid ovs_flow_stats_clear(struct sw_flow *); 28662306a36Sopenharmony_ciu64 ovs_flow_used_time(unsigned long flow_jiffies); 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ciint ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key); 28962306a36Sopenharmony_ciint ovs_flow_key_update_l3l4(struct sk_buff *skb, struct sw_flow_key *key); 29062306a36Sopenharmony_ciint ovs_flow_key_extract(const struct ip_tunnel_info *tun_info, 29162306a36Sopenharmony_ci struct sk_buff *skb, 29262306a36Sopenharmony_ci struct sw_flow_key *key); 29362306a36Sopenharmony_ci/* Extract key from packet coming from userspace. */ 29462306a36Sopenharmony_ciint ovs_flow_key_extract_userspace(struct net *net, const struct nlattr *attr, 29562306a36Sopenharmony_ci struct sk_buff *skb, 29662306a36Sopenharmony_ci struct sw_flow_key *key, bool log); 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci#endif /* flow.h */ 299