1ccb1352eSJesse Gross /* 2971427f3SAndy Zhou * Copyright (c) 2007-2014 Nicira, Inc. 3ccb1352eSJesse Gross * 4ccb1352eSJesse Gross * This program is free software; you can redistribute it and/or 5ccb1352eSJesse Gross * modify it under the terms of version 2 of the GNU General Public 6ccb1352eSJesse Gross * License as published by the Free Software Foundation. 7ccb1352eSJesse Gross * 8ccb1352eSJesse Gross * This program is distributed in the hope that it will be useful, but 9ccb1352eSJesse Gross * WITHOUT ANY WARRANTY; without even the implied warranty of 10ccb1352eSJesse Gross * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11ccb1352eSJesse Gross * General Public License for more details. 12ccb1352eSJesse Gross * 13ccb1352eSJesse Gross * You should have received a copy of the GNU General Public License 14ccb1352eSJesse Gross * along with this program; if not, write to the Free Software 15ccb1352eSJesse Gross * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16ccb1352eSJesse Gross * 02110-1301, USA 17ccb1352eSJesse Gross */ 18ccb1352eSJesse Gross 19ccb1352eSJesse Gross #ifndef DATAPATH_H 20ccb1352eSJesse Gross #define DATAPATH_H 1 21ccb1352eSJesse Gross 22ccb1352eSJesse Gross #include <asm/page.h> 23ccb1352eSJesse Gross #include <linux/kernel.h> 24ccb1352eSJesse Gross #include <linux/mutex.h> 25ccb1352eSJesse Gross #include <linux/netdevice.h> 26ccb1352eSJesse Gross #include <linux/skbuff.h> 27ccb1352eSJesse Gross #include <linux/u64_stats_sync.h> 28ccb1352eSJesse Gross 29ccb1352eSJesse Gross #include "flow.h" 30e6445719SPravin B Shelar #include "flow_table.h" 3146df7b81SPravin B Shelar #include "vport.h" 32ccb1352eSJesse Gross 3315eac2a7SPravin B Shelar #define DP_MAX_PORTS USHRT_MAX 3415eac2a7SPravin B Shelar #define DP_VPORT_HASH_BUCKETS 1024 3515eac2a7SPravin B Shelar 36ccb1352eSJesse Gross #define SAMPLE_ACTION_DEPTH 3 37ccb1352eSJesse Gross 38ccb1352eSJesse Gross /** 39ccb1352eSJesse Gross * struct dp_stats_percpu - per-cpu packet processing statistics for a given 40ccb1352eSJesse Gross * datapath. 41ccb1352eSJesse Gross * @n_hit: Number of received packets for which a matching flow was found in 42ccb1352eSJesse Gross * the flow table. 43ccb1352eSJesse Gross * @n_miss: Number of received packets that had no matching flow in the flow 44ccb1352eSJesse Gross * table. The sum of @n_hit and @n_miss is the number of packets that have 45ccb1352eSJesse Gross * been received by the datapath. 46ccb1352eSJesse Gross * @n_lost: Number of received packets that had no matching flow in the flow 47ccb1352eSJesse Gross * table that could not be sent to userspace (normally due to an overflow in 48ccb1352eSJesse Gross * one of the datapath's queues). 491bd7116fSAndy Zhou * @n_mask_hit: Number of masks looked up for flow match. 501bd7116fSAndy Zhou * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked 511bd7116fSAndy Zhou * up per packet. 52ccb1352eSJesse Gross */ 53ccb1352eSJesse Gross struct dp_stats_percpu { 54ccb1352eSJesse Gross u64 n_hit; 55ccb1352eSJesse Gross u64 n_missed; 56ccb1352eSJesse Gross u64 n_lost; 571bd7116fSAndy Zhou u64 n_mask_hit; 58df9d9fdfSWANG Cong struct u64_stats_sync syncp; 59ccb1352eSJesse Gross }; 60ccb1352eSJesse Gross 61ccb1352eSJesse Gross /** 62ccb1352eSJesse Gross * struct datapath - datapath for flow-based packet switching 63ccb1352eSJesse Gross * @rcu: RCU callback head for deferred destruction. 64ccb1352eSJesse Gross * @list_node: Element in global 'dps' list. 65b637e498SPravin B Shelar * @table: flow table. 6615eac2a7SPravin B Shelar * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by 678e4e1713SPravin B Shelar * ovs_mutex and RCU. 68ccb1352eSJesse Gross * @stats_percpu: Per-CPU datapath statistics. 6946df7b81SPravin B Shelar * @net: Reference to net namespace. 70ccb1352eSJesse Gross * 71ccb1352eSJesse Gross * Context: See the comment on locking at the top of datapath.c for additional 72ccb1352eSJesse Gross * locking information. 73ccb1352eSJesse Gross */ 74ccb1352eSJesse Gross struct datapath { 75ccb1352eSJesse Gross struct rcu_head rcu; 76ccb1352eSJesse Gross struct list_head list_node; 77ccb1352eSJesse Gross 78ccb1352eSJesse Gross /* Flow table. */ 79b637e498SPravin B Shelar struct flow_table table; 80ccb1352eSJesse Gross 81ccb1352eSJesse Gross /* Switch ports. */ 8215eac2a7SPravin B Shelar struct hlist_head *ports; 83ccb1352eSJesse Gross 84ccb1352eSJesse Gross /* Stats. */ 85ccb1352eSJesse Gross struct dp_stats_percpu __percpu *stats_percpu; 8646df7b81SPravin B Shelar 8746df7b81SPravin B Shelar #ifdef CONFIG_NET_NS 8846df7b81SPravin B Shelar /* Network namespace ref. */ 8946df7b81SPravin B Shelar struct net *net; 9046df7b81SPravin B Shelar #endif 9143d4be9cSThomas Graf 9243d4be9cSThomas Graf u32 user_features; 93ccb1352eSJesse Gross }; 94ccb1352eSJesse Gross 95ccb1352eSJesse Gross /** 96ccb1352eSJesse Gross * struct ovs_skb_cb - OVS data in skb CB 978c8b1b83SPravin B Shelar * @egress_tun_key: Tunnel information about this packet on egress path. 988c8b1b83SPravin B Shelar * NULL if the packet is not being tunneled. 9983c8df26SPravin B Shelar * @input_vport: The original vport packet came in on. This value is cached 10083c8df26SPravin B Shelar * when a packet is received by OVS. 101ccb1352eSJesse Gross */ 102ccb1352eSJesse Gross struct ovs_skb_cb { 103f0b128c1SJesse Gross struct ovs_tunnel_info *egress_tun_info; 10483c8df26SPravin B Shelar struct vport *input_vport; 105ccb1352eSJesse Gross }; 106ccb1352eSJesse Gross #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb) 107ccb1352eSJesse Gross 108ccb1352eSJesse Gross /** 109ccb1352eSJesse Gross * struct dp_upcall - metadata to include with a packet to send to userspace 110ccb1352eSJesse Gross * @cmd: One of %OVS_PACKET_CMD_*. 111ccb1352eSJesse Gross * @key: Becomes %OVS_PACKET_ATTR_KEY. Must be nonnull. 1124490108bSBen Pfaff * @userdata: If nonnull, its variable-length value is passed to userspace as 113ccb1352eSJesse Gross * %OVS_PACKET_ATTR_USERDATA. 114ccb1352eSJesse Gross * @pid: Netlink PID to which packet should be sent. If @pid is 0 then no 115ccb1352eSJesse Gross * packet is sent and the packet is accounted in the datapath's @n_lost 116ccb1352eSJesse Gross * counter. 1178f0aad6fSWenyu Zhang * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY. 118ccb1352eSJesse Gross */ 119ccb1352eSJesse Gross struct dp_upcall_info { 120ccb1352eSJesse Gross u8 cmd; 121ccb1352eSJesse Gross const struct sw_flow_key *key; 122ccb1352eSJesse Gross const struct nlattr *userdata; 12315e47304SEric W. Biederman u32 portid; 1248f0aad6fSWenyu Zhang const struct ovs_tunnel_info *egress_tun_info; 125ccb1352eSJesse Gross }; 126ccb1352eSJesse Gross 1278e4e1713SPravin B Shelar /** 1288e4e1713SPravin B Shelar * struct ovs_net - Per net-namespace data for ovs. 1298e4e1713SPravin B Shelar * @dps: List of datapaths to enable dumping them all out. 1308e4e1713SPravin B Shelar * Protected by genl_mutex. 1318e4e1713SPravin B Shelar */ 1328e4e1713SPravin B Shelar struct ovs_net { 1338e4e1713SPravin B Shelar struct list_head dps; 1348e4e1713SPravin B Shelar struct work_struct dp_notify_work; 135aa310701SPravin B Shelar struct vport_net vport_net; 1368e4e1713SPravin B Shelar }; 1378e4e1713SPravin B Shelar 1388e4e1713SPravin B Shelar extern int ovs_net_id; 1398e4e1713SPravin B Shelar void ovs_lock(void); 1408e4e1713SPravin B Shelar void ovs_unlock(void); 1418e4e1713SPravin B Shelar 1428e4e1713SPravin B Shelar #ifdef CONFIG_LOCKDEP 1438e4e1713SPravin B Shelar int lockdep_ovsl_is_held(void); 1448e4e1713SPravin B Shelar #else 1458e4e1713SPravin B Shelar #define lockdep_ovsl_is_held() 1 1468e4e1713SPravin B Shelar #endif 1478e4e1713SPravin B Shelar 14880019d31SThomas Graf #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held()) 1498e4e1713SPravin B Shelar #define ovsl_dereference(p) \ 1508e4e1713SPravin B Shelar rcu_dereference_protected(p, lockdep_ovsl_is_held()) 151663efa36SJesse Gross #define rcu_dereference_ovsl(p) \ 152663efa36SJesse Gross rcu_dereference_check(p, lockdep_ovsl_is_held()) 1538e4e1713SPravin B Shelar 15446df7b81SPravin B Shelar static inline struct net *ovs_dp_get_net(struct datapath *dp) 15546df7b81SPravin B Shelar { 15646df7b81SPravin B Shelar return read_pnet(&dp->net); 15746df7b81SPravin B Shelar } 15846df7b81SPravin B Shelar 15946df7b81SPravin B Shelar static inline void ovs_dp_set_net(struct datapath *dp, struct net *net) 16046df7b81SPravin B Shelar { 16146df7b81SPravin B Shelar write_pnet(&dp->net, net); 16246df7b81SPravin B Shelar } 16346df7b81SPravin B Shelar 1648e4e1713SPravin B Shelar struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no); 1658e4e1713SPravin B Shelar 1668e4e1713SPravin B Shelar static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no) 1678e4e1713SPravin B Shelar { 1688e4e1713SPravin B Shelar WARN_ON_ONCE(!rcu_read_lock_held()); 1698e4e1713SPravin B Shelar return ovs_lookup_vport(dp, port_no); 1708e4e1713SPravin B Shelar } 1718e4e1713SPravin B Shelar 1728e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no) 1738e4e1713SPravin B Shelar { 1748e4e1713SPravin B Shelar WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held()); 1758e4e1713SPravin B Shelar return ovs_lookup_vport(dp, port_no); 1768e4e1713SPravin B Shelar } 1778e4e1713SPravin B Shelar 1788e4e1713SPravin B Shelar static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no) 1798e4e1713SPravin B Shelar { 1808e4e1713SPravin B Shelar ASSERT_OVSL(); 1818e4e1713SPravin B Shelar return ovs_lookup_vport(dp, port_no); 1828e4e1713SPravin B Shelar } 1838e4e1713SPravin B Shelar 184ccb1352eSJesse Gross extern struct notifier_block ovs_dp_device_notifier; 18568eb5503SJohannes Berg extern struct genl_family dp_vport_genl_family; 186ccb1352eSJesse Gross 1878c8b1b83SPravin B Shelar void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key); 188ccb1352eSJesse Gross void ovs_dp_detach_port(struct vport *); 189ccb1352eSJesse Gross int ovs_dp_upcall(struct datapath *, struct sk_buff *, 190ccb1352eSJesse Gross const struct dp_upcall_info *); 191ccb1352eSJesse Gross 192971427f3SAndy Zhou const char *ovs_dp_name(const struct datapath *dp); 193ccb1352eSJesse Gross struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq, 194ccb1352eSJesse Gross u8 cmd); 195ccb1352eSJesse Gross 1962ff3e4e4SPravin B Shelar int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb, 197d98612b8SLorand Jakab struct sw_flow_actions *acts, struct sw_flow_key *); 198971427f3SAndy Zhou 1998e4e1713SPravin B Shelar void ovs_dp_notify_wq(struct work_struct *work); 20003f0d916SAndy Zhou 201971427f3SAndy Zhou int action_fifos_init(void); 202971427f3SAndy Zhou void action_fifos_exit(void); 203971427f3SAndy Zhou 20403f0d916SAndy Zhou #define OVS_NLERR(fmt, ...) \ 2051815a883SJoe Perches do { \ 2061815a883SJoe Perches if (net_ratelimit()) \ 2071815a883SJoe Perches pr_info("netlink: " fmt, ##__VA_ARGS__); \ 2081815a883SJoe Perches } while (0) 209ccb1352eSJesse Gross #endif /* datapath.h */ 210