vport.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 VPORT_H
  19. #define VPORT_H 1
  20. #include <linux/if_tunnel.h>
  21. #include <linux/list.h>
  22. #include <linux/netlink.h>
  23. #include <linux/openvswitch.h>
  24. #include <linux/reciprocal_div.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/u64_stats_sync.h>
  28. #include <net/route.h>
  29. #include "datapath.h"
  30. struct vport;
  31. struct vport_parms;
  32. /* The following definitions are for users of the vport subsytem: */
  33. int ovs_vport_init(void);
  34. void ovs_vport_exit(void);
  35. struct vport *ovs_vport_add(const struct vport_parms *);
  36. void ovs_vport_del(struct vport *);
  37. struct vport *ovs_vport_locate(const struct net *net, const char *name);
  38. void ovs_vport_get_stats(struct vport *, struct ovs_vport_stats *);
  39. int ovs_vport_set_options(struct vport *, struct nlattr *options);
  40. int ovs_vport_get_options(const struct vport *, struct sk_buff *);
  41. int ovs_vport_set_upcall_portids(struct vport *, const struct nlattr *pids);
  42. int ovs_vport_get_upcall_portids(const struct vport *, struct sk_buff *);
  43. u32 ovs_vport_find_upcall_portid(const struct vport *, struct sk_buff *);
  44. int ovs_tunnel_get_egress_info(struct dp_upcall_info *upcall,
  45. struct net *net,
  46. struct sk_buff *,
  47. u8 ipproto,
  48. __be16 tp_src,
  49. __be16 tp_dst);
  50. int ovs_vport_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
  51. struct dp_upcall_info *upcall);
  52. /**
  53. * struct vport_portids - array of netlink portids of a vport.
  54. * must be protected by rcu.
  55. * @rn_ids: The reciprocal value of @n_ids.
  56. * @rcu: RCU callback head for deferred destruction.
  57. * @n_ids: Size of @ids array.
  58. * @ids: Array storing the Netlink socket pids to be used for packets received
  59. * on this port that miss the flow table.
  60. */
  61. struct vport_portids {
  62. struct reciprocal_value rn_ids;
  63. struct rcu_head rcu;
  64. u32 n_ids;
  65. u32 ids[];
  66. };
  67. /**
  68. * struct vport - one port within a datapath
  69. * @rcu: RCU callback head for deferred destruction.
  70. * @dp: Datapath to which this port belongs.
  71. * @upcall_portids: RCU protected 'struct vport_portids'.
  72. * @port_no: Index into @dp's @ports array.
  73. * @hash_node: Element in @dev_table hash table in vport.c.
  74. * @dp_hash_node: Element in @datapath->ports hash table in datapath.c.
  75. * @ops: Class structure.
  76. * @detach_list: list used for detaching vport in net-exit call.
  77. */
  78. struct vport {
  79. struct net_device *dev;
  80. struct datapath *dp;
  81. struct vport_portids __rcu *upcall_portids;
  82. u16 port_no;
  83. struct hlist_node hash_node;
  84. struct hlist_node dp_hash_node;
  85. const struct vport_ops *ops;
  86. struct list_head detach_list;
  87. struct rcu_head rcu;
  88. };
  89. /**
  90. * struct vport_parms - parameters for creating a new vport
  91. *
  92. * @name: New vport's name.
  93. * @type: New vport's type.
  94. * @options: %OVS_VPORT_ATTR_OPTIONS attribute from Netlink message, %NULL if
  95. * none was supplied.
  96. * @dp: New vport's datapath.
  97. * @port_no: New vport's port number.
  98. */
  99. struct vport_parms {
  100. const char *name;
  101. enum ovs_vport_type type;
  102. struct nlattr *options;
  103. /* For ovs_vport_alloc(). */
  104. struct datapath *dp;
  105. u16 port_no;
  106. struct nlattr *upcall_portids;
  107. };
  108. /**
  109. * struct vport_ops - definition of a type of virtual port
  110. *
  111. * @type: %OVS_VPORT_TYPE_* value for this type of virtual port.
  112. * @create: Create a new vport configured as specified. On success returns
  113. * a new vport allocated with ovs_vport_alloc(), otherwise an ERR_PTR() value.
  114. * @destroy: Destroys a vport. Must call vport_free() on the vport but not
  115. * before an RCU grace period has elapsed.
  116. * @set_options: Modify the configuration of an existing vport. May be %NULL
  117. * if modification is not supported.
  118. * @get_options: Appends vport-specific attributes for the configuration of an
  119. * existing vport to a &struct sk_buff. May be %NULL for a vport that does not
  120. * have any configuration.
  121. * @send: Send a packet on the device.
  122. * zero for dropped packets or negative for error.
  123. * @get_egress_tun_info: Get the egress tunnel 5-tuple and other info for
  124. * a packet.
  125. */
  126. struct vport_ops {
  127. enum ovs_vport_type type;
  128. /* Called with ovs_mutex. */
  129. struct vport *(*create)(const struct vport_parms *);
  130. void (*destroy)(struct vport *);
  131. int (*set_options)(struct vport *, struct nlattr *);
  132. int (*get_options)(const struct vport *, struct sk_buff *);
  133. void (*send)(struct vport *, struct sk_buff *);
  134. int (*get_egress_tun_info)(struct vport *, struct sk_buff *,
  135. struct dp_upcall_info *upcall);
  136. struct module *owner;
  137. struct list_head list;
  138. };
  139. struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *,
  140. const struct vport_parms *);
  141. void ovs_vport_free(struct vport *);
  142. void ovs_vport_deferred_free(struct vport *vport);
  143. #define VPORT_ALIGN 8
  144. /**
  145. * vport_priv - access private data area of vport
  146. *
  147. * @vport: vport to access
  148. *
  149. * If a nonzero size was passed in priv_size of vport_alloc() a private data
  150. * area was allocated on creation. This allows that area to be accessed and
  151. * used for any purpose needed by the vport implementer.
  152. */
  153. static inline void *vport_priv(const struct vport *vport)
  154. {
  155. return (u8 *)(uintptr_t)vport + ALIGN(sizeof(struct vport), VPORT_ALIGN);
  156. }
  157. /**
  158. * vport_from_priv - lookup vport from private data pointer
  159. *
  160. * @priv: Start of private data area.
  161. *
  162. * It is sometimes useful to translate from a pointer to the private data
  163. * area to the vport, such as in the case where the private data pointer is
  164. * the result of a hash table lookup. @priv must point to the start of the
  165. * private data area.
  166. */
  167. static inline struct vport *vport_from_priv(void *priv)
  168. {
  169. return (struct vport *)((u8 *)priv - ALIGN(sizeof(struct vport), VPORT_ALIGN));
  170. }
  171. int ovs_vport_receive(struct vport *, struct sk_buff *,
  172. const struct ip_tunnel_info *);
  173. static inline void ovs_skb_postpush_rcsum(struct sk_buff *skb,
  174. const void *start, unsigned int len)
  175. {
  176. if (skb->ip_summed == CHECKSUM_COMPLETE)
  177. skb->csum = csum_add(skb->csum, csum_partial(start, len, 0));
  178. }
  179. static inline const char *ovs_vport_name(struct vport *vport)
  180. {
  181. return vport->dev->name;
  182. }
  183. int ovs_vport_ops_register(struct vport_ops *ops);
  184. void ovs_vport_ops_unregister(struct vport_ops *ops);
  185. static inline struct rtable *ovs_tunnel_route_lookup(struct net *net,
  186. const struct ip_tunnel_key *key,
  187. u32 mark,
  188. struct flowi4 *fl,
  189. u8 protocol)
  190. {
  191. struct rtable *rt;
  192. memset(fl, 0, sizeof(*fl));
  193. fl->daddr = key->u.ipv4.dst;
  194. fl->saddr = key->u.ipv4.src;
  195. fl->flowi4_tos = RT_TOS(key->tos);
  196. fl->flowi4_mark = mark;
  197. fl->flowi4_proto = protocol;
  198. rt = ip_route_output_key(net, fl);
  199. return rt;
  200. }
  201. static inline void ovs_vport_send(struct vport *vport, struct sk_buff *skb)
  202. {
  203. vport->ops->send(vport, skb);
  204. }
  205. #endif /* vport.h */