Skip to content

Commit c411ed8

Browse files
Jiri Bencdavem330
Jiri Benc
authored andcommitted
nsh: add GSO support
Add a new nsh/ directory. It currently holds only GSO functions but more will come: in particular, code shared by openvswitch and tc to manipulate NSH headers. For now, assume there's no hardware support for NSH segmentation. We can always introduce netdev->nsh_features later. Signed-off-by: Jiri Benc <jbenc@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1f0b774 commit c411ed8

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

net/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ source "net/openvswitch/Kconfig"
235235
source "net/vmw_vsock/Kconfig"
236236
source "net/netlink/Kconfig"
237237
source "net/mpls/Kconfig"
238+
source "net/nsh/Kconfig"
238239
source "net/hsr/Kconfig"
239240
source "net/switchdev/Kconfig"
240241
source "net/l3mdev/Kconfig"

net/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ obj-$(CONFIG_NET_IFE) += ife/
7575
obj-$(CONFIG_OPENVSWITCH) += openvswitch/
7676
obj-$(CONFIG_VSOCKETS) += vmw_vsock/
7777
obj-$(CONFIG_MPLS) += mpls/
78+
obj-$(CONFIG_NET_NSH) += nsh/
7879
obj-$(CONFIG_HSR) += hsr/
7980
ifneq ($(CONFIG_NET_SWITCHDEV),)
8081
obj-y += switchdev/

net/nsh/Kconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
menuconfig NET_NSH
2+
tristate "Network Service Header (NSH) protocol"
3+
default n
4+
---help---
5+
Network Service Header is an implementation of Service Function
6+
Chaining (RFC 7665). The current implementation in Linux supports
7+
only MD type 1 and only with the openvswitch module.
8+
9+
If unsure, say N.

net/nsh/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
obj-$(CONFIG_NET_NSH) += nsh.o

net/nsh/nsh.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Network Service Header
3+
*
4+
* Copyright (c) 2017 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*/
10+
11+
#include <linux/module.h>
12+
#include <linux/netdevice.h>
13+
#include <linux/skbuff.h>
14+
#include <net/nsh.h>
15+
#include <net/tun_proto.h>
16+
17+
static struct sk_buff *nsh_gso_segment(struct sk_buff *skb,
18+
netdev_features_t features)
19+
{
20+
struct sk_buff *segs = ERR_PTR(-EINVAL);
21+
unsigned int nsh_len, mac_len;
22+
__be16 proto;
23+
int nhoff;
24+
25+
skb_reset_network_header(skb);
26+
27+
nhoff = skb->network_header - skb->mac_header;
28+
mac_len = skb->mac_len;
29+
30+
if (unlikely(!pskb_may_pull(skb, NSH_BASE_HDR_LEN)))
31+
goto out;
32+
nsh_len = nsh_hdr_len(nsh_hdr(skb));
33+
if (unlikely(!pskb_may_pull(skb, nsh_len)))
34+
goto out;
35+
36+
proto = tun_p_to_eth_p(nsh_hdr(skb)->np);
37+
if (!proto)
38+
goto out;
39+
40+
__skb_pull(skb, nsh_len);
41+
42+
skb_reset_mac_header(skb);
43+
skb_reset_mac_len(skb);
44+
skb->protocol = proto;
45+
46+
features &= NETIF_F_SG;
47+
segs = skb_mac_gso_segment(skb, features);
48+
if (IS_ERR_OR_NULL(segs)) {
49+
skb_gso_error_unwind(skb, htons(ETH_P_NSH), nsh_len,
50+
skb->network_header - nhoff,
51+
mac_len);
52+
goto out;
53+
}
54+
55+
for (skb = segs; skb; skb = skb->next) {
56+
skb->protocol = htons(ETH_P_NSH);
57+
__skb_push(skb, nsh_len);
58+
skb_set_mac_header(skb, -nhoff);
59+
skb->network_header = skb->mac_header + mac_len;
60+
skb->mac_len = mac_len;
61+
}
62+
63+
out:
64+
return segs;
65+
}
66+
67+
static struct packet_offload nsh_packet_offload __read_mostly = {
68+
.type = htons(ETH_P_NSH),
69+
.priority = 15,
70+
.callbacks = {
71+
.gso_segment = nsh_gso_segment,
72+
},
73+
};
74+
75+
static int __init nsh_init_module(void)
76+
{
77+
dev_add_offload(&nsh_packet_offload);
78+
return 0;
79+
}
80+
81+
static void __exit nsh_cleanup_module(void)
82+
{
83+
dev_remove_offload(&nsh_packet_offload);
84+
}
85+
86+
module_init(nsh_init_module);
87+
module_exit(nsh_cleanup_module);
88+
89+
MODULE_AUTHOR("Jiri Benc <jbenc@redhat.com>");
90+
MODULE_DESCRIPTION("NSH protocol");
91+
MODULE_LICENSE("GPL v2");

0 commit comments

Comments
 (0)