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.
Unified · Split
| @@ -580,6 +580,10 @@ enum ovs_userspace_attr { · 579 unmodified lines | ||
| 580 | 580 | |
| 581 | 581 | #define OVS_USERSPACE_ATTR_MAX (__OVS_USERSPACE_ATTR_MAX - 1) |
| 582 | 582 | |
| 583 | +struct ovs_action_trunc { | |
| 584 | + uint32_t max_len; /* Max packet size in bytes. */ | |
| 585 | +}; | |
| 586 | + | |
| 583 | 587 | /** |
| 584 | 588 | * struct ovs_action_push_mpls - %OVS_ACTION_ATTR_PUSH_MPLS action argument. |
| 585 | 589 | * @mpls_lse: MPLS label stack entry to push. |
| @@ -703,6 +707,7 @@ enum ovs_nat_attr { · 117 unmodified lines | ||
| 703 | 707 | * enum ovs_action_attr - Action types. |
| 704 | 708 | * |
| 705 | 709 | * @OVS_ACTION_ATTR_OUTPUT: Output packet to port. |
| 710 | + * @OVS_ACTION_ATTR_TRUNC: Output packet to port with truncated packet size. | |
| 706 | 711 | * @OVS_ACTION_ATTR_USERSPACE: Send packet to userspace according to nested |
| 707 | 712 | * %OVS_USERSPACE_ATTR_* attributes. |
| 708 | 713 | * @OVS_ACTION_ATTR_SET: Replaces the contents of an existing header. The |
| @@ -756,6 +761,7 @@ enum ovs_action_attr { · 47 unmodified lines | ||
| 756 | 761 | * The data must be zero for the unmasked |
| 757 | 762 | * bits. */ |
| 758 | 763 | OVS_ACTION_ATTR_CT, /* Nested OVS_CT_ATTR_* . */ |
| 764 | + OVS_ACTION_ATTR_TRUNC, /* u32 struct ovs_action_trunc. */ | |
| 759 | 765 | |
| 760 | 766 | __OVS_ACTION_ATTR_MAX, /* Nothing past this will be accepted |
| 761 | 767 | * from userspace. */ |
| @@ -750,6 +750,14 @@ static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port, · 749 unmodified lines | ||
| 750 | 750 | |
| 751 | 751 | if (likely(vport)) { |
| 752 | 752 | 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 | + } | |
| 753 | 761 | |
| 754 | 762 | if (likely(!mru || (skb->len <= mru + ETH_HLEN))) { |
| 755 | 763 | ovs_vport_send(vport, skb); |
| @@ -775,7 +783,8 @@ static void do_output(struct datapath *dp, struct sk_buff *skb, int out_port, · 19 unmodified lines | ||
| 775 | 783 | |
| 776 | 784 | static int output_userspace(struct datapath *dp, struct sk_buff *skb, |
| 777 | 785 | 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) | |
| 779 | 788 | { |
| 780 | 789 | struct dp_upcall_info upcall; |
| 781 | 790 | const struct nlattr *a; |
| @@ -822,7 +831,7 @@ static int output_userspace(struct datapath *dp, struct sk_buff *skb, · 40 unmodified lines | ||
| 822 | 831 | } /* End of switch. */ |
| 823 | 832 | } |
| 824 | 833 | |
| 825 | − return ovs_dp_upcall(dp, skb, key, &upcall); | |
| 834 | + return ovs_dp_upcall(dp, skb, key, &upcall, cutlen); | |
| 826 | 835 | } |
| 827 | 836 | |
| 828 | 837 | static int sample(struct datapath *dp, struct sk_buff *skb, |
| @@ -832,6 +841,7 @@ static int sample(struct datapath *dp, struct sk_buff *skb, · 3 unmodified lines | ||
| 832 | 841 | const struct nlattr *acts_list = NULL; |
| 833 | 842 | const struct nlattr *a; |
| 834 | 843 | int rem; |
| 844 | + u32 cutlen = 0; | |
| 835 | 845 | |
| 836 | 846 | for (a = nla_data(attr), rem = nla_len(attr); rem > 0; |
| 837 | 847 | a = nla_next(a, &rem)) { |
| @@ -858,13 +868,24 @@ static int sample(struct datapath *dp, struct sk_buff *skb, · 20 unmodified lines | ||
| 858 | 868 | return 0; |
| 859 | 869 | |
| 860 | 870 | /* 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 | |
| 861 | 872 | * action. Treat this usage as a special case. |
| 862 | 873 | * The output_userspace() should clone the skb to be sent to the |
| 863 | 874 | * user space. This skb will be consumed by its caller. |
| 864 | 875 | */ |
| 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 | + | |
| 865 | 885 | if (likely(nla_type(a) == OVS_ACTION_ATTR_USERSPACE && |
| 866 | 886 | 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); | |
| 868 | 889 | |
| 869 | 890 | skb = skb_clone(skb, GFP_ATOMIC); |
| 870 | 891 | if (!skb) |
| @@ -1051,6 +1072,7 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, · 180 unmodified lines | ||
| 1051 | 1072 | if (out_skb) |
| 1052 | 1073 | do_output(dp, out_skb, prev_port, key); |
| 1053 | 1074 | |
| 1075 | + OVS_CB(skb)->cutlen = 0; | |
| 1054 | 1076 | prev_port = -1; |
| 1055 | 1077 | } |
| 1056 | 1078 | |
| @@ -1059,8 +1081,18 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb, · 2 unmodified lines | ||
| 1059 | 1081 | prev_port = nla_get_u32(a); |
| 1060 | 1082 | break; |
| 1061 | 1083 | |
| 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 | + | |
| 1062 | 1092 | 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; | |
| 1064 | 1096 | break; |
| 1065 | 1097 | |
| 1066 | 1098 | case OVS_ACTION_ATTR_HASH: |
| @@ -137,10 +137,12 @@ EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held); · 136 unmodified lines | ||
| 137 | 137 | static struct vport *new_vport(const struct vport_parms *); |
| 138 | 138 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *, |
| 139 | 139 | const struct sw_flow_key *, |
| 140 | − const struct dp_upcall_info *); | |
| 140 | + const struct dp_upcall_info *, | |
| 141 | + uint32_t cutlen); | |
| 141 | 142 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *, |
| 142 | 143 | const struct sw_flow_key *, |
| 143 | − const struct dp_upcall_info *); | |
| 144 | + const struct dp_upcall_info *, | |
| 145 | + uint32_t cutlen); | |
| 144 | 146 | |
| 145 | 147 | /* Must be called with rcu_read_lock. */ |
| 146 | 148 | 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) · 128 unmodified lines | ||
| 275 | 277 | upcall.cmd = OVS_PACKET_CMD_MISS; |
| 276 | 278 | upcall.portid = ovs_vport_find_upcall_portid(p, skb); |
| 277 | 279 | 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); | |
| 279 | 281 | if (unlikely(error)) |
| 280 | 282 | kfree_skb(skb); |
| 281 | 283 | else |
| @@ -300,7 +302,8 @@ out: · 18 unmodified lines | ||
| 300 | 302 | |
| 301 | 303 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, |
| 302 | 304 | 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) | |
| 304 | 307 | { |
| 305 | 308 | struct dp_stats_percpu *stats; |
| 306 | 309 | int err; |
| @@ -311,9 +314,9 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, · 4 unmodified lines | ||
| 311 | 314 | } |
| 312 | 315 | |
| 313 | 316 | 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); | |
| 315 | 318 | else |
| 316 | − err = queue_gso_packets(dp, skb, key, upcall_info); | |
| 319 | + err = queue_gso_packets(dp, skb, key, upcall_info, cutlen); | |
| 317 | 320 | if (err) |
| 318 | 321 | goto err; |
| 319 | 322 | |
| @@ -331,7 +334,8 @@ err: · 11 unmodified lines | ||
| 331 | 334 | |
| 332 | 335 | static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, |
| 333 | 336 | 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) | |
| 335 | 339 | { |
| 336 | 340 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
| 337 | 341 | struct sw_flow_key later_key; |
| @@ -360,7 +364,7 @@ static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb, · 22 unmodified lines | ||
| 360 | 364 | if (gso_type & SKB_GSO_UDP && skb != segs) |
| 361 | 365 | key = &later_key; |
| 362 | 366 | |
| 363 | − err = queue_userspace_packet(dp, skb, key, upcall_info); | |
| 367 | + err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen); | |
| 364 | 368 | if (err) |
| 365 | 369 | break; |
| 366 | 370 | |
| @@ -416,7 +420,8 @@ static void pad_packet(struct datapath *dp, struct sk_buff *skb) · 49 unmodified lines | ||
| 416 | 420 | |
| 417 | 421 | static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, |
| 418 | 422 | 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) | |
| 420 | 425 | { |
| 421 | 426 | struct ovs_header *upcall; |
| 422 | 427 | struct sk_buff *nskb = NULL; |
| @@ -461,7 +466,7 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, · 38 unmodified lines | ||
| 461 | 466 | else |
| 462 | 467 | hlen = skb->len; |
| 463 | 468 | |
| 464 | − len = upcall_msg_size(upcall_info, hlen); | |
| 469 | + len = upcall_msg_size(upcall_info, hlen - cutlen); | |
| 465 | 470 | user_skb = genlmsg_new(len, GFP_ATOMIC); |
| 466 | 471 | if (!user_skb) { |
| 467 | 472 | err = -ENOMEM; |
| @@ -515,9 +520,9 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb, · 47 unmodified lines | ||
| 515 | 520 | err = -ENOBUFS; |
| 516 | 521 | goto out; |
| 517 | 522 | } |
| 518 | − nla->nla_len = nla_attr_size(skb->len); | |
| 523 | + nla->nla_len = nla_attr_size(skb->len - cutlen); | |
| 519 | 524 | |
| 520 | − err = skb_zerocopy(user_skb, skb, skb->len, hlen); | |
| 525 | + err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen); | |
| 521 | 526 | if (err) |
| 522 | 527 | goto out; |
| 523 | 528 | |
| @@ -100,11 +100,13 @@ struct datapath { · 99 unmodified lines | ||
| 100 | 100 | * @input_vport: The original vport packet came in on. This value is cached |
| 101 | 101 | * when a packet is received by OVS. |
| 102 | 102 | * @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. | |
| 103 | 104 | * fragmented. |
| 104 | 105 | */ |
| 105 | 106 | struct ovs_skb_cb { |
| 106 | 107 | struct vport *input_vport; |
| 107 | 108 | u16 mru; |
| 109 | + u32 cutlen; | |
| 108 | 110 | }; |
| 109 | 111 | #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb) |
| 110 | 112 | |
| @@ -194,7 +196,8 @@ extern struct genl_family dp_vport_genl_family; · 83 unmodified lines | ||
| 194 | 196 | void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key); |
| 195 | 197 | void ovs_dp_detach_port(struct vport *); |
| 196 | 198 | 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); | |
| 198 | 201 | |
| 199 | 202 | const char *ovs_dp_name(const struct datapath *dp); |
| 200 | 203 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *, u32 pid, u32 seq, |
| @@ -2229,6 +2229,7 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr, · 2228 unmodified lines | ||
| 2229 | 2229 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1, |
| 2230 | 2230 | [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash), |
| 2231 | 2231 | [OVS_ACTION_ATTR_CT] = (u32)-1, |
| 2232 | + [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc), | |
| 2232 | 2233 | }; |
| 2233 | 2234 | const struct ovs_action_push_vlan *vlan; |
| 2234 | 2235 | int type = nla_type(a); |
| @@ -2255,6 +2256,14 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr, · 20 unmodified lines | ||
| 2255 | 2256 | return -EINVAL; |
| 2256 | 2257 | break; |
| 2257 | 2258 | |
| 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 | + | |
| 2258 | 2267 | case OVS_ACTION_ATTR_HASH: { |
| 2259 | 2268 | const struct ovs_action_hash *act_hash = nla_data(a); |
| 2260 | 2269 | |
| @@ -444,6 +444,7 @@ int ovs_vport_receive(struct vport *vport, struct sk_buff *skb, · 443 unmodified lines | ||
| 444 | 444 | |
| 445 | 445 | OVS_CB(skb)->input_vport = vport; |
| 446 | 446 | OVS_CB(skb)->mru = 0; |
| 447 | + OVS_CB(skb)->cutlen = 0; | |
| 447 | 448 | if (unlikely(dev_net(skb->dev) != ovs_dp_get_net(vport->dp))) { |
| 448 | 449 | u32 mark; |
| 449 | 450 | |