Skip to content

Commit 2e15ea3

Browse files
Pravin B Shelardavem330
authored andcommitted
ip_gre: Add support to collect tunnel metadata.
Following patch create new tunnel flag which enable tunnel metadata collection on given device. Signed-off-by: Pravin B Shelar <pshelar@nicira.com> Acked-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent a9020fd commit 2e15ea3

File tree

6 files changed

+216
-28
lines changed

6 files changed

+216
-28
lines changed

include/net/ip_tunnels.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct ip_tunnel_dst {
8282
__be32 saddr;
8383
};
8484

85+
struct metadata_dst;
86+
8587
struct ip_tunnel {
8688
struct ip_tunnel __rcu *next;
8789
struct hlist_node hash_node;
@@ -115,6 +117,7 @@ struct ip_tunnel {
115117
unsigned int prl_count; /* # of entries in PRL */
116118
int ip_tnl_net_id;
117119
struct gro_cells gro_cells;
120+
bool collect_md;
118121
};
119122

120123
#define TUNNEL_CSUM __cpu_to_be16(0x01)
@@ -149,6 +152,7 @@ struct tnl_ptk_info {
149152
struct ip_tunnel_net {
150153
struct net_device *fb_tunnel_dev;
151154
struct hlist_head tunnels[IP_TNL_HASH_SIZE];
155+
struct ip_tunnel __rcu *collect_md_tun;
152156
};
153157

154158
struct ip_tunnel_encap_ops {
@@ -235,7 +239,8 @@ struct ip_tunnel *ip_tunnel_lookup(struct ip_tunnel_net *itn,
235239
__be32 key);
236240

237241
int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
238-
const struct tnl_ptk_info *tpi, bool log_ecn_error);
242+
const struct tnl_ptk_info *tpi, struct metadata_dst *tun_dst,
243+
bool log_ecn_error);
239244
int ip_tunnel_changelink(struct net_device *dev, struct nlattr *tb[],
240245
struct ip_tunnel_parm *p);
241246
int ip_tunnel_newlink(struct net_device *dev, struct nlattr *tb[],

include/uapi/linux/if_tunnel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ enum {
112112
IFLA_GRE_ENCAP_FLAGS,
113113
IFLA_GRE_ENCAP_SPORT,
114114
IFLA_GRE_ENCAP_DPORT,
115+
IFLA_GRE_COLLECT_METADATA,
115116
__IFLA_GRE_MAX,
116117
};
117118

net/ipv4/ip_gre.c

Lines changed: 179 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/udp.h>
2626
#include <linux/if_arp.h>
2727
#include <linux/mroute.h>
28+
#include <linux/if_vlan.h>
2829
#include <linux/init.h>
2930
#include <linux/in6.h>
3031
#include <linux/inetdevice.h>
@@ -47,6 +48,7 @@
4748
#include <net/netns/generic.h>
4849
#include <net/rtnetlink.h>
4950
#include <net/gre.h>
51+
#include <net/dst_metadata.h>
5052

5153
#if IS_ENABLED(CONFIG_IPV6)
5254
#include <net/ipv6.h>
@@ -200,9 +202,29 @@ static int ipgre_err(struct sk_buff *skb, u32 info,
200202
return PACKET_RCVD;
201203
}
202204

205+
static __be64 key_to_tunnel_id(__be32 key)
206+
{
207+
#ifdef __BIG_ENDIAN
208+
return (__force __be64)((__force u32)key);
209+
#else
210+
return (__force __be64)((__force u64)key << 32);
211+
#endif
212+
}
213+
214+
/* Returns the least-significant 32 bits of a __be64. */
215+
static __be32 tunnel_id_to_key(__be64 x)
216+
{
217+
#ifdef __BIG_ENDIAN
218+
return (__force __be32)x;
219+
#else
220+
return (__force __be32)((__force u64)x >> 32);
221+
#endif
222+
}
223+
203224
static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
204225
{
205226
struct net *net = dev_net(skb->dev);
227+
struct metadata_dst *tun_dst = NULL;
206228
struct ip_tunnel_net *itn;
207229
const struct iphdr *iph;
208230
struct ip_tunnel *tunnel;
@@ -218,40 +240,162 @@ static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
218240

219241
if (tunnel) {
220242
skb_pop_mac_header(skb);
221-
ip_tunnel_rcv(tunnel, skb, tpi, log_ecn_error);
243+
if (tunnel->collect_md) {
244+
struct ip_tunnel_info *info;
245+
246+
tun_dst = metadata_dst_alloc(0, GFP_ATOMIC);
247+
if (!tun_dst)
248+
return PACKET_REJECT;
249+
250+
info = &tun_dst->u.tun_info;
251+
info->key.ipv4_src = iph->saddr;
252+
info->key.ipv4_dst = iph->daddr;
253+
info->key.ipv4_tos = iph->tos;
254+
info->key.ipv4_ttl = iph->ttl;
255+
256+
info->mode = IP_TUNNEL_INFO_RX;
257+
info->key.tun_flags = tpi->flags &
258+
(TUNNEL_CSUM | TUNNEL_KEY);
259+
info->key.tun_id = key_to_tunnel_id(tpi->key);
260+
261+
info->key.tp_src = 0;
262+
info->key.tp_dst = 0;
263+
}
264+
265+
ip_tunnel_rcv(tunnel, skb, tpi, tun_dst, log_ecn_error);
222266
return PACKET_RCVD;
223267
}
224268
return PACKET_REJECT;
225269
}
226270

271+
static void build_header(struct sk_buff *skb, int hdr_len, __be16 flags,
272+
__be16 proto, __be32 key, __be32 seq)
273+
{
274+
struct gre_base_hdr *greh;
275+
276+
skb_push(skb, hdr_len);
277+
278+
skb_reset_transport_header(skb);
279+
greh = (struct gre_base_hdr *)skb->data;
280+
greh->flags = tnl_flags_to_gre_flags(flags);
281+
greh->protocol = proto;
282+
283+
if (flags & (TUNNEL_KEY | TUNNEL_CSUM | TUNNEL_SEQ)) {
284+
__be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
285+
286+
if (flags & TUNNEL_SEQ) {
287+
*ptr = seq;
288+
ptr--;
289+
}
290+
if (flags & TUNNEL_KEY) {
291+
*ptr = key;
292+
ptr--;
293+
}
294+
if (flags & TUNNEL_CSUM &&
295+
!(skb_shinfo(skb)->gso_type &
296+
(SKB_GSO_GRE | SKB_GSO_GRE_CSUM))) {
297+
*ptr = 0;
298+
*(__sum16 *)ptr = csum_fold(skb_checksum(skb, 0,
299+
skb->len, 0));
300+
}
301+
}
302+
}
303+
227304
static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
228305
const struct iphdr *tnl_params,
229306
__be16 proto)
230307
{
231308
struct ip_tunnel *tunnel = netdev_priv(dev);
232-
struct tnl_ptk_info tpi;
233309

234-
tpi.flags = tunnel->parms.o_flags;
235-
tpi.proto = proto;
236-
tpi.key = tunnel->parms.o_key;
237310
if (tunnel->parms.o_flags & TUNNEL_SEQ)
238311
tunnel->o_seqno++;
239-
tpi.seq = htonl(tunnel->o_seqno);
240312

241313
/* Push GRE header. */
242-
gre_build_header(skb, &tpi, tunnel->tun_hlen);
243-
244-
skb_set_inner_protocol(skb, tpi.proto);
314+
build_header(skb, tunnel->tun_hlen, tunnel->parms.o_flags,
315+
proto, tunnel->parms.o_key, htonl(tunnel->o_seqno));
245316

317+
skb_set_inner_protocol(skb, proto);
246318
ip_tunnel_xmit(skb, dev, tnl_params, tnl_params->protocol);
247319
}
248320

321+
static void gre_fb_xmit(struct sk_buff *skb, struct net_device *dev)
322+
{
323+
struct ip_tunnel_info *tun_info;
324+
struct net *net = dev_net(dev);
325+
const struct ip_tunnel_key *key;
326+
struct flowi4 fl;
327+
struct rtable *rt;
328+
int min_headroom;
329+
int tunnel_hlen;
330+
__be16 df, flags;
331+
int err;
332+
333+
tun_info = skb_tunnel_info(skb, AF_INET);
334+
if (unlikely(!tun_info || tun_info->mode != IP_TUNNEL_INFO_TX))
335+
goto err_free_skb;
336+
337+
key = &tun_info->key;
338+
memset(&fl, 0, sizeof(fl));
339+
fl.daddr = key->ipv4_dst;
340+
fl.saddr = key->ipv4_src;
341+
fl.flowi4_tos = RT_TOS(key->ipv4_tos);
342+
fl.flowi4_mark = skb->mark;
343+
fl.flowi4_proto = IPPROTO_GRE;
344+
345+
rt = ip_route_output_key(net, &fl);
346+
if (IS_ERR(rt))
347+
goto err_free_skb;
348+
349+
tunnel_hlen = ip_gre_calc_hlen(key->tun_flags);
350+
351+
min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
352+
+ tunnel_hlen + sizeof(struct iphdr);
353+
if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
354+
int head_delta = SKB_DATA_ALIGN(min_headroom -
355+
skb_headroom(skb) +
356+
16);
357+
err = pskb_expand_head(skb, max_t(int, head_delta, 0),
358+
0, GFP_ATOMIC);
359+
if (unlikely(err))
360+
goto err_free_rt;
361+
}
362+
363+
/* Push Tunnel header. */
364+
skb = gre_handle_offloads(skb, !!(tun_info->key.tun_flags & TUNNEL_CSUM));
365+
if (IS_ERR(skb)) {
366+
skb = NULL;
367+
goto err_free_rt;
368+
}
369+
370+
flags = tun_info->key.tun_flags & (TUNNEL_CSUM | TUNNEL_KEY);
371+
build_header(skb, tunnel_hlen, flags, htons(ETH_P_TEB),
372+
tunnel_id_to_key(tun_info->key.tun_id), 0);
373+
374+
df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
375+
err = iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
376+
key->ipv4_dst, IPPROTO_GRE,
377+
key->ipv4_tos, key->ipv4_ttl, df, false);
378+
iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
379+
return;
380+
381+
err_free_rt:
382+
ip_rt_put(rt);
383+
err_free_skb:
384+
kfree_skb(skb);
385+
dev->stats.tx_dropped++;
386+
}
387+
249388
static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
250389
struct net_device *dev)
251390
{
252391
struct ip_tunnel *tunnel = netdev_priv(dev);
253392
const struct iphdr *tnl_params;
254393

394+
if (tunnel->collect_md) {
395+
gre_fb_xmit(skb, dev);
396+
return NETDEV_TX_OK;
397+
}
398+
255399
if (dev->header_ops) {
256400
/* Need space for new headers */
257401
if (skb_cow_head(skb, dev->needed_headroom -
@@ -277,7 +421,6 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
277421
goto out;
278422

279423
__gre_xmit(skb, dev, tnl_params, skb->protocol);
280-
281424
return NETDEV_TX_OK;
282425

283426
free_skb:
@@ -292,6 +435,11 @@ static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
292435
{
293436
struct ip_tunnel *tunnel = netdev_priv(dev);
294437

438+
if (tunnel->collect_md) {
439+
gre_fb_xmit(skb, dev);
440+
return NETDEV_TX_OK;
441+
}
442+
295443
skb = gre_handle_offloads(skb, !!(tunnel->parms.o_flags&TUNNEL_CSUM));
296444
if (IS_ERR(skb))
297445
goto out;
@@ -300,7 +448,6 @@ static netdev_tx_t gre_tap_xmit(struct sk_buff *skb,
300448
goto free_skb;
301449

302450
__gre_xmit(skb, dev, &tunnel->parms.iph, htons(ETH_P_TEB));
303-
304451
return NETDEV_TX_OK;
305452

306453
free_skb:
@@ -596,8 +743,10 @@ static int ipgre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
596743
return ipgre_tunnel_validate(tb, data);
597744
}
598745

599-
static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
600-
struct ip_tunnel_parm *parms)
746+
static void ipgre_netlink_parms(struct net_device *dev,
747+
struct nlattr *data[],
748+
struct nlattr *tb[],
749+
struct ip_tunnel_parm *parms)
601750
{
602751
memset(parms, 0, sizeof(*parms));
603752

@@ -635,6 +784,12 @@ static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
635784

636785
if (!data[IFLA_GRE_PMTUDISC] || nla_get_u8(data[IFLA_GRE_PMTUDISC]))
637786
parms->iph.frag_off = htons(IP_DF);
787+
788+
if (data[IFLA_GRE_COLLECT_METADATA]) {
789+
struct ip_tunnel *t = netdev_priv(dev);
790+
791+
t->collect_md = true;
792+
}
638793
}
639794

640795
/* This function returns true when ENCAP attributes are present in the nl msg */
@@ -712,7 +867,7 @@ static int ipgre_newlink(struct net *src_net, struct net_device *dev,
712867
return err;
713868
}
714869

715-
ipgre_netlink_parms(data, tb, &p);
870+
ipgre_netlink_parms(dev, data, tb, &p);
716871
return ip_tunnel_newlink(dev, tb, &p);
717872
}
718873

@@ -730,7 +885,7 @@ static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
730885
return err;
731886
}
732887

733-
ipgre_netlink_parms(data, tb, &p);
888+
ipgre_netlink_parms(dev, data, tb, &p);
734889
return ip_tunnel_changelink(dev, tb, &p);
735890
}
736891

@@ -765,6 +920,8 @@ static size_t ipgre_get_size(const struct net_device *dev)
765920
nla_total_size(2) +
766921
/* IFLA_GRE_ENCAP_DPORT */
767922
nla_total_size(2) +
923+
/* IFLA_GRE_COLLECT_METADATA */
924+
nla_total_size(0) +
768925
0;
769926
}
770927

