Skip to content

Commit f2a4d08

Browse files
williamtudavem330
authored andcommitted
openvswitch: Add packet truncation support.
The patch adds a new OVS action, OVS_ACTION_ATTR_TRUNC, in order to truncate packets. A 'max_len' is added for setting up the maximum packet size, and a 'cutlen' field is to record the number of bytes to trim the packet when the packet is outputting to a port, or when the packet is sent to userspace. Signed-off-by: William Tu <u9012063@gmail.com> Cc: Pravin Shelar <pshelar@nicira.com> Acked-by: Pravin B Shelar <pshelar@ovn.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 1578b0a commit f2a4d08

6 files changed

Lines changed: 73 additions & 17 deletions

File tree

‎include/uapi/linux/openvswitch.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,10 @@ enum ovs_userspace_attr {
580580

581581
#define OVS_USERSPACE_ATTR_MAX (__OVS_USERSPACE_ATTR_MAX - 1)
582582

583+
struct ovs_action_trunc {
584+
uint32_t max_len; /* Max packet size in bytes. */
585+
};
586+
583587
/**
584588
* struct ovs_action_push_mpls - %OVS_ACTION_ATTR_PUSH_MPLS action argument.
585589
* @mpls_lse: MPLS label stack entry to push.
@@ -703,6 +707,7 @@ enum ovs_nat_attr {
703707
* enum ovs_action_attr - Action types.
704708
*
705709
* @OVS_ACTION_ATTR_OUTPUT: Output packet to port.
710+
* @OVS_ACTION_ATTR_TRUNC: Output packet to port with truncated packet size.
706711
* @OVS_ACTION_ATTR_USERSPACE: Send packet to userspace according to nested
707712
* %OVS_USERSPACE_ATTR_* attributes.
708713
* @OVS_ACTION_ATTR_SET: Replaces the contents of an existing header. The
@@ -756,6 +761,7 @@ enum ovs_action_attr {
756761
* The data must be zero for the unmasked
757762
* bits. */
758763
OVS_ACTION_ATTR_CT, /* Nested OVS_CT_ATTR_* . */
764+
OVS_ACTION_ATTR_TRUNC, /* u32 struct ovs_action_trunc. */
759765

760766
__OVS_ACTION_ATTR_MAX, /* Nothing past this will be accepted
761767
* from userspace. */

‎net/openvswitch/actions.c‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
750750

751751
if (likely(vport)) {
752752
u16 mru = OVS_CB(skb)->mru;
753+
u32 cutlen = OVS_CB(skb)->cutlen;
754+
755+
if (unlikely(cutlen > 0)) {
756+
if (skb->len - cutlen > ETH_HLEN)
757+
pskb_trim(skb, skb->len - cutlen);
758+
else
759+
pskb_trim(skb, ETH_HLEN);
760+
}
753761

754762
if (likely(!mru || (skb->len <= mru + ETH_HLEN))) {
755763
ovs_vport_send(vport, skb);
@@ -775,7 +783,8 @@ static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port,
775783

776784
static int output_userspace(struct datapath *dp, struct sk_buff *skb,
777785
struct sw_flow_key *key, const struct nlattr *attr,
778-
const struct nlattr *actions, int actions_len)
786+
const struct nlattr *actions, int actions_len,
787+
uint32_t cutlen)
779788
{
780789
struct dp_upcall_info upcall;
781790
const struct nlattr *a;
@@ -822,7 +831,7 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb,
822831
} /* End of switch. */
823832
}
824833

825-
return ovs_dp_upcall(dp, skb, key, &upcall);
834+
return ovs_dp_upcall(dp, skb, key, &upcall, cutlen);
826835
}
827836

