blob: 30ab8e127288d4290b5f823a62726179c19b0f5a [file] [log] [blame]
Jesse Grossf5796682014-10-03 15:35:33 -07001/*
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
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Jesse Grossf5796682014-10-03 15:35:33 -070012#include <linux/in.h>
13#include <linux/ip.h>
14#include <linux/net.h>
15#include <linux/rculist.h>
16#include <linux/udp.h>
17#include <linux/if_vlan.h>
Thomas Graf62b9c8d2014-10-22 17:29:06 +020018#include <linux/module.h>
Jesse Grossf5796682014-10-03 15:35:33 -070019
20#include <net/geneve.h>
21#include <net/icmp.h>
22#include <net/ip.h>
23#include <net/route.h>
24#include <net/udp.h>
25#include <net/xfrm.h>
26
27#include "datapath.h"
28#include "vport.h"
Pravin B Shelar6b001e62015-08-26 23:46:53 -070029#include "vport-netdev.h"
Jesse Grossf5796682014-10-03 15:35:33 -070030
Thomas Graf62b9c8d2014-10-22 17:29:06 +020031static struct vport_ops ovs_geneve_vport_ops;
Jesse Grossf5796682014-10-03 15:35:33 -070032/**
33 * struct geneve_port - Keeps track of open UDP ports
Pravin B Shelar6b001e62015-08-26 23:46:53 -070034 * @dst_port: destination port.
Jesse Grossf5796682014-10-03 15:35:33 -070035 */
36struct geneve_port {
Jean Sacren2f7066a2016-01-09 16:07:10 -070037 u16 dst_port;
Jesse Grossf5796682014-10-03 15:35:33 -070038};
39
Jesse Grossf5796682014-10-03 15:35:33 -070040static inline struct geneve_port *geneve_vport(const struct vport *vport)
41{
42 return vport_priv(vport);
43}
44
Jesse Grossf5796682014-10-03 15:35:33 -070045static int geneve_get_options(const struct vport *vport,
46 struct sk_buff *skb)
47{
48 struct geneve_port *geneve_port = geneve_vport(vport);
Jesse Grossf5796682014-10-03 15:35:33 -070049
Jean Sacren2f7066a2016-01-09 16:07:10 -070050 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, geneve_port->dst_port))
Jesse Grossf5796682014-10-03 15:35:33 -070051 return -EMSGSIZE;
52 return 0;
53}
54
Jesse Grossf5796682014-10-03 15:35:33 -070055static struct vport *geneve_tnl_create(const struct vport_parms *parms)
56{
57 struct net *net = ovs_dp_get_net(parms->dp);
58 struct nlattr *options = parms->options;
59 struct geneve_port *geneve_port;
Pravin B Shelar6b001e62015-08-26 23:46:53 -070060 struct net_device *dev;
Jesse Grossf5796682014-10-03 15:35:33 -070061 struct vport *vport;
62 struct nlattr *a;
Jesse Grossf5796682014-10-03 15:35:33 -070063 u16 dst_port;
Pravin B Shelar6b001e62015-08-26 23:46:53 -070064 int err;
Jesse Grossf5796682014-10-03 15:35:33 -070065
66 if (!options) {
67 err = -EINVAL;
68 goto error;
69 }
70
71 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
72 if (a && nla_len(a) == sizeof(u16)) {
73 dst_port = nla_get_u16(a);
74 } else {
75 /* Require destination port from userspace. */
76 err = -EINVAL;
77 goto error;
78 }
79
80 vport = ovs_vport_alloc(sizeof(struct geneve_port),
81 &ovs_geneve_vport_ops, parms);
82 if (IS_ERR(vport))
83 return vport;
84
85 geneve_port = geneve_vport(vport);
Jean Sacren2f7066a2016-01-09 16:07:10 -070086 geneve_port->dst_port = dst_port;
Jesse Grossf5796682014-10-03 15:35:33 -070087
Pravin B Shelar6b001e62015-08-26 23:46:53 -070088 rtnl_lock();
89 dev = geneve_dev_create_fb(net, parms->name, NET_NAME_USER, dst_port);
90 if (IS_ERR(dev)) {
91 rtnl_unlock();
Jesse Grossf5796682014-10-03 15:35:33 -070092 ovs_vport_free(vport);
Pravin B Shelar6b001e62015-08-26 23:46:53 -070093 return ERR_CAST(dev);
Jesse Grossf5796682014-10-03 15:35:33 -070094 }
Jesse Grossf5796682014-10-03 15:35:33 -070095
Pravin B Shelar6b001e62015-08-26 23:46:53 -070096 dev_change_flags(dev, dev->flags | IFF_UP);
97 rtnl_unlock();
Jesse Grossf5796682014-10-03 15:35:33 -070098 return vport;
99error:
100 return ERR_PTR(err);
101}
102
Pravin B Shelar6b001e62015-08-26 23:46:53 -0700103static struct vport *geneve_create(const struct vport_parms *parms)
Jesse Grossf5796682014-10-03 15:35:33 -0700104{
Pravin B Shelar6b001e62015-08-26 23:46:53 -0700105 struct vport *vport;
Jesse Grossf5796682014-10-03 15:35:33 -0700106
Pravin B Shelar6b001e62015-08-26 23:46:53 -0700107 vport = geneve_tnl_create(parms);
108 if (IS_ERR(vport))
109 return vport;
Jesse Grossf5796682014-10-03 15:35:33 -0700110
Pravin B Shelar6b001e62015-08-26 23:46:53 -0700111 return ovs_netdev_link(vport, parms->name);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800112}
113
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200114static struct vport_ops ovs_geneve_vport_ops = {
Jesse Grossf5796682014-10-03 15:35:33 -0700115 .type = OVS_VPORT_TYPE_GENEVE,
Pravin B Shelar6b001e62015-08-26 23:46:53 -0700116 .create = geneve_create,
117 .destroy = ovs_netdev_tunnel_destroy,
Jesse Grossf5796682014-10-03 15:35:33 -0700118 .get_options = geneve_get_options,
Pravin B Shelaraec15922015-10-20 23:00:10 -0700119 .send = dev_queue_xmit,
Jesse Grossf5796682014-10-03 15:35:33 -0700120};
Thomas Graf62b9c8d2014-10-22 17:29:06 +0200121
122static int __init ovs_geneve_tnl_init(void)
123{
124 return ovs_vport_ops_register(&ovs_geneve_vport_ops);
125}
126
127static void __exit ovs_geneve_tnl_exit(void)
128{
129 ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
130}
131
132module_init(ovs_geneve_tnl_init);
133module_exit(ovs_geneve_tnl_exit);
134
135MODULE_DESCRIPTION("OVS: Geneve swiching port");
136MODULE_LICENSE("GPL");
137MODULE_ALIAS("vport-type-5");