@@ -796,6 +953,11 @@ static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
796953
t->encap.flags))
797954
goto nla_put_failure;
798955

956+
if (t->collect_md) {
957+
if (nla_put_flag(skb, IFLA_GRE_COLLECT_METADATA))
958+
goto nla_put_failure;
959+
}
960+
799961
return 0;
800962

801963
nla_put_failure:
@@ -817,6 +979,7 @@ static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
817979
[IFLA_GRE_ENCAP_FLAGS] = { .type = NLA_U16 },
818980
[IFLA_GRE_ENCAP_SPORT] = { .type = NLA_U16 },
819981
[IFLA_GRE_ENCAP_DPORT] = { .type = NLA_U16 },
982+
[IFLA_GRE_COLLECT_METADATA] = { .type = NLA_FLAG },
820983
};
821984

822985
static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
@@ -851,7 +1014,7 @@ static struct rtnl_link_ops ipgre_tap_ops __read_mostly = {
8511014

8521015
static int __net_init ipgre_tap_init_net(struct net *net)
8531016
{
854-
return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, NULL);
1017+
return ip_tunnel_init_net(net, gre_tap_net_id, &ipgre_tap_ops, "gretap0");
8551018
}
8561019

8571020
static void __net_exit ipgre_tap_exit_net(struct net *net)

0 commit comments

Comments
 (0)