aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@davemloft.net>2013-08-20 03:16:47 -0400
committerDavid S. Miller <davem@davemloft.net>2013-08-20 03:16:47 -0400
commit563b44951e76bd23ba3fabcc3a0c708d8f59c5fb (patch)
treeedf1ffa351402de61a4fe26e4443872c2d7b1dcb
parent7559fb2fc547b3507ba462c6558e291fb21b9cee (diff)
parent58264848a5a7b91195f43c4729072e8cc980288d (diff)
Merge branch 'openvswitch_vxlan'
Pravin B Shelar says: ==================== openvswitch: VXLAN tunneling. First four vxlan patches extends vxlan so that openvswitch can share vxlan recv code. Rest of patches refactors vxlan data plane so that ovs can share that code with vxlan module. Last patch adds vxlan-vport to openvswitch. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/vxlan.c342
-rw-r--r--include/net/vxlan.h39
-rw-r--r--include/uapi/linux/openvswitch.h11
-rw-r--r--net/openvswitch/Kconfig13
-rw-r--r--net/openvswitch/Makefile4
-rw-r--r--net/openvswitch/vport-vxlan.c204
-rw-r--r--net/openvswitch/vport.c3
-rw-r--r--net/openvswitch/vport.h1
8 files changed, 477 insertions, 140 deletions
diff --git a/drivers/net/vxlan.c b/drivers/net/vxlan.c
index 570ad7aa7204..b9401b53b699 100644
--- a/drivers/net/vxlan.c
+++ b/drivers/net/vxlan.c
@@ -27,6 +27,7 @@
27#include <linux/igmp.h> 27#include <linux/igmp.h>
28#include <linux/etherdevice.h> 28#include <linux/etherdevice.h>
29#include <linux/if_ether.h> 29#include <linux/if_ether.h>
30#include <linux/if_vlan.h>
30#include <linux/hash.h> 31#include <linux/hash.h>
31#include <linux/ethtool.h> 32#include <linux/ethtool.h>
32#include <net/arp.h> 33#include <net/arp.h>
@@ -41,6 +42,7 @@
41#include <net/inet_ecn.h> 42#include <net/inet_ecn.h>
42#include <net/net_namespace.h> 43#include <net/net_namespace.h>
43#include <net/netns/generic.h> 44#include <net/netns/generic.h>
45#include <net/vxlan.h>
44 46
45#define VXLAN_VERSION "0.1" 47#define VXLAN_VERSION "0.1"
46 48
@@ -57,6 +59,7 @@
57#define VXLAN_VID_MASK (VXLAN_N_VID - 1) 59#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
58/* IP header + UDP + VXLAN + Ethernet header */ 60/* IP header + UDP + VXLAN + Ethernet header */
59#define VXLAN_HEADROOM (20 + 8 + 8 + 14) 61#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
62#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
60 63
61#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */ 64#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62 65
@@ -82,16 +85,6 @@ static int vxlan_net_id;
82 85
83static const u8 all_zeros_mac[ETH_ALEN]; 86static const u8 all_zeros_mac[ETH_ALEN];
84 87
85/* per UDP socket information */
86struct vxlan_sock {
87 struct hlist_node hlist;
88 struct rcu_head rcu;
89 struct work_struct del_work;
90 atomic_t refcnt;
91 struct socket *sock;
92 struct hlist_head vni_list[VNI_HASH_SIZE];
93};
94
95/* per-network namespace private data for this module */ 88/* per-network namespace private data for this module */
96struct vxlan_net { 89struct vxlan_net {
97 struct list_head vxlan_list; 90 struct list_head vxlan_list;
@@ -188,7 +181,7 @@ static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
188} 181}
189 182
190/* Find VXLAN socket based on network namespace and UDP port */ 183/* Find VXLAN socket based on network namespace and UDP port */
191static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port) 184static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
192{ 185{
193 struct vxlan_sock *vs; 186 struct vxlan_sock *vs;
194 187
@@ -199,16 +192,10 @@ static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
199 return NULL; 192 return NULL;
200} 193}
201 194
202/* Look up VNI in a per net namespace table */ 195static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
203static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
204{ 196{
205 struct vxlan_sock *vs;
206 struct vxlan_dev *vxlan; 197 struct vxlan_dev *vxlan;
207 198
208 vs = vxlan_find_port(net, port);
209 if (!vs)
210 return NULL;
211
212 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) { 199 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
213 if (vxlan->default_dst.remote_vni == id) 200 if (vxlan->default_dst.remote_vni == id)
214 return vxlan; 201 return vxlan;
@@ -217,6 +204,18 @@ static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
217 return NULL; 204 return NULL;
218} 205}
219 206
207/* Look up VNI in a per net namespace table */
208static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
209{
210 struct vxlan_sock *vs;
211
212 vs = vxlan_find_sock(net, port);
213 if (!vs)
214 return NULL;
215
216 return vxlan_vs_find_vni(vs, id);
217}
218
220/* Fill in neighbour message in skbuff. */ 219/* Fill in neighbour message in skbuff. */
221static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan, 220static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
222 const struct vxlan_fdb *fdb, 221 const struct vxlan_fdb *fdb,
@@ -802,8 +801,10 @@ static void vxlan_sock_hold(struct vxlan_sock *vs)
802 atomic_inc(&vs->refcnt); 801 atomic_inc(&vs->refcnt);
803} 802}
804 803
805static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs) 804void vxlan_sock_release(struct vxlan_sock *vs)
806{ 805{
806 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
807
807 if (!atomic_dec_and_test(&vs->refcnt)) 808 if (!atomic_dec_and_test(&vs->refcnt))
808 return; 809 return;
809 810
@@ -813,6 +814,7 @@ static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
813 814
814 queue_work(vxlan_wq, &vs->del_work); 815 queue_work(vxlan_wq, &vs->del_work);
815} 816}
817EXPORT_SYMBOL_GPL(vxlan_sock_release);
816 818
817/* Callback to update multicast group membership when first VNI on 819/* Callback to update multicast group membership when first VNI on
818 * multicast asddress is brought up 820 * multicast asddress is brought up
@@ -821,7 +823,6 @@ static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
821static void vxlan_igmp_join(struct work_struct *work) 823static void vxlan_igmp_join(struct work_struct *work)
822{ 824{
823 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join); 825 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
824 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
825 struct vxlan_sock *vs = vxlan->vn_sock; 826 struct vxlan_sock *vs = vxlan->vn_sock;
826 struct sock *sk = vs->sock->sk; 827 struct sock *sk = vs->sock->sk;
827 struct ip_mreqn mreq = { 828 struct ip_mreqn mreq = {
@@ -833,7 +834,7 @@ static void vxlan_igmp_join(struct work_struct *work)
833 ip_mc_join_group(sk, &mreq); 834 ip_mc_join_group(sk, &mreq);
834 release_sock(sk); 835 release_sock(sk);
835 836
836 vxlan_sock_release(vn, vs); 837 vxlan_sock_release(vs);
837 dev_put(vxlan->dev); 838 dev_put(vxlan->dev);
838} 839}
839 840
@@ -841,7 +842,6 @@ static void vxlan_igmp_join(struct work_struct *work)
841static void vxlan_igmp_leave(struct work_struct *work) 842static void vxlan_igmp_leave(struct work_struct *work)
842{ 843{
843 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave); 844 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
844 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
845 struct vxlan_sock *vs = vxlan->vn_sock; 845 struct vxlan_sock *vs = vxlan->vn_sock;
846 struct sock *sk = vs->sock->sk; 846 struct sock *sk = vs->sock->sk;
847 struct ip_mreqn mreq = { 847 struct ip_mreqn mreq = {
@@ -853,30 +853,23 @@ static void vxlan_igmp_leave(struct work_struct *work)
853 ip_mc_leave_group(sk, &mreq); 853 ip_mc_leave_group(sk, &mreq);
854 release_sock(sk); 854 release_sock(sk);
855 855
856 vxlan_sock_release(vn, vs); 856 vxlan_sock_release(vs);
857 dev_put(vxlan->dev); 857 dev_put(vxlan->dev);
858} 858}
859 859
860/* Callback from net/ipv4/udp.c to receive packets */ 860/* Callback from net/ipv4/udp.c to receive packets */
861static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 861static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
862{ 862{
863 struct iphdr *oip; 863 struct vxlan_sock *vs;
864 struct vxlanhdr *vxh; 864 struct vxlanhdr *vxh;
865 struct vxlan_dev *vxlan;
866 struct pcpu_tstats *stats;
867 __be16 port; 865 __be16 port;
868 __u32 vni;
869 int err;
870
871 /* pop off outer UDP header */
872 __skb_pull(skb, sizeof(struct udphdr));
873 866
874 /* Need Vxlan and inner Ethernet header to be present */ 867 /* Need Vxlan and inner Ethernet header to be present */
875 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr))) 868 if (!pskb_may_pull(skb, VXLAN_HLEN))
876 goto error; 869 goto error;
877 870
878 /* Drop packets with reserved bits set */ 871 /* Return packets with reserved bits set */
879 vxh = (struct vxlanhdr *) skb->data; 872 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
880 if (vxh->vx_flags != htonl(VXLAN_FLAGS) || 873 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
881 (vxh->vx_vni & htonl(0xff))) { 874 (vxh->vx_vni & htonl(0xff))) {
882 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n", 875 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
@@ -884,28 +877,44 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
884 goto error; 877 goto error;
885 } 878 }
886 879
887 __skb_pull(skb, sizeof(struct vxlanhdr)); 880 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
881 goto drop;
888 882
889 /* Is this VNI defined? */
890 vni = ntohl(vxh->vx_vni) >> 8;
891 port = inet_sk(sk)->inet_sport; 883 port = inet_sk(sk)->inet_sport;
892 vxlan = vxlan_find_vni(sock_net(sk), vni, port); 884
893 if (!vxlan) { 885 vs = vxlan_find_sock(sock_net(sk), port);
894 netdev_dbg(skb->dev, "unknown vni %d port %u\n", 886 if (!vs)
895 vni, ntohs(port));
896 goto drop; 887 goto drop;
897 }
898 888
899 if (!pskb_may_pull(skb, ETH_HLEN)) { 889 vs->rcv(vs, skb, vxh->vx_vni);
900 vxlan->dev->stats.rx_length_errors++; 890 return 0;
901 vxlan->dev->stats.rx_errors++; 891
892drop:
893 /* Consume bad packet */
894 kfree_skb(skb);
895 return 0;
896
897error:
898 /* Return non vxlan pkt */
899 return 1;
900}
901
902static void vxlan_rcv(struct vxlan_sock *vs,
903 struct sk_buff *skb, __be32 vx_vni)
904{
905 struct iphdr *oip;
906 struct vxlan_dev *vxlan;
907 struct pcpu_tstats *stats;
908 __u32 vni;
909 int err;
910
911 vni = ntohl(vx_vni) >> 8;
912 /* Is this VNI defined? */
913 vxlan = vxlan_vs_find_vni(vs, vni);
914 if (!vxlan)
902 goto drop; 915 goto drop;
903 }
904 916
905 skb_reset_mac_header(skb); 917 skb_reset_mac_header(skb);
906
907 /* Re-examine inner Ethernet packet */
908 oip = ip_hdr(skb);
909 skb->protocol = eth_type_trans(skb, vxlan->dev); 918 skb->protocol = eth_type_trans(skb, vxlan->dev);
910 919
911 /* Ignore packet loops (and multicast echo) */ 920 /* Ignore packet loops (and multicast echo) */
@@ -913,11 +922,12 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
913 vxlan->dev->dev_addr) == 0) 922 vxlan->dev->dev_addr) == 0)
914 goto drop; 923 goto drop;
915 924
925 /* Re-examine inner Ethernet packet */
926 oip = ip_hdr(skb);
916 if ((vxlan->flags & VXLAN_F_LEARN) && 927 if ((vxlan->flags & VXLAN_F_LEARN) &&
917 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source)) 928 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
918 goto drop; 929 goto drop;
919 930
920 __skb_tunnel_rx(skb, vxlan->dev);
921 skb_reset_network_header(skb); 931 skb_reset_network_header(skb);
922 932
923 /* If the NIC driver gave us an encapsulated packet with 933 /* If the NIC driver gave us an encapsulated packet with
@@ -951,16 +961,10 @@ static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
951 961
952 netif_rx(skb); 962 netif_rx(skb);
953 963
954 return 0; 964 return;
955error:
956 /* Put UDP header back */
957 __skb_push(skb, sizeof(struct udphdr));
958
959 return 1;
960drop: 965drop:
961 /* Consume bad packet */ 966 /* Consume bad packet */
962 kfree_skb(skb); 967 kfree_skb(skb);
963 return 0;
964} 968}
965 969
966static int arp_reduce(struct net_device *dev, struct sk_buff *skb) 970static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
@@ -1079,11 +1083,8 @@ static void vxlan_sock_put(struct sk_buff *skb)
1079} 1083}
1080 1084
1081/* On transmit, associate with the tunnel socket */ 1085/* On transmit, associate with the tunnel socket */
1082static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb) 1086static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
1083{ 1087{
1084 struct vxlan_dev *vxlan = netdev_priv(dev);
1085 struct sock *sk = vxlan->vn_sock->sock->sk;
1086
1087 skb_orphan(skb); 1088 skb_orphan(skb);
1088 sock_hold(sk); 1089 sock_hold(sk);
1089 skb->sk = sk; 1090 skb->sk = sk;
@@ -1095,9 +1096,9 @@ static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
1095 * better and maybe available from hardware 1096 * better and maybe available from hardware
1096 * secondary choice is to use jhash on the Ethernet header 1097 * secondary choice is to use jhash on the Ethernet header
1097 */ 1098 */
1098static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb) 1099__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
1099{ 1100{
1100 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1; 1101 unsigned int range = (port_max - port_min) + 1;
1101 u32 hash; 1102 u32 hash;
1102 1103
1103 hash = skb_get_rxhash(skb); 1104 hash = skb_get_rxhash(skb);
@@ -1105,8 +1106,9 @@ static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
1105 hash = jhash(skb->data, 2 * ETH_ALEN, 1106 hash = jhash(skb->data, 2 * ETH_ALEN,
1106 (__force u32) skb->protocol); 1107 (__force u32) skb->protocol);
1107 1108
1108 return htons((((u64) hash * range) >> 32) + vxlan->port_min); 1109 return htons((((u64) hash * range) >> 32) + port_min);
1109} 1110}
1111EXPORT_SYMBOL_GPL(vxlan_src_port);
1110 1112
1111static int handle_offloads(struct sk_buff *skb) 1113static int handle_offloads(struct sk_buff *skb)
1112{ 1114{
@@ -1122,6 +1124,64 @@ static int handle_offloads(struct sk_buff *skb)
1122 return 0; 1124 return 0;
1123} 1125}
1124 1126
1127int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
1128 struct rtable *rt, struct sk_buff *skb,
1129 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1130 __be16 src_port, __be16 dst_port, __be32 vni)
1131{
1132 struct vxlanhdr *vxh;
1133 struct udphdr *uh;
1134 int min_headroom;
1135 int err;
1136
1137 if (!skb->encapsulation) {
1138 skb_reset_inner_headers(skb);
1139 skb->encapsulation = 1;
1140 }
1141
1142 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1143 + VXLAN_HLEN + sizeof(struct iphdr)
1144 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1145
1146 /* Need space for new headers (invalidates iph ptr) */
1147 err = skb_cow_head(skb, min_headroom);
1148 if (unlikely(err))
1149 return err;
1150
1151 if (vlan_tx_tag_present(skb)) {
1152 if (WARN_ON(!__vlan_put_tag(skb,
1153 skb->vlan_proto,
1154 vlan_tx_tag_get(skb))))
1155 return -ENOMEM;
1156
1157 skb->vlan_tci = 0;
1158 }
1159
1160 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1161 vxh->vx_flags = htonl(VXLAN_FLAGS);
1162 vxh->vx_vni = vni;
1163
1164 __skb_push(skb, sizeof(*uh));
1165 skb_reset_transport_header(skb);
1166 uh = udp_hdr(skb);
1167
1168 uh->dest = dst_port;
1169 uh->source = src_port;
1170
1171 uh->len = htons(skb->len);
1172 uh->check = 0;
1173
1174 vxlan_set_owner(vs->sock->sk, skb);
1175
1176 err = handle_offloads(skb);
1177 if (err)
1178 return err;
1179
1180 return iptunnel_xmit(net, rt, skb, src, dst,
1181 IPPROTO_UDP, tos, ttl, df);
1182}
1183EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1184
1125/* Bypass encapsulation if the destination is local */ 1185/* Bypass encapsulation if the destination is local */
1126static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan, 1186static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1127 struct vxlan_dev *dst_vxlan) 1187 struct vxlan_dev *dst_vxlan)
@@ -1159,8 +1219,6 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1159 struct vxlan_dev *vxlan = netdev_priv(dev); 1219 struct vxlan_dev *vxlan = netdev_priv(dev);
1160 struct rtable *rt; 1220 struct rtable *rt;
1161 const struct iphdr *old_iph; 1221 const struct iphdr *old_iph;
1162 struct vxlanhdr *vxh;
1163 struct udphdr *uh;
1164 struct flowi4 fl4; 1222 struct flowi4 fl4;
1165 __be32 dst; 1223 __be32 dst;
1166 __be16 src_port, dst_port; 1224 __be16 src_port, dst_port;
@@ -1182,15 +1240,6 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1182 goto drop; 1240 goto drop;
1183 } 1241 }
1184 1242
1185 if (!skb->encapsulation) {
1186 skb_reset_inner_headers(skb);
1187 skb->encapsulation = 1;
1188 }
1189
1190 /* Need space for new headers (invalidates iph ptr) */
1191 if (skb_cow_head(skb, VXLAN_HEADROOM))
1192 goto drop;
1193
1194 old_iph = ip_hdr(skb); 1243 old_iph = ip_hdr(skb);
1195 1244
1196 ttl = vxlan->ttl; 1245 ttl = vxlan->ttl;
@@ -1201,7 +1250,7 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1201 if (tos == 1) 1250 if (tos == 1)
1202 tos = ip_tunnel_get_dsfield(old_iph, skb); 1251 tos = ip_tunnel_get_dsfield(old_iph, skb);
1203 1252
1204 src_port = vxlan_src_port(vxlan, skb); 1253 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
1205 1254
1206 memset(&fl4, 0, sizeof(fl4)); 1255 memset(&fl4, 0, sizeof(fl4));
1207 fl4.flowi4_oif = rdst->remote_ifindex; 1256 fl4.flowi4_oif = rdst->remote_ifindex;
@@ -1218,9 +1267,8 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1218 1267
1219 if (rt->dst.dev == dev) { 1268 if (rt->dst.dev == dev) {
1220 netdev_dbg(dev, "circular route to %pI4\n", &dst); 1269 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1221 ip_rt_put(rt);
1222 dev->stats.collisions++; 1270 dev->stats.collisions++;
1223 goto tx_error; 1271 goto rt_tx_error;
1224 } 1272 }
1225 1273
1226 /* Bypass encapsulation if the destination is local */ 1274 /* Bypass encapsulation if the destination is local */
@@ -1235,30 +1283,16 @@ static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1235 vxlan_encap_bypass(skb, vxlan, dst_vxlan); 1283 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1236 return; 1284 return;
1237 } 1285 }
1238 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1239 vxh->vx_flags = htonl(VXLAN_FLAGS);
1240 vxh->vx_vni = htonl(vni << 8);
1241
1242 __skb_push(skb, sizeof(*uh));
1243 skb_reset_transport_header(skb);
1244 uh = udp_hdr(skb);
1245
1246 uh->dest = dst_port;
1247 uh->source = src_port;
1248
1249 uh->len = htons(skb->len);
1250 uh->check = 0;
1251
1252 vxlan_set_owner(dev, skb);
1253
1254 if (handle_offloads(skb))
1255 goto drop;
1256 1286
1257 tos = ip_tunnel_ecn_encap(tos, old_iph, skb); 1287 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1258 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst); 1288 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1259 1289
1260 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst, 1290 err = vxlan_xmit_skb(dev_net(dev), vxlan->vn_sock, rt, skb,
1261 IPPROTO_UDP, tos, ttl, df); 1291 fl4.saddr, dst, tos, ttl, df,
1292 src_port, dst_port, htonl(vni << 8));
1293
1294 if (err < 0)
1295 goto rt_tx_error;
1262 iptunnel_xmit_stats(err, &dev->stats, dev->tstats); 1296 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1263 1297
1264 return; 1298 return;
@@ -1267,6 +1301,8 @@ drop:
1267 dev->stats.tx_dropped++; 1301 dev->stats.tx_dropped++;
1268 goto tx_free; 1302 goto tx_free;
1269 1303
1304rt_tx_error:
1305 ip_rt_put(rt);
1270tx_error: 1306tx_error:
1271 dev->stats.tx_errors++; 1307 dev->stats.tx_errors++;
1272tx_free: 1308tx_free:
@@ -1365,25 +1401,31 @@ static void vxlan_cleanup(unsigned long arg)
1365 mod_timer(&vxlan->age_timer, next_timer); 1401 mod_timer(&vxlan->age_timer, next_timer);
1366} 1402}
1367 1403
1404static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1405{
1406 __u32 vni = vxlan->default_dst.remote_vni;
1407
1408 vxlan->vn_sock = vs;
1409 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1410}
1411
1368/* Setup stats when device is created */ 1412/* Setup stats when device is created */
1369static int vxlan_init(struct net_device *dev) 1413static int vxlan_init(struct net_device *dev)
1370{ 1414{
1371 struct vxlan_dev *vxlan = netdev_priv(dev); 1415 struct vxlan_dev *vxlan = netdev_priv(dev);
1372 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id); 1416 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1373 struct vxlan_sock *vs; 1417 struct vxlan_sock *vs;
1374 __u32 vni = vxlan->default_dst.remote_vni;
1375 1418
1376 dev->tstats = alloc_percpu(struct pcpu_tstats); 1419 dev->tstats = alloc_percpu(struct pcpu_tstats);
1377 if (!dev->tstats) 1420 if (!dev->tstats)
1378 return -ENOMEM; 1421 return -ENOMEM;
1379 1422
1380 spin_lock(&vn->sock_lock); 1423 spin_lock(&vn->sock_lock);
1381 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port); 1424 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
1382 if (vs) { 1425 if (vs) {
1383 /* If we have a socket with same port already, reuse it */ 1426 /* If we have a socket with same port already, reuse it */
1384 atomic_inc(&vs->refcnt); 1427 atomic_inc(&vs->refcnt);
1385 vxlan->vn_sock = vs; 1428 vxlan_vs_add_dev(vs, vxlan);
1386 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1387 } else { 1429 } else {
1388 /* otherwise make new socket outside of RTNL */ 1430 /* otherwise make new socket outside of RTNL */
1389 dev_hold(dev); 1431 dev_hold(dev);
@@ -1408,13 +1450,12 @@ static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
1408static void vxlan_uninit(struct net_device *dev) 1450static void vxlan_uninit(struct net_device *dev)
1409{ 1451{
1410 struct vxlan_dev *vxlan = netdev_priv(dev); 1452 struct vxlan_dev *vxlan = netdev_priv(dev);
1411 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1412 struct vxlan_sock *vs = vxlan->vn_sock; 1453 struct vxlan_sock *vs = vxlan->vn_sock;
1413 1454
1414 vxlan_fdb_delete_default(vxlan); 1455 vxlan_fdb_delete_default(vxlan);
1415 1456
1416 if (vs) 1457 if (vs)
1417 vxlan_sock_release(vn, vs); 1458 vxlan_sock_release(vs);
1418 free_percpu(dev->tstats); 1459 free_percpu(dev->tstats);
1419} 1460}
1420 1461
@@ -1530,8 +1571,11 @@ static void vxlan_setup(struct net_device *dev)
1530 dev->features |= NETIF_F_RXCSUM; 1571 dev->features |= NETIF_F_RXCSUM;
1531 dev->features |= NETIF_F_GSO_SOFTWARE; 1572 dev->features |= NETIF_F_GSO_SOFTWARE;
1532 1573
1574 dev->vlan_features = dev->features;
1575 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1533 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM; 1576 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
1534 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 1577 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1578 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
1535 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE; 1579 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1536 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1580 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1537 1581
@@ -1631,8 +1675,10 @@ static void vxlan_del_work(struct work_struct *work)
1631 kfree_rcu(vs, rcu); 1675 kfree_rcu(vs, rcu);
1632} 1676}
1633 1677
1634static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port) 1678static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
1679 vxlan_rcv_t *rcv, void *data)
1635{ 1680{
1681 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1636 struct vxlan_sock *vs; 1682 struct vxlan_sock *vs;
1637 struct sock *sk; 1683 struct sock *sk;
1638 struct sockaddr_in vxlan_addr = { 1684 struct sockaddr_in vxlan_addr = {
@@ -1644,8 +1690,10 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1644 unsigned int h; 1690 unsigned int h;
1645 1691
1646 vs = kmalloc(sizeof(*vs), GFP_KERNEL); 1692 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1647 if (!vs) 1693 if (!vs) {
1694 pr_debug("memory alocation failure\n");
1648 return ERR_PTR(-ENOMEM); 1695 return ERR_PTR(-ENOMEM);
1696 }
1649 1697
1650 for (h = 0; h < VNI_HASH_SIZE; ++h) 1698 for (h = 0; h < VNI_HASH_SIZE; ++h)
1651 INIT_HLIST_HEAD(&vs->vni_list[h]); 1699 INIT_HLIST_HEAD(&vs->vni_list[h]);
@@ -1673,57 +1721,70 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1673 kfree(vs); 1721 kfree(vs);
1674 return ERR_PTR(rc); 1722 return ERR_PTR(rc);
1675 } 1723 }
1724 atomic_set(&vs->refcnt, 1);
1725 vs->rcv = rcv;
1726 vs->data = data;
1676 1727
1677 /* Disable multicast loopback */ 1728 /* Disable multicast loopback */
1678 inet_sk(sk)->mc_loop = 0; 1729 inet_sk(sk)->mc_loop = 0;
1730 spin_lock(&vn->sock_lock);
1731 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
1732 spin_unlock(&vn->sock_lock);
1679 1733
1680 /* Mark socket as an encapsulation socket. */ 1734 /* Mark socket as an encapsulation socket. */
1681 udp_sk(sk)->encap_type = 1; 1735 udp_sk(sk)->encap_type = 1;
1682 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv; 1736 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1683 udp_encap_enable(); 1737 udp_encap_enable();
1684 atomic_set(&vs->refcnt, 1); 1738 return vs;
1739}
1740
1741struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
1742 vxlan_rcv_t *rcv, void *data,
1743 bool no_share)
1744{
1745 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1746 struct vxlan_sock *vs;
1747
1748 vs = vxlan_socket_create(net, port, rcv, data);
1749 if (!IS_ERR(vs))
1750 return vs;
1751
1752 if (no_share) /* Return error if sharing is not allowed. */
1753 return vs;
1754
1755 spin_lock(&vn->sock_lock);
1756 vs = vxlan_find_sock(net, port);
1757 if (vs) {
1758 if (vs->rcv == rcv)
1759 atomic_inc(&vs->refcnt);
1760 else
1761 vs = ERR_PTR(-EBUSY);
1762 }
1763 spin_unlock(&vn->sock_lock);
1764
1765 if (!vs)
1766 vs = ERR_PTR(-EINVAL);
1685 1767
1686 return vs; 1768 return vs;
1687} 1769}
1770EXPORT_SYMBOL_GPL(vxlan_sock_add);
1688 1771
1689/* Scheduled at device creation to bind to a socket */ 1772/* Scheduled at device creation to bind to a socket */
1690static void vxlan_sock_work(struct work_struct *work) 1773static void vxlan_sock_work(struct work_struct *work)
1691{ 1774{
1692 struct vxlan_dev *vxlan 1775 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
1693 = container_of(work, struct vxlan_dev, sock_work); 1776 struct net *net = dev_net(vxlan->dev);
1694 struct net_device *dev = vxlan->dev;
1695 struct net *net = dev_net(dev);
1696 __u32 vni = vxlan->default_dst.remote_vni;
1697 __be16 port = vxlan->dst_port;
1698 struct vxlan_net *vn = net_generic(net, vxlan_net_id); 1777 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1699 struct vxlan_sock *nvs, *ovs; 1778 __be16 port = vxlan->dst_port;
1700 1779 struct vxlan_sock *nvs;
1701 nvs = vxlan_socket_create(net, port);
1702 if (IS_ERR(nvs)) {
1703 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1704 PTR_ERR(nvs));
1705 goto out;
1706 }
1707 1780
1781 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
1708 spin_lock(&vn->sock_lock); 1782 spin_lock(&vn->sock_lock);
1709 /* Look again to see if can reuse socket */ 1783 if (!IS_ERR(nvs))
1710 ovs = vxlan_find_port(net, port); 1784 vxlan_vs_add_dev(nvs, vxlan);
1711 if (ovs) { 1785 spin_unlock(&vn->sock_lock);
1712 atomic_inc(&ovs->refcnt); 1786
1713 vxlan->vn_sock = ovs; 1787 dev_put(vxlan->dev);
1714 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1715 spin_unlock(&vn->sock_lock);
1716
1717 sk_release_kernel(nvs->sock->sk);
1718 kfree(nvs);
1719 } else {
1720 vxlan->vn_sock = nvs;
1721 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1722 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1723 spin_unlock(&vn->sock_lock);
1724 }
1725out:
1726 dev_put(dev);
1727} 1788}
1728 1789
1729static int vxlan_newlink(struct net *net, struct net_device *dev, 1790static int vxlan_newlink(struct net *net, struct net_device *dev,
@@ -1838,7 +1899,8 @@ static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1838 struct vxlan_dev *vxlan = netdev_priv(dev); 1899 struct vxlan_dev *vxlan = netdev_priv(dev);
1839 1900
1840 spin_lock(&vn->sock_lock); 1901 spin_lock(&vn->sock_lock);
1841 hlist_del_rcu(&vxlan->hlist); 1902 if (!hlist_unhashed(&vxlan->hlist))
1903 hlist_del_rcu(&vxlan->hlist);
1842 spin_unlock(&vn->sock_lock); 1904 spin_unlock(&vn->sock_lock);
1843 1905
1844 list_del(&vxlan->next); 1906 list_del(&vxlan->next);
diff --git a/include/net/vxlan.h b/include/net/vxlan.h
new file mode 100644
index 000000000000..ad342e3688a0
--- /dev/null
+++ b/include/net/vxlan.h
@@ -0,0 +1,39 @@
1#ifndef __NET_VXLAN_H
2#define __NET_VXLAN_H 1
3
4#include <linux/skbuff.h>
5#include <linux/netdevice.h>
6#include <linux/udp.h>
7
8#define VNI_HASH_BITS 10
9#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
10
11struct vxlan_sock;
12typedef void (vxlan_rcv_t)(struct vxlan_sock *vh, struct sk_buff *skb, __be32 key);
13
14/* per UDP socket information */
15struct vxlan_sock {
16 struct hlist_node hlist;
17 vxlan_rcv_t *rcv;
18 void *data;
19 struct work_struct del_work;
20 struct socket *sock;
21 struct rcu_head rcu;
22 struct hlist_head vni_list[VNI_HASH_SIZE];
23 atomic_t refcnt;
24};
25
26struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
27 vxlan_rcv_t *rcv, void *data,
28 bool no_share);
29
30void vxlan_sock_release(struct vxlan_sock *vs);
31
32int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
33 struct rtable *rt, struct sk_buff *skb,
34 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
35 __be16 src_port, __be16 dst_port, __be32 vni);
36
37__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb);
38
39#endif
diff --git a/include/uapi/linux/openvswitch.h b/include/uapi/linux/openvswitch.h
index c55efaaa9bb4..52490b0e62b5 100644
--- a/include/uapi/linux/openvswitch.h
+++ b/include/uapi/linux/openvswitch.h
@@ -165,6 +165,7 @@ enum ovs_vport_type {
165 OVS_VPORT_TYPE_NETDEV, /* network device */ 165 OVS_VPORT_TYPE_NETDEV, /* network device */
166 OVS_VPORT_TYPE_INTERNAL, /* network device implemented by datapath */ 166 OVS_VPORT_TYPE_INTERNAL, /* network device implemented by datapath */
167 OVS_VPORT_TYPE_GRE, /* GRE tunnel. */ 167 OVS_VPORT_TYPE_GRE, /* GRE tunnel. */
168 OVS_VPORT_TYPE_VXLAN, /* VXLAN tunnel. */
168 __OVS_VPORT_TYPE_MAX 169 __OVS_VPORT_TYPE_MAX
169}; 170};
170 171
@@ -211,6 +212,16 @@ enum ovs_vport_attr {
211 212
212#define OVS_VPORT_ATTR_MAX (__OVS_VPORT_ATTR_MAX - 1) 213#define OVS_VPORT_ATTR_MAX (__OVS_VPORT_ATTR_MAX - 1)
213 214
215/* OVS_VPORT_ATTR_OPTIONS attributes for tunnels.
216 */
217enum {
218 OVS_TUNNEL_ATTR_UNSPEC,
219 OVS_TUNNEL_ATTR_DST_PORT, /* 16-bit UDP port, used by L4 tunnels. */
220 __OVS_TUNNEL_ATTR_MAX
221};
222
223#define OVS_TUNNEL_ATTR_MAX (__OVS_TUNNEL_ATTR_MAX - 1)
224
214/* Flows. */ 225/* Flows. */
215 226
216#define OVS_FLOW_FAMILY "ovs_flow" 227#define OVS_FLOW_FAMILY "ovs_flow"
diff --git a/net/openvswitch/Kconfig b/net/openvswitch/Kconfig
index 27ee56b688a3..bed30e69baa7 100644
--- a/net/openvswitch/Kconfig
+++ b/net/openvswitch/Kconfig
@@ -40,3 +40,16 @@ config OPENVSWITCH_GRE
40 Say N to exclude this support and reduce the binary size. 40 Say N to exclude this support and reduce the binary size.
41 41
42 If unsure, say Y. 42 If unsure, say Y.
43
44config OPENVSWITCH_VXLAN
45 bool "Open vSwitch VXLAN tunneling support"
46 depends on INET
47 depends on OPENVSWITCH
48 depends on VXLAN && !(OPENVSWITCH=y && VXLAN=m)
49 default y
50 ---help---
51 If you say Y here, then the Open vSwitch will be able create vxlan vport.
52
53 Say N to exclude this support and reduce the binary size.
54
55 If unsure, say Y.
diff --git a/net/openvswitch/Makefile b/net/openvswitch/Makefile
index 01bddb2991e3..82e4ee54a44b 100644
--- a/net/openvswitch/Makefile
+++ b/net/openvswitch/Makefile
@@ -13,3 +13,7 @@ openvswitch-y := \
13 vport-gre.o \ 13 vport-gre.o \
14 vport-internal_dev.o \ 14 vport-internal_dev.o \
15 vport-netdev.o 15 vport-netdev.o
16
17ifneq ($(CONFIG_OPENVSWITCH_VXLAN),)
18openvswitch-y += vport-vxlan.o
19endif
diff --git a/net/openvswitch/vport-vxlan.c b/net/openvswitch/vport-vxlan.c
new file mode 100644
index 000000000000..36848bd54a77
--- /dev/null
+++ b/net/openvswitch/vport-vxlan.c
@@ -0,0 +1,204 @@
1/*
2 * Copyright (c) 2013 Nicira, Inc.
3 * Copyright (c) 2013 Cisco Systems, Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/in.h>
23#include <linux/ip.h>
24#include <linux/net.h>
25#include <linux/rculist.h>
26#include <linux/udp.h>
27
28#include <net/icmp.h>
29#include <net/ip.h>
30#include <net/udp.h>
31#include <net/ip_tunnels.h>
32#include <net/udp.h>
33#include <net/rtnetlink.h>
34#include <net/route.h>
35#include <net/dsfield.h>
36#include <net/inet_ecn.h>
37#include <net/net_namespace.h>
38#include <net/netns/generic.h>
39#include <net/vxlan.h>
40
41#include "datapath.h"
42#include "vport.h"
43
44/**
45 * struct vxlan_port - Keeps track of open UDP ports
46 * @vs: vxlan_sock created for the port.
47 * @name: vport name.
48 */
49struct vxlan_port {
50 struct vxlan_sock *vs;
51 char name[IFNAMSIZ];
52};
53
54static inline struct vxlan_port *vxlan_vport(const struct vport *vport)
55{
56 return vport_priv(vport);
57}
58
59/* Called with rcu_read_lock and BH disabled. */
60static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb, __be32 vx_vni)
61{
62 struct ovs_key_ipv4_tunnel tun_key;
63 struct vport *vport = vs->data;
64 struct iphdr *iph;
65 __be64 key;
66
67 /* Save outer tunnel values */
68 iph = ip_hdr(skb);
69 key = cpu_to_be64(ntohl(vx_vni) >> 8);
70 ovs_flow_tun_key_init(&tun_key, iph, key, TUNNEL_KEY);
71
72 ovs_vport_receive(vport, skb, &tun_key);
73}
74
75static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
76{
77 struct vxlan_port *vxlan_port = vxlan_vport(vport);
78 __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
79
80 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
81 return -EMSGSIZE;
82 return 0;
83}
84
85static void vxlan_tnl_destroy(struct vport *vport)
86{
87 struct vxlan_port *vxlan_port = vxlan_vport(vport);
88
89 vxlan_sock_release(vxlan_port->vs);
90
91 ovs_vport_deferred_free(vport);
92}
93
94static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
95{
96 struct net *net = ovs_dp_get_net(parms->dp);
97 struct nlattr *options = parms->options;
98 struct vxlan_port *vxlan_port;
99 struct vxlan_sock *vs;
100 struct vport *vport;
101 struct nlattr *a;
102 u16 dst_port;
103 int err;
104
105 if (!options) {
106 err = -EINVAL;
107 goto error;
108 }
109 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
110 if (a && nla_len(a) == sizeof(u16)) {
111 dst_port = nla_get_u16(a);
112 } else {
113 /* Require destination port from userspace. */
114 err = -EINVAL;
115 goto error;
116 }
117
118 vport = ovs_vport_alloc(sizeof(struct vxlan_port),
119 &ovs_vxlan_vport_ops, parms);
120 if (IS_ERR(vport))
121 return vport;
122
123 vxlan_port = vxlan_vport(vport);
124 strncpy(vxlan_port->name, parms->name, IFNAMSIZ);
125
126 vs = vxlan_sock_add(net, htons(dst_port), vxlan_rcv, vport, true);
127 if (IS_ERR(vs)) {
128 ovs_vport_free(vport);
129 return (void *)vs;
130 }
131 vxlan_port->vs = vs;
132
133 return vport;
134
135error:
136 return ERR_PTR(err);
137}
138
139static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
140{
141 struct net *net = ovs_dp_get_net(vport->dp);
142 struct vxlan_port *vxlan_port = vxlan_vport(vport);
143 __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport;
144 struct rtable *rt;
145 struct flowi4 fl;
146 __be16 src_port;
147 int port_min;
148 int port_max;
149 __be16 df;
150 int err;
151
152 if (unlikely(!OVS_CB(skb)->tun_key)) {
153 err = -EINVAL;
154 goto error;
155 }
156
157 /* Route lookup */
158 memset(&fl, 0, sizeof(fl));
159 fl.daddr = OVS_CB(skb)->tun_key->ipv4_dst;
160 fl.saddr = OVS_CB(skb)->tun_key->ipv4_src;
161 fl.flowi4_tos = RT_TOS(OVS_CB(skb)->tun_key->ipv4_tos);
162 fl.flowi4_mark = skb->mark;
163 fl.flowi4_proto = IPPROTO_UDP;
164
165 rt = ip_route_output_key(net, &fl);
166 if (IS_ERR(rt)) {
167 err = PTR_ERR(rt);
168 goto error;
169 }
170
171 df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
172 htons(IP_DF) : 0;
173
174 skb->local_df = 1;
175
176 inet_get_local_port_range(&port_min, &port_max);
177 src_port = vxlan_src_port(port_min, port_max, skb);
178
179 err = vxlan_xmit_skb(net, vxlan_port->vs, rt, skb,
180 fl.saddr, OVS_CB(skb)->tun_key->ipv4_dst,
181 OVS_CB(skb)->tun_key->ipv4_tos,
182 OVS_CB(skb)->tun_key->ipv4_ttl, df,
183 src_port, dst_port,
184 htonl(be64_to_cpu(OVS_CB(skb)->tun_key->tun_id) << 8));
185 if (err < 0)
186 ip_rt_put(rt);
187error:
188 return err;
189}
190
191static const char *vxlan_get_name(const struct vport *vport)
192{
193 struct vxlan_port *vxlan_port = vxlan_vport(vport);
194 return vxlan_port->name;
195}
196
197const struct vport_ops ovs_vxlan_vport_ops = {
198 .type = OVS_VPORT_TYPE_VXLAN,
199 .create = vxlan_tnl_create,
200 .destroy = vxlan_tnl_destroy,
201 .get_name = vxlan_get_name,
202 .get_options = vxlan_get_options,
203 .send = vxlan_tnl_send,
204};
diff --git a/net/openvswitch/vport.c b/net/openvswitch/vport.c
index d4c7fa04ce08..d69e0c06dfde 100644
--- a/net/openvswitch/vport.c
+++ b/net/openvswitch/vport.c
@@ -42,6 +42,9 @@ static const struct vport_ops *vport_ops_list[] = {
42#ifdef CONFIG_OPENVSWITCH_GRE 42#ifdef CONFIG_OPENVSWITCH_GRE
43 &ovs_gre_vport_ops, 43 &ovs_gre_vport_ops,
44#endif 44#endif
45#ifdef CONFIG_OPENVSWITCH_VXLAN
46 &ovs_vxlan_vport_ops,
47#endif
45}; 48};
46 49
47/* Protected by RCU read lock for reading, ovs_mutex for writing. */ 50/* Protected by RCU read lock for reading, ovs_mutex for writing. */
diff --git a/net/openvswitch/vport.h b/net/openvswitch/vport.h
index 376045c42f8b..1a9fbcec6e1b 100644
--- a/net/openvswitch/vport.h
+++ b/net/openvswitch/vport.h
@@ -199,6 +199,7 @@ void ovs_vport_record_error(struct vport *, enum vport_err_type err_type);
199extern const struct vport_ops ovs_netdev_vport_ops; 199extern const struct vport_ops ovs_netdev_vport_ops;
200extern const struct vport_ops ovs_internal_vport_ops; 200extern const struct vport_ops ovs_internal_vport_ops;
201extern const struct vport_ops ovs_gre_vport_ops; 201extern const struct vport_ops ovs_gre_vport_ops;
202extern const struct vport_ops ovs_vxlan_vport_ops;
202 203
203static inline void ovs_skb_postpush_rcsum(struct sk_buff *skb, 204static inline void ovs_skb_postpush_rcsum(struct sk_buff *skb,
204 const void *start, unsigned int len) 205 const void *start, unsigned int len)