datapath.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2007-2012 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #ifndef DATAPATH_H
  19. #define DATAPATH_H 1
  20. #include <asm/page.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mutex.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/u64_stats_sync.h>
  26. #include "flow.h"
  27. #include "vport.h"
  28. #define DP_MAX_PORTS USHRT_MAX
  29. #define DP_VPORT_HASH_BUCKETS 1024
  30. #define SAMPLE_ACTION_DEPTH 3
  31. /**
  32. * struct dp_stats_percpu - per-cpu packet processing statistics for a given
  33. * datapath.
  34. * @n_hit: Number of received packets for which a matching flow was found in
  35. * the flow table.
  36. * @n_miss: Number of received packets that had no matching flow in the flow
  37. * table. The sum of @n_hit and @n_miss is the number of packets that have
  38. * been received by the datapath.
  39. * @n_lost: Number of received packets that had no matching flow in the flow
  40. * table that could not be sent to userspace (normally due to an overflow in
  41. * one of the datapath's queues).
  42. */
  43. struct dp_stats_percpu {
  44. u64 n_hit;
  45. u64 n_missed;
  46. u64 n_lost;
  47. struct u64_stats_sync sync;
  48. };
  49. /**
  50. * struct datapath - datapath for flow-based packet switching
  51. * @rcu: RCU callback head for deferred destruction.
  52. * @list_node: Element in global 'dps' list.
  53. * @n_flows: Number of flows currently in flow table.
  54. * @table: Current flow table. Protected by genl_lock and RCU.
  55. * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
  56. * RTNL and RCU.
  57. * @stats_percpu: Per-CPU datapath statistics.
  58. * @net: Reference to net namespace.
  59. *
  60. * Context: See the comment on locking at the top of datapath.c for additional
  61. * locking information.
  62. */
  63. struct datapath {
  64. struct rcu_head rcu;
  65. struct list_head list_node;
  66. /* Flow table. */
  67. struct flow_table __rcu *table;
  68. /* Switch ports. */
  69. struct hlist_head *ports;
  70. /* Stats. */
  71. struct dp_stats_percpu __percpu *stats_percpu;
  72. #ifdef CONFIG_NET_NS
  73. /* Network namespace ref. */
  74. struct net *net;
  75. #endif
  76. };
  77. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no);
  78. static inline struct vport *ovs_vport_rcu(const struct datapath *dp, int port_no)
  79. {
  80. WARN_ON_ONCE(!rcu_read_lock_held());
  81. return ovs_lookup_vport(dp, port_no);
  82. }
  83. static inline struct vport *ovs_vport_rtnl_rcu(const struct datapath *dp, int port_no)
  84. {
  85. WARN_ON_ONCE(!rcu_read_lock_held() && !rtnl_is_locked());
  86. return ovs_lookup_vport(dp, port_no);
  87. }
  88. static inline struct vport *ovs_vport_rtnl(const struct datapath *dp, int port_no)
  89. {
  90. ASSERT_RTNL();
  91. return ovs_lookup_vport(dp, port_no);
  92. }
  93. /**
  94. * struct ovs_skb_cb - OVS data in skb CB
  95. * @flow: The flow associated with this packet. May be %NULL if no flow.
  96. */
  97. struct ovs_skb_cb {
  98. struct sw_flow *flow;
  99. };
  100. #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
  101. /**
  102. * struct dp_upcall - metadata to include with a packet to send to userspace
  103. * @cmd: One of %OVS_PACKET_CMD_*.
  104. * @key: Becomes %OVS_PACKET_ATTR_KEY. Must be nonnull.
  105. * @userdata: If nonnull, its u64 value is extracted and passed to userspace as
  106. * %OVS_PACKET_ATTR_USERDATA.
  107. * @pid: Netlink PID to which packet should be sent. If @pid is 0 then no
  108. * packet is sent and the packet is accounted in the datapath's @n_lost
  109. * counter.
  110. */
  111. struct dp_upcall_info {
  112. u8 cmd;
  113. const struct sw_flow_key *key;
  114. const struct nlattr *userdata;
  115. u32 portid;
  116. };
  117. static inline struct net *ovs_dp_get_net(struct datapath *dp)
  118. {
  119. return read_pnet(&dp->net);
  120. }
  121. static inline void ovs_dp_set_net(struct datapath *dp, struct net *net)
  122. {
  123. write_pnet(&dp->net, net);
  124. }
  125. extern struct notifier_block ovs_dp_device_notifier;
  126. extern struct genl_multicast_group ovs_dp_vport_multicast_group;
  127. void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
  128. void ovs_dp_detach_port(struct vport *);
  129. int ovs_dp_upcall(struct datapath *, struct sk_buff *,
  130. const struct dp_upcall_info *);
  131. const char *ovs_dp_name(const struct datapath *dp);
  132. struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,
  133. u8 cmd);
  134. int ovs_execute_actions(struct datapath *dp, struct sk_buff *skb);
  135. #endif /* datapath.h */