datapath.h 10.0 KB

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