Skip to content

Commit 29930e3

Browse files
postgraphummakynes
authored andcommitted
ipvs: add checksum support for gue encapsulation
Add checksum support for gue encapsulation with the tun_flags parameter, which could be one of the values below: IP_VS_TUNNEL_ENCAP_FLAG_NOCSUM IP_VS_TUNNEL_ENCAP_FLAG_CSUM IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM Signed-off-by: Jacky Hu <[email protected]> Signed-off-by: Julian Anastasov <[email protected]> Signed-off-by: Simon Horman <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 2cf6bff commit 29930e3

File tree

4 files changed

+146
-17
lines changed

4 files changed

+146
-17
lines changed

include/net/ip_vs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ struct ip_vs_dest_user_kern {
603603

604604
u16 tun_type; /* tunnel type */
605605
__be16 tun_port; /* tunnel port */
606+
u16 tun_flags; /* tunnel flags */
606607
};
607608

608609

@@ -665,6 +666,7 @@ struct ip_vs_dest {
665666
atomic_t last_weight; /* server latest weight */
666667
__u16 tun_type; /* tunnel type */
667668
__be16 tun_port; /* tunnel port */
669+
__u16 tun_flags; /* tunnel flags */
668670

669671
refcount_t refcnt; /* reference counter */
670672
struct ip_vs_stats stats; /* statistics */

include/uapi/linux/ip_vs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ enum {
131131
IP_VS_CONN_F_TUNNEL_TYPE_MAX,
132132
};
133133

134+
/* Tunnel encapsulation flags */
135+
#define IP_VS_TUNNEL_ENCAP_FLAG_NOCSUM (0)
136+
#define IP_VS_TUNNEL_ENCAP_FLAG_CSUM (1 << 0)
137+
#define IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM (1 << 1)
138+
134139
/*
135140
* The struct ip_vs_service_user and struct ip_vs_dest_user are
136141
* used to set IPVS rules through setsockopt.
@@ -403,6 +408,8 @@ enum {
403408

404409
IPVS_DEST_ATTR_TUN_PORT, /* tunnel port */
405410

411+
IPVS_DEST_ATTR_TUN_FLAGS, /* tunnel flags */
412+
406413
__IPVS_DEST_ATTR_MAX,
407414
};
408415

net/netfilter/ipvs/ip_vs_ctl.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,
893893
/* set the tunnel info */
894894
dest->tun_type = udest->tun_type;
895895
dest->tun_port = udest->tun_port;
896+
dest->tun_flags = udest->tun_flags;
896897

897898
/* set the IP_VS_CONN_F_NOOUTPUT flag if not masquerading/NAT */
898899
if ((conn_flags & IP_VS_CONN_F_FWD_MASK) != IP_VS_CONN_F_MASQ) {
@@ -2967,6 +2968,7 @@ static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
29672968
[IPVS_DEST_ATTR_ADDR_FAMILY] = { .type = NLA_U16 },
29682969
[IPVS_DEST_ATTR_TUN_TYPE] = { .type = NLA_U8 },
29692970
[IPVS_DEST_ATTR_TUN_PORT] = { .type = NLA_U16 },
2971+
[IPVS_DEST_ATTR_TUN_FLAGS] = { .type = NLA_U16 },
29702972
};
29712973