828837
static int sample(struct datapath *dp, struct sk_buff *skb,
@@ -832,6 +841,7 @@ static int sample(struct datapath *dp, struct sk_buff *skb,
832841
const struct nlattr *acts_list = NULL;
833842
const struct nlattr *a;
834843
int rem;
844+
u32 cutlen = 0;
835845

836846
for (a = nla_data(attr), rem = nla_len(attr); rem > 0;
837847
a = nla_next(a, &rem)) {
@@ -858,13 +868,24 @@ static int sample(struct datapath *dp, struct sk_buff *skb,
858868
return 0;
859869

860870
/* The only known usage of sample action is having a single user-space
871+
* action, or having a truncate action followed by a single user-space
861872
* action. Treat this usage as a special case.
862873
* The output_userspace() should clone the skb to be sent to the
863874
* user space. This skb will be consumed by its caller.
864875
*/
876+
if (unlikely(nla_type(a) == OVS_ACTION_ATTR_TRUNC)) {
877+
struct ovs_action_trunc *trunc = nla_data(a);
878+
879+
if (skb->len > trunc->max_len)
880+
cutlen = skb->len - trunc->max_len;
881+
882+
a = nla_next(a, &rem);
883+
}
884+
865885
if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE &&
866886
nla_is_last(a, rem)))
867-
return output_userspace(dp, skb, key, a, actions, actions_len);
887+
return output_userspace(dp, skb, key, a, actions,
888+
actions_len, cutlen);
868889

869890
skb = skb_clone(skb, GFP_ATOMIC);
870891
if (!skb)
@@ -1051,6 +1072,7 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
10511072
if (out_skb)
10521073
do_output(dp, out_skb, prev_port, key);
10531074

1075+
OVS_CB(skb)->cutlen = 0;
10541076
prev_port = -1;
10551077
}
10561078

@@ -1059,8 +1081,18 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
10591081
prev_port = nla_get_u32(a);
10601082
break;
10611083

1084+
case OVS_ACTION_ATTR_TRUNC: {
1085+
struct ovs_action_trunc *trunc = nla_data(a);
1086+
1087+
if (skb->len > trunc->max_len)
1088+
OVS_CB(skb)->cutlen = skb->len - trunc->max_len;
1089+
break;
1090+
}
1091+
10621092
case OVS_ACTION_ATTR_USERSPACE:
1063-
output_userspace(dp, skb, key, a, attr, len);
1093+
output_userspace(dp, skb, key, a, attr,
1094+
len, OVS_CB(skb)->cutlen);
1095+
OVS_CB(skb)->cutlen = 0;
10641096
break;
10651097

10661098
case OVS_ACTION_ATTR_HASH:

‎net/openvswitch/datapath.c‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
137137
static struct vport *new_vport(const struct vport_parms *);
138138
static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
139139
const struct sw_flow_key *,
140-
const struct dp_upcall_info *);
140+
const struct dp_upcall_info *,
141+
uint32_t cutlen);
141142
static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
142143
const struct sw_flow_key *,
143-
const struct dp_upcall_info *);
144+
const struct dp_upcall_info *,
145+
uint32_t cutlen);
144146

145147
/* Must be called with rcu_read_lock. */
146148
static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
@@ -275,7 +277,7 @@ void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
275277
upcall.cmd = OVS_PACKET_CMD_MISS;
276278
upcall.portid = ovs_vport_find_upcall_portid(p, skb);
277279
upcall.mru = OVS_CB(skb)->mru;
278-
error = ovs_dp_upcall(dp, skb, key, &upcall);
280+
error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
279281
if (unlikely(error))
280282
kfree_skb(skb);
281283
else
@@ -300,7 +302,8 @@ void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
300302

301303
int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
302304
const struct sw_flow_key *key,
303-
const struct dp_upcall_info *upcall_info)
305+
const struct dp_upcall_info *upcall_info,
306+
uint32_t cutlen)
304307
{
305308
struct dp_stats_percpu *stats;
306309
int err;
@@ -311,9 +314,9 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
311314
}
312315

313316
if (!skb_is_gso(skb))
314-
err = queue_userspace_packet(dp, skb, key, upcall_info);
317+
err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
315318
else
316-
err = queue_gso_packets(dp, skb, key, upcall_info);
319+
err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
317320
if (err)
318321
goto err;
319322

@@ -331,7 +334,8 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
331334

