datapath.h 8.1 KB

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