29722974
static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
@@ -3273,6 +3275,8 @@ static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
32733275
dest->tun_type) ||
32743276
nla_put_be16(skb, IPVS_DEST_ATTR_TUN_PORT,
32753277
dest->tun_port) ||
3278+
nla_put_u16(skb, IPVS_DEST_ATTR_TUN_FLAGS,
3279+
dest->tun_flags) ||
32763280
nla_put_u32(skb, IPVS_DEST_ATTR_U_THRESH, dest->u_threshold) ||
32773281
nla_put_u32(skb, IPVS_DEST_ATTR_L_THRESH, dest->l_threshold) ||
32783282
nla_put_u32(skb, IPVS_DEST_ATTR_ACTIVE_CONNS,
@@ -3393,14 +3397,16 @@ static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
33933397
/* If a full entry was requested, check for the additional fields */
33943398
if (full_entry) {
33953399
struct nlattr *nla_fwd, *nla_weight, *nla_u_thresh,
3396-
*nla_l_thresh, *nla_tun_type, *nla_tun_port;
3400+
*nla_l_thresh, *nla_tun_type, *nla_tun_port,
3401+
*nla_tun_flags;
33973402

33983403
nla_fwd = attrs[IPVS_DEST_ATTR_FWD_METHOD];
33993404
nla_weight = attrs[IPVS_DEST_ATTR_WEIGHT];
34003405
nla_u_thresh = attrs[IPVS_DEST_ATTR_U_THRESH];
34013406
nla_l_thresh = attrs[IPVS_DEST_ATTR_L_THRESH];
34023407
nla_tun_type = attrs[IPVS_DEST_ATTR_TUN_TYPE];
34033408
nla_tun_port = attrs[IPVS_DEST_ATTR_TUN_PORT];
3409+
nla_tun_flags = attrs[IPVS_DEST_ATTR_TUN_FLAGS];
34043410

34053411
if (!(nla_fwd && nla_weight && nla_u_thresh && nla_l_thresh))
34063412
return -EINVAL;
@@ -3416,6 +3422,9 @@ static int ip_vs_genl_parse_dest(struct ip_vs_dest_user_kern *udest,
34163422

34173423
if (nla_tun_port)
34183424
udest->tun_port = nla_get_be16(nla_tun_port);
3425+
3426+
if (nla_tun_flags)
3427+
udest->tun_flags = nla_get_u16(nla_tun_flags);
34193428
}
34203429

34213430
return 0;

net/netfilter/ipvs/ip_vs_xmit.c

Lines changed: 127 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <net/ipv6.h>
4141
#include <net/ip6_route.h>
4242
#include <net/ip_tunnels.h>
43+
#include <net/ip6_checksum.h>
4344
#include <net/addrconf.h>
4445
#include <linux/icmpv6.h>
4546
#include <linux/netfilter.h>
@@ -385,8 +386,13 @@ __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
385386
mtu = dst_mtu(&rt->dst) - sizeof(struct iphdr);
386387
if (!dest)
387388
goto err_put;
388-
if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
389+
if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
389390
mtu -= sizeof(struct udphdr) + sizeof(struct guehdr);
391+
if ((dest->tun_flags &
392+
IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
393+
skb->ip_summed == CHECKSUM_PARTIAL)
394+
mtu -= GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
395+
}
390396
if (mtu < 68) {
391397
IP_VS_DBG_RL("%s(): mtu less than 68\n", __func__);
392398
goto err_put;
@@ -540,8 +546,13 @@ __ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,
540546
mtu = dst_mtu(&rt->dst) - sizeof(struct ipv6hdr);
541547
if (!dest)
542548
goto err_put;
543-
if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
549+
if (dest->tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
544550
mtu -= sizeof(struct udphdr) + sizeof(struct guehdr);
551+
if ((dest->tun_flags &
552+
IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
553+
skb->ip_summed == CHECKSUM_PARTIAL)
554+
mtu -= GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
555+
}
545556
if (mtu < IPV6_MIN_MTU) {
546557
IP_VS_DBG_RL("%s(): mtu less than %d\n", __func__,
547558
IPV6_MIN_MTU);
@@ -1006,17 +1017,56 @@ ipvs_gue_encap(struct net *net, struct sk_buff *skb,
10061017
__be16 sport = udp_flow_src_port(net, skb, 0, 0, false);
10071018
struct udphdr *udph; /* Our new UDP header */
10081019
struct guehdr *gueh; /* Our new GUE header */
1020+
size_t hdrlen, optlen = 0;
1021+
void *data;
1022+
bool need_priv = false;
1023+
1024+
if ((cp->dest->tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1025+
skb->ip_summed == CHECKSUM_PARTIAL) {
1026+
optlen += GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
1027+
need_priv = true;
1028+
}
10091029

1010-
skb_push(skb, sizeof(struct guehdr));
1030+
hdrlen = sizeof(struct guehdr) + optlen;
1031+
1032+
skb_push(skb, hdrlen);
10111033

10121034
gueh = (struct guehdr *)skb->data;
10131035

10141036
gueh->control = 0;
10151037
gueh->version = 0;
1016-
gueh->hlen = 0;
1038+
gueh->hlen = optlen >> 2;
10171039
gueh->flags = 0;
10181040
gueh->proto_ctype = *next_protocol;
10191041

1042+
data = &gueh[1];
1043+
1044+
if (need_priv) {
1045+
__be32 *flags = data;
1046+
u16 csum_start = skb_checksum_start_offset(skb);
1047+
__be16 *pd;
1048+
1049+
gueh->flags |= GUE_FLAG_PRIV;
1050+
*flags = 0;
1051+
data += GUE_LEN_PRIV;
1052+
1053+
if (csum_start < hdrlen)
1054+
return -EINVAL;
1055+
1056+
csum_start -= hdrlen;
1057+
pd = data;
1058+
pd[0] = htons(csum_start);
1059+
pd[1] = htons(csum_start + skb->csum_offset);
1060+
1061+
if (!skb_is_gso(skb)) {
1062+
skb->ip_summed = CHECKSUM_NONE;
1063+
skb->encapsulation = 0;
1064+
}
1065+
1066+
*flags |= GUE_PFLAG_REMCSUM;
1067+
data += GUE_PLEN_REMCSUM;
1068+
}
1069+
10201070
skb_push(skb, sizeof(struct udphdr));
10211071
skb_reset_transport_header(skb);
10221072

@@ -1070,6 +1120,7 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
10701120
unsigned int max_headroom; /* The extra header space needed */
10711121
int ret, local;
10721122
int tun_type, gso_type;
1123+
int tun_flags;
10731124

10741125
EnterFunction(10);
10751126

@@ -1092,9 +1143,19 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
10921143
max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct iphdr);
10931144

10941145
tun_type = cp->dest->tun_type;
1146+
tun_flags = cp->dest->tun_flags;
10951147

1096-
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
1097-
max_headroom += sizeof(struct udphdr) + sizeof(struct guehdr);
1148+
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1149+
size_t gue_hdrlen, gue_optlen = 0;
1150+
1151+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1152+
skb->ip_summed == CHECKSUM_PARTIAL) {
1153+
gue_optlen += GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
1154+
}
1155+
gue_hdrlen = sizeof(struct guehdr) + gue_optlen;
1156+
1157+
max_headroom += sizeof(struct udphdr) + gue_hdrlen;
1158+
}
10981159

10991160
/* We only care about the df field if sysctl_pmtu_disc(ipvs) is set */
11001161
dfp = sysctl_pmtu_disc(ipvs) ? &df : NULL;
@@ -1105,8 +1166,17 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
11051166
goto tx_error;
11061167

11071168
gso_type = __tun_gso_type_mask(AF_INET, cp->af);
1108-
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
1109-
gso_type |= SKB_GSO_UDP_TUNNEL;
1169+
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1170+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1171+
(tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1172+
gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1173+
else
1174+
gso_type |= SKB_GSO_UDP_TUNNEL;
1175+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1176+
skb->ip_summed == CHECKSUM_PARTIAL) {
1177+
gso_type |= SKB_GSO_TUNNEL_REMCSUM;
1178+
}
1179+
}
11101180

11111181
if (iptunnel_handle_offloads(skb, gso_type))
11121182
goto tx_error;
@@ -1115,8 +1185,19 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
11151185

11161186
skb_set_inner_ipproto(skb, next_protocol);
11171187

1118-
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
1119-
ipvs_gue_encap(net, skb, cp, &next_protocol);
1188+
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1189+
bool check = false;
1190+
1191+
if (ipvs_gue_encap(net, skb, cp, &next_protocol))
1192+
goto tx_error;
1193+
1194+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1195+
(tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1196+
check = true;
1197+
1198+
udp_set_csum(!check, skb, saddr, cp->daddr.ip, skb->len);
1199+
}
1200+
11201201

11211202
skb_push(skb, sizeof(struct iphdr));
11221203
skb_reset_network_header(skb);
@@ -1174,6 +1255,7 @@ ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
11741255
unsigned int max_headroom; /* The extra header space needed */
11751256
int ret, local;
11761257
int tun_type, gso_type;
1258+
int tun_flags;
11771259

11781260
EnterFunction(10);
11791261

@@ -1197,9 +1279,19 @@ ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
11971279
max_headroom = LL_RESERVED_SPACE(tdev) + sizeof(struct ipv6hdr);
11981280

11991281
tun_type = cp->dest->tun_type;
1282+
tun_flags = cp->dest->tun_flags;
12001283

1201-
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
1202-
max_headroom += sizeof(struct udphdr) + sizeof(struct guehdr);
1284+
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1285+
size_t gue_hdrlen, gue_optlen = 0;
1286+
1287+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1288+
skb->ip_summed == CHECKSUM_PARTIAL) {
1289+
gue_optlen += GUE_PLEN_REMCSUM + GUE_LEN_PRIV;
1290+
}
1291+
gue_hdrlen = sizeof(struct guehdr) + gue_optlen;
1292+
1293+
max_headroom += sizeof(struct udphdr) + gue_hdrlen;
1294+
}
12031295

12041296
skb = ip_vs_prepare_tunneled_skb(skb, cp->af, max_headroom,
12051297
&next_protocol, &payload_len,
@@ -1208,8 +1300,17 @@ ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
12081300
goto tx_error;
12091301

12101302
gso_type = __tun_gso_type_mask(AF_INET6, cp->af);
1211-
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
1212-
gso_type |= SKB_GSO_UDP_TUNNEL;
1303+
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1304+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1305+
(tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1306+
gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1307+
else
1308+
gso_type |= SKB_GSO_UDP_TUNNEL;
1309+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM) &&
1310+
skb->ip_summed == CHECKSUM_PARTIAL) {
1311+
gso_type |= SKB_GSO_TUNNEL_REMCSUM;
1312+
}
1313+
}
12131314

12141315
if (iptunnel_handle_offloads(skb, gso_type))
12151316
goto tx_error;
@@ -1218,8 +1319,18 @@ ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
12181319

12191320
skb_set_inner_ipproto(skb, next_protocol);
12201321

1221-
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE)
1222-
ipvs_gue_encap(net, skb, cp, &next_protocol);
1322+
if (tun_type == IP_VS_CONN_F_TUNNEL_TYPE_GUE) {
1323+
bool check = false;
1324+
1325+
if (ipvs_gue_encap(net, skb, cp, &next_protocol))
1326+
goto tx_error;
1327+
1328+
if ((tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_CSUM) ||
1329+
(tun_flags & IP_VS_TUNNEL_ENCAP_FLAG_REMCSUM))
1330+
check = true;
1331+
1332+
udp6_set_csum(!check, skb, &saddr, &cp->daddr.in6, skb->len);
1333+
}
12231334

12241335
skb_push(skb, sizeof(struct ipv6hdr));
12251336
skb_reset_network_header(skb);

0 commit comments

Comments
 (0)