332335
static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
333336
const struct sw_flow_key *key,
334-
const struct dp_upcall_info *upcall_info)
337+
const struct dp_upcall_info *upcall_info,
338+
uint32_t cutlen)
335339
{
336340
unsigned short gso_type = skb_shinfo(skb)->gso_type;
337341
struct sw_flow_key later_key;
@@ -360,7 +364,7 @@ static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
360364
if (gso_type & SKB_GSO_UDP && skb != segs)
361365
key = &later_key;
362366

363-
err = queue_userspace_packet(dp, skb, key, upcall_info);
367+
err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
364368
if (err)
365369
break;
366370

@@ -416,7 +420,8 @@ static void pad_packet(struct datapath *dp, struct sk_buff *skb)
416420

417421
static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
418422
const struct sw_flow_key *key,
419-
const struct dp_upcall_info *upcall_info)
423+
const struct dp_upcall_info *upcall_info,
424+
uint32_t cutlen)
420425
{
421426
struct ovs_header *upcall;
422427
struct sk_buff *nskb = NULL;
@@ -461,7 +466,7 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
461466
else
462467
hlen = skb->len;
463468

464-
len = upcall_msg_size(upcall_info, hlen);
469+
len = upcall_msg_size(upcall_info, hlen - cutlen);
465470
user_skb = genlmsg_new(len, GFP_ATOMIC);
466471
if (!user_skb) {
467472
err = -ENOMEM;
@@ -515,9 +520,9 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
515520
err = -ENOBUFS;
516521
goto out;
517522
}
518-
nla->nla_len = nla_attr_size(skb->len);
523+
nla->nla_len = nla_attr_size(skb->len - cutlen);
519524

520-
err = skb_zerocopy(user_skb, skb, skb->len, hlen);
525+
err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
521526
if (err)
522527
goto out;
523528

‎net/openvswitch/datapath.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ struct datapath {
100100
* @input_vport: The original vport packet came in on. This value is cached
101101
* when a packet is received by OVS.
102102
* @mru: The maximum received fragement size; 0 if the packet is not
103+
* @cutlen: The number of bytes from the packet end to be removed.
103104
* fragmented.
104105
*/
105106
struct ovs_skb_cb {
106107
struct vport *input_vport;
107108
u16 mru;
109+
u32 cutlen;
108110
};
109111
#define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
110112

@@ -194,7 +196,8 @@ extern struct genl_family dp_vport_genl_family;
194196
void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key);
195197
void ovs_dp_detach_port(struct vport *);
196198
int ovs_dp_upcall(struct datapath *, struct sk_buff *,
197-
const struct sw_flow_key *, const struct dp_upcall_info *);
199+
const struct sw_flow_key *, const struct dp_upcall_info *,
200+
uint32_t cutlen);
198201

199202
const char *ovs_dp_name(const struct datapath *dp);
200203
struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq,

‎net/openvswitch/flow_netlink.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,7 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
22292229
[OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
22302230
[OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
22312231
[OVS_ACTION_ATTR_CT] = (u32)-1,
2232+
[OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
22322233
};
22332234
const struct ovs_action_push_vlan *vlan;
22342235
int type = nla_type(a);
@@ -2255,6 +2256,14 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
22552256
return -EINVAL;
22562257
break;
22572258

2259+
case OVS_ACTION_ATTR_TRUNC: {
2260+
const struct ovs_action_trunc *trunc = nla_data(a);
2261+
2262+
if (trunc->max_len < ETH_HLEN)
2263+
return -EINVAL;
2264+
break;
2265+
}
2266+
22582267
case OVS_ACTION_ATTR_HASH: {
22592268
const struct ovs_action_hash *act_hash = nla_data(a);
22602269

‎net/openvswitch/vport.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ int ovs_vport_receive(struct vport *vport, struct sk_buff *skb,
444444

445445
OVS_CB(skb)->input_vport = vport;
446446
OVS_CB(skb)->mru = 0;
447+
OVS_CB(skb)->cutlen = 0;
447448
if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) {
448449
u32 mark;
449450

0 commit comments

Comments
 (0)