Skip to content

Commit 1d8fff9

Browse files
tgrafdavem330
authored andcommitted
ip_tunnel: Make ovs_tunnel_info and ovs_key_ipv4_tunnel generic
Rename the tunnel metadata data structures currently internal to OVS and make them generic for use by all IP tunnels. Both structures are kernel internal and will stay that way. Their members are exposed to user space through individual Netlink attributes by OVS. It will therefore be possible to extend/modify these structures without affecting user ABI. Signed-off-by: Thomas Graf <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent e3e4712 commit 1d8fff9

File tree

13 files changed

+128
-135
lines changed

13 files changed

+128
-135
lines changed

include/net/ip_tunnels.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@
2222
/* Keep error state on tunnel for 30 sec */
2323
#define IPTUNNEL_ERR_TIMEO (30*HZ)
2424

25+
/* Used to memset ip_tunnel padding. */
26+
#define IP_TUNNEL_KEY_SIZE \
27+
(offsetof(struct ip_tunnel_key, tp_dst) + \
28+
FIELD_SIZEOF(struct ip_tunnel_key, tp_dst))
29+
30+
struct ip_tunnel_key {
31+
__be64 tun_id;
32+
__be32 ipv4_src;
33+
__be32 ipv4_dst;
34+
__be16 tun_flags;
35+
__u8 ipv4_tos;
36+
__u8 ipv4_ttl;
37+
__be16 tp_src;
38+
__be16 tp_dst;
39+
} __packed __aligned(4); /* Minimize padding. */
40+
41+
struct ip_tunnel_info {
42+
struct ip_tunnel_key key;
43+
const void *options;
44+
u8 options_len;
45+
};
46+
2547
/* 6rd prefix/relay information */
2648
#ifdef CONFIG_IPV6_SIT_6RD
2749
struct ip_tunnel_6rd_parm {
@@ -136,6 +158,47 @@ int ip_tunnel_encap_add_ops(const struct ip_tunnel_encap_ops *op,
136158
int ip_tunnel_encap_del_ops(const struct ip_tunnel_encap_ops *op,
137159
unsigned int num);
138160

161+
static inline void __ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
162+
__be32 saddr, __be32 daddr,
163+
u8 tos, u8 ttl,
164+
__be16 tp_src, __be16 tp_dst,
165+
__be64 tun_id, __be16 tun_flags,
166+
const void *opts, u8 opts_len)
167+
{
168+
tun_info->key.tun_id = tun_id;
169+
tun_info->key.ipv4_src = saddr;
170+
tun_info->key.ipv4_dst = daddr;
171+
tun_info->key.ipv4_tos = tos;
172+
tun_info->key.ipv4_ttl = ttl;
173+
tun_info->key.tun_flags = tun_flags;
174+
175+
/* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
176+
* the upper tunnel are used.
177+
* E.g: GRE over IPSEC, the tp_src and tp_port are zero.
178+
*/
179+
tun_info->key.tp_src = tp_src;
180+
tun_info->key.tp_dst = tp_dst;
181+
182+
/* Clear struct padding. */
183+
if (sizeof(tun_info->key) != IP_TUNNEL_KEY_SIZE)
184+
memset((unsigned char *)&tun_info->key + IP_TUNNEL_KEY_SIZE,
185+
0, sizeof(tun_info->key) - IP_TUNNEL_KEY_SIZE);
186+
187+
tun_info->options = opts;
188+
tun_info->options_len = opts_len;
189+
}
190+
191+
static inline void ip_tunnel_info_init(struct ip_tunnel_info *tun_info,
192+
const struct iphdr *iph,
193+
__be16 tp_src, __be16 tp_dst,
194+
__be64 tun_id, __be16 tun_flags,
195+
const void *opts, u8 opts_len)
196+
{
197+
__ip_tunnel_info_init(tun_info, iph->saddr, iph->daddr,
198+
iph->tos, iph->ttl, tp_src, tp_dst,
199+
tun_id, tun_flags, opts, opts_len);
200+
}
201+
139202
#ifdef CONFIG_INET
140203

141204
int ip_tunnel_init(struct net_device *dev);

include/uapi/linux/openvswitch.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ enum ovs_key_attr {
321321
* the accepted length of the array. */
322322

323323
#ifdef __KERNEL__
324-
OVS_KEY_ATTR_TUNNEL_INFO, /* struct ovs_tunnel_info */
324+
OVS_KEY_ATTR_TUNNEL_INFO, /* struct ip_tunnel_info */
325325
#endif
326326
__OVS_KEY_ATTR_MAX
327327
};

net/openvswitch/actions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb,
611611
struct sw_flow_key *key, const struct nlattr *attr,
612612
const struct nlattr *actions, int actions_len)
613613
{
614-
struct ovs_tunnel_info info;
614+
struct ip_tunnel_info info;
615615
struct dp_upcall_info upcall;
616616
const struct nlattr *a;
617617
int rem;

net/openvswitch/datapath.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/netdevice.h>
2626
#include <linux/skbuff.h>
2727
#include <linux/u64_stats_sync.h>
28+
#include <net/ip_tunnels.h>
2829

2930
#include "flow.h"
3031
#include "flow_table.h"
@@ -98,7 +99,7 @@ struct datapath {
9899
* when a packet is received by OVS.
99100
*/
100101
struct ovs_skb_cb {
101-
struct ovs_tunnel_info *egress_tun_info;
102+
struct ip_tunnel_info *egress_tun_info;
102103
struct vport *input_vport;
103104
};
104105
#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
@@ -114,7 +115,7 @@ struct ovs_skb_cb {
114115
* @egress_tun_info: If nonnull, becomes %OVS_PACKET_ATTR_EGRESS_TUN_KEY.
115116
*/
116117
struct dp_upcall_info {
117-
const struct ovs_tunnel_info *egress_tun_info;
118+
const struct ip_tunnel_info *egress_tun_info;
118119
const struct nlattr *userdata;
119120
const struct nlattr *actions;
120121
int actions_len;

net/openvswitch/flow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,12 @@ int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key)
682682
return key_extract(skb, key);
683683
}
684684

685-
int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
685+
int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
686686
struct sk_buff *skb, struct sw_flow_key *key)
687687
{
688688
/* Extract metadata from packet. */
689689
if (tun_info) {
690-
memcpy(&key->tun_key, &tun_info->tunnel, sizeof(key->tun_key));
690+
memcpy(&key->tun_key, &tun_info->key, sizeof(key->tun_key));
691691

692692
if (tun_info->options) {
693693
BUILD_BUG_ON((1 << (sizeof(tun_info->options_len) *

net/openvswitch/flow.h

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,10 @@
3232
#include <linux/time.h>
3333
#include <linux/flex_array.h>
3434
#include <net/inet_ecn.h>
35+
#include <net/ip_tunnels.h>
3536

3637
struct sk_buff;
3738

38-
/* Used to memset ovs_key_ipv4_tunnel padding. */
39-
#define OVS_TUNNEL_KEY_SIZE \
40-
(offsetof(struct ovs_key_ipv4_tunnel, tp_dst) + \
41-
FIELD_SIZEOF(struct ovs_key_ipv4_tunnel, tp_dst))
42-
43-
struct ovs_key_ipv4_tunnel {
44-
__be64 tun_id;
45-
__be32 ipv4_src;
46-
__be32 ipv4_dst;
47-
__be16 tun_flags;
48-
u8 ipv4_tos;
49-
u8 ipv4_ttl;
50-
__be16 tp_src;
51-
__be16 tp_dst;
52-
} __packed __aligned(4); /* Minimize padding. */
53-
54-
struct ovs_tunnel_info {
55-
struct ovs_key_ipv4_tunnel tunnel;
56-
const void *options;
57-
u8 options_len;
58-
};
59-
6039
/* Store options at the end of the array if they are less than the
6140
* maximum size. This allows us to get the benefits of variable length
6241
* matching for small options.
@@ -66,63 +45,14 @@ struct ovs_tunnel_info {
6645
#define TUN_METADATA_OPTS(flow_key, opt_len) \
6746
((void *)((flow_key)->tun_opts + TUN_METADATA_OFFSET(opt_len)))
6847

69-
static inline void __ovs_flow_tun_info_init(struct ovs_tunnel_info *tun_info,
70-
__be32 saddr, __be32 daddr,
71-
u8 tos, u8 ttl,
72-
__be16 tp_src,
73-
__be16 tp_dst,
74-
__be64 tun_id,
75-
__be16 tun_flags,
76-
const void *opts,
77-
u8 opts_len)
78-
{
79-
tun_info->tunnel.tun_id = tun_id;
80-
tun_info->tunnel.ipv4_src = saddr;
81-
tun_info->tunnel.ipv4_dst = daddr;
82-
tun_info->tunnel.ipv4_tos = tos;
83-
tun_info->tunnel.ipv4_ttl = ttl;
84-
tun_info->tunnel.tun_flags = tun_flags;
85-
86-
/* For the tunnel types on the top of IPsec, the tp_src and tp_dst of
87-
* the upper tunnel are used.
88-
* E.g: GRE over IPSEC, the tp_src and tp_port are zero.
89-
*/
90-
tun_info->tunnel.tp_src = tp_src;
91-
tun_info->tunnel.tp_dst = tp_dst;
92-
93-
/* Clear struct padding. */
94-
if (sizeof(tun_info->tunnel) != OVS_TUNNEL_KEY_SIZE)
95-
memset((unsigned char *)&tun_info->tunnel + OVS_TUNNEL_KEY_SIZE,
96-
0, sizeof(tun_info->tunnel) - OVS_TUNNEL_KEY_SIZE);
97-
98-
tun_info->options = opts;
99-
tun_info->options_len = opts_len;
100-
}
101-
102-
static inline void ovs_flow_tun_info_init(struct ovs_tunnel_info *tun_info,
103-
const struct iphdr *iph,
104-
__be16 tp_src,
105-
__be16 tp_dst,
106-
__be64 tun_id,
107-
__be16 tun_flags,
108-
const void *opts,
109-
u8 opts_len)
110-
{
111-
__ovs_flow_tun_info_init(tun_info, iph->saddr, iph->daddr,
112-
iph->tos, iph->ttl,
113-
tp_src, tp_dst,
114-
tun_id, tun_flags,
115-
opts, opts_len);
116-
}
117-
11848
#define OVS_SW_FLOW_KEY_METADATA_SIZE \
11949
(offsetof(struct sw_flow_key, recirc_id) + \
12050
FIELD_SIZEOF(struct sw_flow_key, recirc_id))
12151

12252
struct sw_flow_key {
12353
u8 tun_opts[255];
12454
u8 tun_opts_len;
125-
struct ovs_key_ipv4_tunnel tun_key; /* Encapsulating tunnel key. */
55+
struct ip_tunnel_key tun_key; /* Encapsulating tunnel key. */
12656
struct {
12757
u32 priority; /* Packet QoS priority. */
12858
u32 skb_mark; /* SKB mark. */
@@ -273,7 +203,7 @@ void ovs_flow_stats_clear(struct sw_flow *);
273203
u64 ovs_flow_used_time(unsigned long flow_jiffies);
274204

275205
int ovs_flow_key_update(struct sk_buff *skb, struct sw_flow_key *key);
276-
int ovs_flow_key_extract(const struct ovs_tunnel_info *tun_info,
206+
int ovs_flow_key_extract(const struct ip_tunnel_info *tun_info,
277207
struct sk_buff *skb,
278208
struct sw_flow_key *key);
279209
/* Extract key from packet coming from userspace. */

net/openvswitch/flow_netlink.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ static int vxlan_opt_to_nlattr(struct sk_buff *skb,
641641
}
642642

643643
static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
644-
const struct ovs_key_ipv4_tunnel *output,
644+
const struct ip_tunnel_key *output,
645645
const void *tun_opts, int swkey_tun_opts_len)
646646
{
647647
if (output->tun_flags & TUNNEL_KEY &&
@@ -689,7 +689,7 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
689689
}
690690

691691
static int ipv4_tun_to_nlattr(struct sk_buff *skb,
692-
const struct ovs_key_ipv4_tunnel *output,
692+
const struct ip_tunnel_key *output,
693693
const void *tun_opts, int swkey_tun_opts_len)
694694
{
695695
struct nlattr *nla;
@@ -708,9 +708,9 @@ static int ipv4_tun_to_nlattr(struct sk_buff *skb,
708708
}
709709

710710
int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
711-
const struct ovs_tunnel_info *egress_tun_info)
711+
const struct ip_tunnel_info *egress_tun_info)
712712
{
713-
return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel,
713+
return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
714714
egress_tun_info->options,
715715
egress_tun_info->options_len);
716716
}
@@ -1746,7 +1746,7 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
17461746
{
17471747
struct sw_flow_match match;
17481748
struct sw_flow_key key;
1749-
struct ovs_tunnel_info *tun_info;
1749+
struct ip_tunnel_info *tun_info;
17501750
struct nlattr *a;
17511751
int err = 0, start, opts_type;
17521752

@@ -1777,7 +1777,7 @@ static int validate_and_copy_set_tun(const struct nlattr *attr,
17771777
return PTR_ERR(a);
17781778

17791779
tun_info = nla_data(a);
1780-
tun_info->tunnel = key.tun_key;
1780+
tun_info->key = key.tun_key;
17811781
tun_info->options_len = key.tun_opts_len;
17821782

17831783
if (tun_info->options_len) {
@@ -2227,13 +2227,13 @@ static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
22272227

22282228
switch (key_type) {
22292229
case OVS_KEY_ATTR_TUNNEL_INFO: {
2230-
struct ovs_tunnel_info *tun_info = nla_data(ovs_key);
2230+
struct ip_tunnel_info *tun_info = nla_data(ovs_key);
22312231

22322232
start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
22332233
if (!start)
22342234
return -EMSGSIZE;
22352235

2236-
err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel,
2236+
err = ipv4_tun_to_nlattr(skb, &tun_info->key,
22372237
tun_info->options_len ?
22382238
tun_info->options : NULL,
22392239
tun_info->options_len);

net/openvswitch/flow_netlink.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb);
5555
int ovs_nla_get_match(struct sw_flow_match *, const struct nlattr *key,
5656
const struct nlattr *mask, bool log);
5757
int ovs_nla_put_egress_tunnel_key(struct sk_buff *,
58-
const struct ovs_tunnel_info *);
58+
const struct ip_tunnel_info *);
5959

6060
bool ovs_nla_get_ufid(struct sw_flow_id *, const struct nlattr *, bool log);
6161
int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,

net/openvswitch/vport-geneve.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void geneve_rcv(struct geneve_sock *gs, struct sk_buff *skb)
7777
struct vport *vport = gs->rcv_data;
7878
struct genevehdr *geneveh = geneve_hdr(skb);
7979
int opts_len;
80-
struct ovs_tunnel_info tun_info;
80+
struct ip_tunnel_info tun_info;
8181
__be64 key;
8282
__be16 flags;
8383

@@ -90,10 +90,9 @@ static void geneve_rcv(struct geneve_sock *gs, struct sk_buff *skb)
9090

9191
key = vni_to_tunnel_id(geneveh->vni);
9292

93-
ovs_flow_tun_info_init(&tun_info, ip_hdr(skb),
94-
udp_hdr(skb)->source, udp_hdr(skb)->dest,
95-
key, flags,
96-
geneveh->options, opts_len);
93+
ip_tunnel_info_init(&tun_info, ip_hdr(skb),
94+
udp_hdr(skb)->source, udp_hdr(skb)->dest,
95+
key, flags, geneveh->options, opts_len);
9796

9897
ovs_vport_receive(vport, skb, &tun_info);
9998
}
@@ -165,8 +164,8 @@ static struct vport *geneve_tnl_create(const struct vport_parms *parms)
165164

166165
static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb)
167166
{
168-
const struct ovs_key_ipv4_tunnel *tun_key;
169-
struct ovs_tunnel_info *tun_info;
167+
const struct ip_tunnel_key *tun_key;
168+
struct ip_tunnel_info *tun_info;
170169
struct net *net = ovs_dp_get_net(vport->dp);
171170
struct geneve_port *geneve_port = geneve_vport(vport);
172171
__be16 dport = inet_sk(geneve_port->gs->sock->sk)->inet_sport;
@@ -183,7 +182,7 @@ static int geneve_tnl_send(struct vport *vport, struct sk_buff *skb)
183182
goto error;
184183
}
185184

186-
tun_key = &tun_info->tunnel;
185+
tun_key = &tun_info->key;
187186
rt = ovs_tunnel_route_lookup(net, tun_key, skb->mark, &fl, IPPROTO_UDP);
188187
if (IS_ERR(rt)) {
189188
err = PTR_ERR(rt);
@@ -225,7 +224,7 @@ static const char *geneve_get_name(const struct vport *vport)
225224
}
226225

227226
static int geneve_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
228-
struct ovs_tunnel_info *egress_tun_info)
227+
struct ip_tunnel_info *egress_tun_info)
229228
{
230229
struct geneve_port *geneve_port = geneve_vport(vport);
231230
struct net *net = ovs_dp_get_net(vport->dp);

0 commit comments

Comments
 (0)