Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (c) 2007-2014 Nicira, Inc.
0004  */
0005 
0006 #ifndef DATAPATH_H
0007 #define DATAPATH_H 1
0008 
0009 #include <asm/page.h>
0010 #include <linux/kernel.h>
0011 #include <linux/mutex.h>
0012 #include <linux/netdevice.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/u64_stats_sync.h>
0015 #include <net/ip_tunnels.h>
0016 
0017 #include "conntrack.h"
0018 #include "flow.h"
0019 #include "flow_table.h"
0020 #include "meter.h"
0021 #include "vport-internal_dev.h"
0022 
0023 #define DP_MAX_PORTS                USHRT_MAX
0024 #define DP_VPORT_HASH_BUCKETS       1024
0025 #define DP_MASKS_REBALANCE_INTERVAL 4000
0026 
0027 /**
0028  * struct dp_stats_percpu - per-cpu packet processing statistics for a given
0029  * datapath.
0030  * @n_hit: Number of received packets for which a matching flow was found in
0031  * the flow table.
0032  * @n_miss: Number of received packets that had no matching flow in the flow
0033  * table.  The sum of @n_hit and @n_miss is the number of packets that have
0034  * been received by the datapath.
0035  * @n_lost: Number of received packets that had no matching flow in the flow
0036  * table that could not be sent to userspace (normally due to an overflow in
0037  * one of the datapath's queues).
0038  * @n_mask_hit: Number of masks looked up for flow match.
0039  *   @n_mask_hit / (@n_hit + @n_missed)  will be the average masks looked
0040  *   up per packet.
0041  * @n_cache_hit: The number of received packets that had their mask found using
0042  * the mask cache.
0043  */
0044 struct dp_stats_percpu {
0045     u64 n_hit;
0046     u64 n_missed;
0047     u64 n_lost;
0048     u64 n_mask_hit;
0049     u64 n_cache_hit;
0050     struct u64_stats_sync syncp;
0051 };
0052 
0053 /**
0054  * struct dp_nlsk_pids - array of netlink portids of for a datapath.
0055  *                       This is used when OVS_DP_F_DISPATCH_UPCALL_PER_CPU
0056  *                       is enabled and must be protected by rcu.
0057  * @rcu: RCU callback head for deferred destruction.
0058  * @n_pids: Size of @pids array.
0059  * @pids: Array storing the Netlink socket PIDs indexed by CPU ID for packets
0060  *       that miss the flow table.
0061  */
0062 struct dp_nlsk_pids {
0063     struct rcu_head rcu;
0064     u32 n_pids;
0065     u32 pids[];
0066 };
0067 
0068 /**
0069  * struct datapath - datapath for flow-based packet switching
0070  * @rcu: RCU callback head for deferred destruction.
0071  * @list_node: Element in global 'dps' list.
0072  * @table: flow table.
0073  * @ports: Hash table for ports.  %OVSP_LOCAL port always exists.  Protected by
0074  * ovs_mutex and RCU.
0075  * @stats_percpu: Per-CPU datapath statistics.
0076  * @net: Reference to net namespace.
0077  * @max_headroom: the maximum headroom of all vports in this datapath; it will
0078  * be used by all the internal vports in this dp.
0079  * @upcall_portids: RCU protected 'struct dp_nlsk_pids'.
0080  *
0081  * Context: See the comment on locking at the top of datapath.c for additional
0082  * locking information.
0083  */
0084 struct datapath {
0085     struct rcu_head rcu;
0086     struct list_head list_node;
0087 
0088     /* Flow table. */
0089     struct flow_table table;
0090 
0091     /* Switch ports. */
0092     struct hlist_head *ports;
0093 
0094     /* Stats. */
0095     struct dp_stats_percpu __percpu *stats_percpu;
0096 
0097     /* Network namespace ref. */
0098     possible_net_t net;
0099 
0100     u32 user_features;
0101 
0102     u32 max_headroom;
0103 
0104     /* Switch meters. */
0105     struct dp_meter_table meter_tbl;
0106 
0107     struct dp_nlsk_pids __rcu *upcall_portids;
0108 };
0109 
0110 /**
0111  * struct ovs_skb_cb - OVS data in skb CB
0112  * @input_vport: The original vport packet came in on. This value is cached
0113  * when a packet is received by OVS.
0114  * @mru: The maximum received fragement size; 0 if the packet is not
0115  * fragmented.
0116  * @acts_origlen: The netlink size of the flow actions applied to this skb.
0117  * @cutlen: The number of bytes from the packet end to be removed.
0118  */
0119 struct ovs_skb_cb {
0120     struct vport        *input_vport;
0121     u16         mru;
0122     u16         acts_origlen;
0123     u32         cutlen;
0124 };
0125 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
0126 
0127 /**
0128  * struct dp_upcall - metadata to include with a packet to send to userspace
0129  * @cmd: One of %OVS_PACKET_CMD_*.
0130  * @userdata: If nonnull, its variable-length value is passed to userspace as
0131  * %OVS_PACKET_ATTR_USERDATA.
0132  * @portid: Netlink portid to which packet should be sent.  If @portid is 0
0133  * then no packet is sent and the packet is accounted in the datapath's @n_lost
0134  * counter.
0135  * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
0136  * @mru: If not zero, Maximum received IP fragment size.
0137  */
0138 struct dp_upcall_info {
0139     struct ip_tunnel_info *egress_tun_info;
0140     const struct nlattr *userdata;
0141     const struct nlattr *actions;
0142     int actions_len;
0143     u32 portid;
0144     u8 cmd;
0145     u16 mru;
0146 };
0147 
0148 /**
0149  * struct ovs_net - Per net-namespace data for ovs.
0150  * @dps: List of datapaths to enable dumping them all out.
0151  * Protected by genl_mutex.
0152  */
0153 struct ovs_net {
0154     struct list_head dps;
0155     struct work_struct dp_notify_work;
0156     struct delayed_work masks_rebalance;
0157 #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
0158     struct ovs_ct_limit_info *ct_limit_info;
0159 #endif
0160 
0161     /* Module reference for configuring conntrack. */
0162     bool xt_label;
0163 };
0164 
0165 /**
0166  * enum ovs_pkt_hash_types - hash info to include with a packet
0167  * to send to userspace.
0168  * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
0169  * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
0170  * over transport ports.
0171  */
0172 enum ovs_pkt_hash_types {
0173     OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
0174     OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
0175 };
0176 
0177 extern unsigned int ovs_net_id;
0178 void ovs_lock(void);
0179 void ovs_unlock(void);
0180 
0181 #ifdef CONFIG_LOCKDEP
0182 int lockdep_ovsl_is_held(void);
0183 #else
0184 #define lockdep_ovsl_is_held()  1
0185 #endif
0186 
0187 #define ASSERT_OVSL()       WARN_ON(!lockdep_ovsl_is_held())
0188 #define ovsl_dereference(p)                 \
0189     rcu_dereference_protected(p, lockdep_ovsl_is_held())
0190 #define rcu_dereference_ovsl(p)                 \
0191     rcu_dereference_check(p, lockdep_ovsl_is_held())
0192 
0193 static inline struct net *ovs_dp_get_net(const struct datapath *dp)
0194 {
0195     return read_pnet(&dp->net);
0196 }
0197 
0198 static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
0199 {
0200     write_pnet(&dp->net, net);
0201 }
0202 
0203 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
0204 
0205 static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
0206 {
0207     WARN_ON_ONCE(!rcu_read_lock_held());
0208     return ovs_lookup_vport(dp, port_no);
0209 }
0210 
0211 static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
0212 {
0213     WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
0214     return ovs_lookup_vport(dp, port_no);
0215 }
0216 
0217 static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
0218 {
0219     ASSERT_OVSL();
0220     return ovs_lookup_vport(dp, port_no);
0221 }
0222 
0223 /* Must be called with rcu_read_lock. */
0224 static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
0225 {
0226     struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
0227 
0228     if (dev) {
0229         struct vport *vport = ovs_internal_dev_get_vport(dev);
0230 
0231         if (vport)
0232             return vport->dp;
0233     }
0234 
0235     return NULL;
0236 }
0237 
0238 /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
0239  * returned dp pointer valid.
0240  */
0241 static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
0242 {
0243     struct datapath *dp;
0244 
0245     WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
0246     rcu_read_lock();
0247     dp = get_dp_rcu(net, dp_ifindex);
0248     rcu_read_unlock();
0249 
0250     return dp;
0251 }
0252 
0253 extern struct notifier_block ovs_dp_device_notifier;
0254 extern struct genl_family dp_vport_genl_family;
0255 
0256 void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
0257 void ovs_dp_detach_port(struct vport *);
0258 int ovs_dp_upcall(struct datapath *, struct sk_buff *,
0259           const struct sw_flow_key *, const struct dp_upcall_info *,
0260           uint32_t cutlen);
0261 
0262 u32 ovs_dp_get_upcall_portid(const struct datapath *dp, uint32_t cpu_id);
0263 
0264 const char *ovs_dp_name(const struct datapath *dp);
0265 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
0266                      u32 portid, u32 seq, u8 cmd);
0267 
0268 int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
0269             const struct sw_flow_actions *, struct sw_flow_key *);
0270 
0271 void ovs_dp_notify_wq(struct work_struct *work);
0272 
0273 int action_fifos_init(void);
0274 void action_fifos_exit(void);
0275 
0276 /* 'KEY' must not have any bits set outside of the 'MASK' */
0277 #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
0278 #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
0279 
0280 #define OVS_NLERR(logging_allowed, fmt, ...)            \
0281 do {                                \
0282     if (logging_allowed && net_ratelimit())         \
0283         pr_info("netlink: " fmt "\n", ##__VA_ARGS__);   \
0284 } while (0)
0285 #endif /* datapath.h */