vport-geneve.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2014 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/in.h>
  11. #include <linux/ip.h>
  12. #include <linux/net.h>
  13. #include <linux/rculist.h>
  14. #include <linux/udp.h>
  15. #include <linux/if_vlan.h>
  16. #include <linux/module.h>
  17. #include <net/geneve.h>
  18. #include <net/icmp.h>
  19. #include <net/ip.h>
  20. #include <net/route.h>
  21. #include <net/udp.h>
  22. #include <net/xfrm.h>
  23. #include "datapath.h"
  24. #include "vport.h"
  25. static struct vport_ops ovs_geneve_vport_ops;
  26. /**
  27. * struct geneve_port - Keeps track of open UDP ports
  28. * @gs: The socket created for this port number.
  29. * @name: vport name.
  30. */
  31. struct geneve_port {
  32. struct geneve_sock *gs;
  33. char name[IFNAMSIZ];
  34. };
  35. static LIST_HEAD(geneve_ports);
  36. static inline struct geneve_port *geneve_vport(const struct vport *vport)
  37. {
  38. return vport_priv(vport);
  39. }
  40. /* Convert 64 bit tunnel ID to 24 bit VNI. */
  41. static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
  42. {
  43. #ifdef __BIG_ENDIAN
  44. vni[0] = (__force __u8)(tun_id >> 16);
  45. vni[1] = (__force __u8)(tun_id >> 8);
  46. vni[2] = (__force __u8)tun_id;
  47. #else
  48. vni[0] = (__force __u8)((__force u64)tun_id >> 40);
  49. vni[1] = (__force __u8)((__force u64)tun_id >> 48);
  50. vni[2] = (__force __u8)((__force u64)tun_id >> 56);
  51. #endif
  52. }
  53. /* Convert 24 bit VNI to 64 bit tunnel ID. */
  54. static __be64 vni_to_tunnel_id(const __u8 *vni)
  55. {
  56. #ifdef __BIG_ENDIAN
  57. return (vni[0] << 16) | (vni[1] << 8) | vni[2];
  58. #else
  59. return (__force __be64)(((__force u64)vni[0] << 40) |
  60. ((__force u64)vni[1] << 48) |
  61. ((__force u64)vni[2] << 56));
  62. #endif
  63. }
  64. static void geneve_rcv(struct geneve_sock *gs, struct sk_buff *skb)
  65. {
  66. struct vport *vport = gs->rcv_data;
  67. struct genevehdr *geneveh = geneve_hdr(skb);
  68. int opts_len;
  69. struct ovs_tunnel_info tun_info;
  70. __be64 key;
  71. __be16 flags;
  72. opts_len = geneveh->opt_len * 4;
  73. flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
  74. (udp_hdr(skb)->check != 0 ? TUNNEL_CSUM : 0) |
  75. (geneveh->oam ? TUNNEL_OAM : 0) |
  76. (geneveh->critical ? TUNNEL_CRIT_OPT : 0);
  77. key = vni_to_tunnel_id(geneveh->vni);
  78. ovs_flow_tun_info_init(&tun_info, ip_hdr(skb),
  79. udp_hdr(skb)->source, udp_hdr(skb)->dest,
  80. key, flags,
  81. geneveh->options, opts_len);
  82. ovs_vport_receive(vport, skb, &tun_info);
  83. }
  84. static int geneve_get_options(const struct vport *vport,
  85. struct sk_buff *skb)
  86. {
  87. struct geneve_port *geneve_port = geneve_vport(vport);
  88. struct inet_sock *sk = inet_sk(geneve_port->gs->sock->sk);
  89. if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(sk->inet_sport)))
  90. return -EMSGSIZE;
  91. return 0;
  92. }
  93. static void geneve_tnl_destroy(struct vport *vport)
  94. {
  95. struct geneve_port *geneve_port = geneve_vport(vport);
  96. geneve_sock_release(geneve_port->gs);
  97. ovs_vport_deferred_free(vport);
  98. }
  99. static struct vport *geneve_tnl_create(const struct vport_parms *parms)
  100. {
  101. struct net *net = ovs_dp_get_net(parms->dp);
  102. struct nlattr *options = parms->options;
  103. struct geneve_port *geneve_port;
  104. struct geneve_sock *gs;
  105. struct vport *vport;
  106. struct nlattr *a;
  107. int err;
  108. u16 dst_port;
  109. if (!options) {
  110. err = -EINVAL;
  111. goto error;
  112. }
  113. a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
  114. if (a && nla_len(a) == sizeof(u16)) {
  115. dst_port = nla_get_u16(a);
  116. } else {
  117. /* Require destination port from userspace. */
  118. err = -EINVAL;
  119. goto error;
  120. }
  121. vport = ovs_vport_alloc(sizeof(struct geneve_port),
  122. &ovs_geneve_vport_ops, parms);
  123. if (IS_ERR(vport))
  124. return vport;
  125. geneve_port = geneve_vport(vport);
  126. strncpy(geneve_port->name, parms->name, IFNAMSIZ);
  127. gs = geneve_sock_add(net, htons(dst_port), geneve_rcv, vport, true, 0);
  128. if (IS_ERR(gs)) {
  129. ovs_vport_free(vport);
  130. return (void *)gs;
  131. }
  132. geneve_port->gs = gs;
  133. return vport;
  134. error:
  135. return ERR_PTR(err);
  136. }
  137. static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb)
  138. {
  139. const struct ovs_key_ipv4_tunnel *tun_key;
  140. struct ovs_tunnel_info *tun_info;
  141. struct net *net = ovs_dp_get_net(vport->dp);
  142. struct geneve_port *geneve_port = geneve_vport(vport);
  143. __be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport;
  144. __be16 sport;
  145. struct rtable *rt;
  146. struct flowi4 fl;
  147. u8 vni[3], opts_len, *opts;
  148. __be16 df;
  149. int err;
  150. tun_info = OVS_CB(skb)->egress_tun_info;
  151. if (unlikely(!tun_info)) {
  152. err = -EINVAL;
  153. goto error;
  154. }
  155. tun_key = &tun_info->tunnel;
  156. rt = ovs_tunnel_route_lookup(net, tun_key, skb->mark, &fl, IPPROTO_UDP);
  157. if (IS_ERR(rt)) {
  158. err = PTR_ERR(rt);
  159. goto error;
  160. }
  161. df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
  162. sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
  163. tunnel_id_to_vni(tun_key->tun_id, vni);
  164. skb->ignore_df = 1;
  165. if (tun_key->tun_flags & TUNNEL_GENEVE_OPT) {
  166. opts = (u8 *)tun_info->options;
  167. opts_len = tun_info->options_len;
  168. } else {
  169. opts = NULL;
  170. opts_len = 0;
  171. }
  172. err = geneve_xmit_skb(geneve_port->gs, rt, skb, fl.saddr,
  173. tun_key->ipv4_dst, tun_key->ipv4_tos,
  174. tun_key->ipv4_ttl, df, sport, dport,
  175. tun_key->tun_flags, vni, opts_len, opts,
  176. !!(tun_key->tun_flags & TUNNEL_CSUM), false);
  177. if (err < 0)
  178. ip_rt_put(rt);
  179. return err;
  180. error:
  181. kfree_skb(skb);
  182. return err;
  183. }
  184. static const char *geneve_get_name(const struct vport *vport)
  185. {
  186. struct geneve_port *geneve_port = geneve_vport(vport);
  187. return geneve_port->name;
  188. }
  189. static int geneve_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
  190. struct ovs_tunnel_info *egress_tun_info)
  191. {
  192. struct geneve_port *geneve_port = geneve_vport(vport);
  193. struct net *net = ovs_dp_get_net(vport->dp);
  194. __be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport;
  195. __be16 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
  196. /* Get tp_src and tp_dst, refert to geneve_build_header().
  197. */
  198. return ovs_tunnel_get_egress_info(egress_tun_info,
  199. ovs_dp_get_net(vport->dp),
  200. OVS_CB(skb)->egress_tun_info,
  201. IPPROTO_UDP, skb->mark, sport, dport);
  202. }
  203. static struct vport_ops ovs_geneve_vport_ops = {
  204. .type = OVS_VPORT_TYPE_GENEVE,
  205. .create = geneve_tnl_create,
  206. .destroy = geneve_tnl_destroy,
  207. .get_name = geneve_get_name,
  208. .get_options = geneve_get_options,
  209. .send = geneve_tnl_send,
  210. .owner = THIS_MODULE,
  211. .get_egress_tun_info = geneve_get_egress_tun_info,
  212. };
  213. static int __init ovs_geneve_tnl_init(void)
  214. {
  215. return ovs_vport_ops_register(&ovs_geneve_vport_ops);
  216. }
  217. static void __exit ovs_geneve_tnl_exit(void)
  218. {
  219. ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
  220. }
  221. module_init(ovs_geneve_tnl_init);
  222. module_exit(ovs_geneve_tnl_exit);
  223. MODULE_DESCRIPTION("OVS: Geneve swiching port");
  224. MODULE_LICENSE("GPL");
  225. MODULE_ALIAS("vport-type-5");