datapath.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "conntrack.h"
  15. #include "flow.h"
  16. #include "flow_table.h"
  17. #include "meter.h"
  18. #include "vport-internal_dev.h"
  19. #define DP_MAX_PORTS USHRT_MAX
  20. #define DP_VPORT_HASH_BUCKETS 1024
  21. #define DP_MASKS_REBALANCE_INTERVAL 4000
  22. /**
  23. * struct dp_stats_percpu - per-cpu packet processing statistics for a given
  24. * datapath.
  25. * @n_hit: Number of received packets for which a matching flow was found in
  26. * the flow table.
  27. * @n_miss: Number of received packets that had no matching flow in the flow
  28. * table. The sum of @n_hit and @n_miss is the number of packets that have
  29. * been received by the datapath.
  30. * @n_lost: Number of received packets that had no matching flow in the flow
  31. * table that could not be sent to userspace (normally due to an overflow in
  32. * one of the datapath's queues).
  33. * @n_mask_hit: Number of masks looked up for flow match.
  34. * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked
  35. * up per packet.
  36. * @n_cache_hit: The number of received packets that had their mask found using
  37. * the mask cache.
  38. */
  39. struct dp_stats_percpu {
  40. u64 n_hit;
  41. u64 n_missed;
  42. u64 n_lost;
  43. u64 n_mask_hit;
  44. u64 n_cache_hit;
  45. struct u64_stats_sync syncp;
  46. };
  47. /**
  48. * struct datapath - datapath for flow-based packet switching
  49. * @rcu: RCU callback head for deferred destruction.
  50. * @list_node: Element in global 'dps' list.
  51. * @table: flow table.
  52. * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
  53. * ovs_mutex and RCU.
  54. * @stats_percpu: Per-CPU datapath statistics.
  55. * @net: Reference to net namespace.
  56. * @max_headroom: the maximum headroom of all vports in this datapath; it will
  57. * be used by all the internal vports in this dp.
  58. *
  59. * Context: See the comment on locking at the top of datapath.c for additional
  60. * locking information.
  61. */
  62. struct datapath {
  63. struct rcu_head rcu;
  64. struct list_head list_node;
  65. /* Flow table. */
  66. struct flow_table table;
  67. /* Switch ports. */
  68. struct hlist_head *ports;
  69. /* Stats. */
  70. struct dp_stats_percpu __percpu *stats_percpu;
  71. /* Network namespace ref. */
  72. possible_net_t net;
  73. u32 user_features;
  74. u32 max_headroom;
  75. /* Switch meters. */
  76. struct dp_meter_table meter_tbl;
  77. };
  78. /**
  79. * struct ovs_skb_cb - OVS data in skb CB
  80. * @input_vport: The original vport packet came in on. This value is cached
  81. * when a packet is received by OVS.
  82. * @mru: The maximum received fragement size; 0 if the packet is not
  83. * fragmented.
  84. * @acts_origlen: The netlink size of the flow actions applied to this skb.
  85. * @cutlen: The number of bytes from the packet end to be removed.
  86. */
  87. struct ovs_skb_cb {
  88. struct vport *input_vport;
  89. u16 mru;
  90. u16 acts_origlen;
  91. u32 cutlen;
  92. };
  93. #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
  94. /**
  95. * struct dp_upcall - metadata to include with a packet to send to userspace
  96. * @cmd: One of %OVS_PACKET_CMD_*.
  97. * @userdata: If nonnull, its variable-length value is passed to userspace as
  98. * %OVS_PACKET_ATTR_USERDATA.
  99. * @portid: Netlink portid to which packet should be sent. If @portid is 0
  100. * then no packet is sent and the packet is accounted in the datapath's @n_lost
  101. * counter.
  102. * @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
  103. * @mru: If not zero, Maximum received IP fragment size.
  104. */
  105. struct dp_upcall_info {
  106. struct ip_tunnel_info *egress_tun_info;
  107. const struct nlattr *userdata;
  108. const struct nlattr *actions;
  109. int actions_len;
  110. u32 portid;
  111. u8 cmd;
  112. u16 mru;
  113. };
  114. /**
  115. * struct ovs_net - Per net-namespace data for ovs.
  116. * @dps: List of datapaths to enable dumping them all out.
  117. * Protected by genl_mutex.
  118. */
  119. struct ovs_net {
  120. struct list_head dps;
  121. struct work_struct dp_notify_work;
  122. struct delayed_work masks_rebalance;
  123. #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
  124. struct ovs_ct_limit_info *ct_limit_info;
  125. #endif
  126. /* Module reference for configuring conntrack. */
  127. bool xt_label;
  128. };
  129. /**
  130. * enum ovs_pkt_hash_types - hash info to include with a packet
  131. * to send to userspace.
  132. * @OVS_PACKET_HASH_SW_BIT: indicates hash was computed in software stack.
  133. * @OVS_PACKET_HASH_L4_BIT: indicates hash is a canonical 4-tuple hash
  134. * over transport ports.
  135. */
  136. enum ovs_pkt_hash_types {
  137. OVS_PACKET_HASH_SW_BIT = (1ULL << 32),
  138. OVS_PACKET_HASH_L4_BIT = (1ULL << 33),
  139. };
  140. extern unsigned int ovs_net_id;
  141. void ovs_lock(void);
  142. void ovs_unlock(void);
  143. #ifdef CONFIG_LOCKDEP
  144. int lockdep_ovsl_is_held(void);
  145. #else
  146. #define lockdep_ovsl_is_held() 1
  147. #endif
  148. #define ASSERT_OVSL() WARN_ON(!lockdep_ovsl_is_held())
  149. #define ovsl_dereference(p) \
  150. rcu_dereference_protected(p, lockdep_ovsl_is_held())
  151. #define rcu_dereference_ovsl(p) \
  152. rcu_dereference_check(p, lockdep_ovsl_is_held())
  153. static inline struct net *ovs_dp_get_net(const struct datapath *dp)
  154. {
  155. return read_pnet(&dp->net);
  156. }
  157. static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
  158. {
  159. write_pnet(&dp->net, net);
  160. }
  161. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
  162. static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
  163. {
  164. WARN_ON_ONCE(!rcu_read_lock_held());
  165. return ovs_lookup_vport(dp, port_no);
  166. }
  167. static inline struct vport *ovs_vport_ovsl_rcu(const struct datapath *dp, int port_no)
  168. {
  169. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  170. return ovs_lookup_vport(dp, port_no);
  171. }
  172. static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_no)
  173. {
  174. ASSERT_OVSL();
  175. return ovs_lookup_vport(dp, port_no);
  176. }
  177. /* Must be called with rcu_read_lock. */
  178. static inline struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
  179. {
  180. struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
  181. if (dev) {
  182. struct vport *vport = ovs_internal_dev_get_vport(dev);
  183. if (vport)
  184. return vport->dp;
  185. }
  186. return NULL;
  187. }
  188. /* The caller must hold either ovs_mutex or rcu_read_lock to keep the
  189. * returned dp pointer valid.
  190. */
  191. static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
  192. {
  193. struct datapath *dp;
  194. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
  195. rcu_read_lock();
  196. dp = get_dp_rcu(net, dp_ifindex);
  197. rcu_read_unlock();
  198. return dp;
  199. }
  200. extern struct notifier_block ovs_dp_device_notifier;
  201. extern struct genl_family dp_vport_genl_family;
  202. DECLARE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
  203. void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
  204. void ovs_dp_detach_port(struct vport *);
  205. int ovs_dp_upcall(struct datapath *, struct sk_buff *,
  206. const struct sw_flow_key *, const struct dp_upcall_info *,
  207. uint32_t cutlen);
  208. const char *ovs_dp_name(const struct datapath *dp);
  209. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
  210. u32 portid, u32 seq, u8 cmd);
  211. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb,
  212. const struct sw_flow_actions *, struct sw_flow_key *);
  213. void ovs_dp_notify_wq(struct work_struct *work);
  214. int action_fifos_init(void);
  215. void action_fifos_exit(void);
  216. /* 'KEY' must not have any bits set outside of the 'MASK' */
  217. #define OVS_MASKED(OLD, KEY, MASK) ((KEY) | ((OLD) & ~(MASK)))
  218. #define OVS_SET_MASKED(OLD, KEY, MASK) ((OLD) = OVS_MASKED(OLD, KEY, MASK))
  219. #define OVS_NLERR(logging_allowed, fmt, ...) \
  220. do { \
  221. if (logging_allowed && net_ratelimit()) \
  222. pr_info("netlink: " fmt "\n", ##__VA_ARGS__); \
  223. } while (0)
  224. #endif /* datapath.h */