Skip to content

Commit 4d5ec89

Browse files
numansiddiquedavem330
authored andcommitted
net: openvswitch: Add a new action check_pkt_len
This patch adds a new action - 'check_pkt_len' which checks the packet length and executes a set of actions if the packet length is greater than the specified length or executes another set of actions if the packet length is lesser or equal to. This action takes below nlattrs * OVS_CHECK_PKT_LEN_ATTR_PKT_LEN - 'pkt_len' to check for * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER - Nested actions to apply if the packet length is greater than the specified 'pkt_len' * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL - Nested actions to apply if the packet length is lesser or equal to the specified 'pkt_len'. The main use case for adding this action is to solve the packet drops because of MTU mismatch in OVN virtual networking solution. When a VM (which belongs to a logical switch of OVN) sends a packet destined to go via the gateway router and if the nic which provides external connectivity, has a lesser MTU, OVS drops the packet if the packet length is greater than this MTU. With the help of this action, OVN will check the packet length and if it is greater than the MTU size, it will generate an ICMP packet (type 3, code 4) and includes the next hop mtu in it so that the sender can fragment the packets. Reported-at: https://mail.openvswitch.org/pipermail/ovs-discuss/2018-July/047039.html Suggested-by: Ben Pfaff <blp@ovn.org> Signed-off-by: Numan Siddique <nusiddiq@redhat.com> CC: Gregory Rose <gvrose8192@gmail.com> CC: Pravin B Shelar <pshelar@ovn.org> Acked-by: Pravin B Shelar <pshelar@ovn.org> Tested-by: Greg Rose <gvrose8192@gmail.com> Reviewed-by: Greg Rose <gvrose8192@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d7aa033 commit 4d5ec89

3 files changed

Lines changed: 261 additions & 0 deletions

File tree

‎include/uapi/linux/openvswitch.h‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,44 @@ struct ovs_action_push_eth {
798798
struct ovs_key_ethernet addresses;
799799
};
800800

801+
/*
802+
* enum ovs_check_pkt_len_attr - Attributes for %OVS_ACTION_ATTR_CHECK_PKT_LEN.
803+
*
804+
* @OVS_CHECK_PKT_LEN_ATTR_PKT_LEN: u16 Packet length to check for.
805+
* @OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER: Nested OVS_ACTION_ATTR_*
806+
* actions to apply if the packer length is greater than the specified
807+
* length in the attr - OVS_CHECK_PKT_LEN_ATTR_PKT_LEN.
808+
* @OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL - Nested OVS_ACTION_ATTR_*
809+
* actions to apply if the packer length is lesser or equal to the specified
810+
* length in the attr - OVS_CHECK_PKT_LEN_ATTR_PKT_LEN.
811+
*/
812+
enum ovs_check_pkt_len_attr {
813+
OVS_CHECK_PKT_LEN_ATTR_UNSPEC,
814+
OVS_CHECK_PKT_LEN_ATTR_PKT_LEN,
815+
OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER,
816+
OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL,
817+
__OVS_CHECK_PKT_LEN_ATTR_MAX,
818+
819+
#ifdef __KERNEL__
820+
OVS_CHECK_PKT_LEN_ATTR_ARG /* struct check_pkt_len_arg */
821+
#endif
822+
};
823+
824+
#define OVS_CHECK_PKT_LEN_ATTR_MAX (__OVS_CHECK_PKT_LEN_ATTR_MAX - 1)
825+
826+
#ifdef __KERNEL__
827+
struct check_pkt_len_arg {
828+
u16 pkt_len; /* Same value as OVS_CHECK_PKT_LEN_ATTR_PKT_LEN'. */
829+
bool exec_for_greater; /* When true, actions in IF_GREATER will
830+
* not change flow keys. False otherwise.
831+
*/
832+
bool exec_for_lesser_equal; /* When true, actions in IF_LESS_EQUAL
833+
* will not change flow keys. False
834+
* otherwise.
835+
*/
836+
};
837+
#endif
838+
801839
/**
802840
* enum ovs_action_attr - Action types.
803841
*
@@ -842,6 +880,9 @@ struct ovs_action_push_eth {
842880
* packet, or modify the packet (e.g., change the DSCP field).
843881
* @OVS_ACTION_ATTR_CLONE: make a copy of the packet and execute a list of
844882
* actions without affecting the original packet and key.
883+
* @OVS_ACTION_ATTR_CHECK_PKT_LEN: Check the packet length and execute a set
884+
* of actions if greater than the specified packet length, else execute
885+
* another set of actions.
845886
*
846887
* Only a single header can be set with a single %OVS_ACTION_ATTR_SET. Not all
847888
* fields within a header are modifiable, e.g. the IPv4 protocol and fragment
@@ -876,6 +917,7 @@ enum ovs_action_attr {
876917
OVS_ACTION_ATTR_POP_NSH, /* No argument. */
877918
OVS_ACTION_ATTR_METER, /* u32 meter ID. */
878919
OVS_ACTION_ATTR_CLONE, /* Nested OVS_CLONE_ATTR_*. */
920+
OVS_ACTION_ATTR_CHECK_PKT_LEN, /* Nested OVS_CHECK_PKT_LEN_ATTR_*. */
879921

880922
__OVS_ACTION_ATTR_MAX, /* Nothing past this will be accepted
881923
* from userspace. */

‎net/openvswitch/actions.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ static int clone_execute(struct datapath *dp, struct sk_buff *skb,
169169
const struct nlattr *actions, int len,
170170
bool last, bool clone_flow_key);
171171

172+
static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
173+
struct sw_flow_key *key,
174+
const struct nlattr *attr, int len);
175+
172176
static void update_ethertype(struct sk_buff *skb, struct ethhdr *hdr,
173177
__be16 ethertype)
174178
{
@@ -1213,6 +1217,40 @@ static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
12131217
return clone_execute(dp, skb, key, recirc_id, NULL, 0, last, true);
12141218
}
12151219

1220+
static int execute_check_pkt_len(struct datapath *dp, struct sk_buff *skb,
1221+
struct sw_flow_key *key,
1222+
const struct nlattr *attr, bool last)
1223+
{
1224+
const struct nlattr *actions, *cpl_arg;
1225+
const struct check_pkt_len_arg *arg;
1226+
int rem = nla_len(attr);
1227+
bool clone_flow_key;
1228+
1229+
/* The first netlink attribute in 'attr' is always
1230+
* 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
1231+
*/
1232+
cpl_arg = nla_data(attr);
1233+
arg = nla_data(cpl_arg);
1234+
1235+
if (skb->len <= arg->pkt_len) {
1236+
/* Second netlink attribute in 'attr' is always
1237+
* 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
1238+
*/
1239+
actions = nla_next(cpl_arg, &rem);
1240+
clone_flow_key = !arg->exec_for_lesser_equal;
1241+
} else {
1242+
/* Third netlink attribute in 'attr' is always
1243+
* 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER'.
1244+
*/
1245+
actions = nla_next(cpl_arg, &rem);
1246+
actions = nla_next(actions, &rem);
1247+
clone_flow_key = !arg->exec_for_greater;
1248+
}
1249+
1250+
return clone_execute(dp, skb, key, 0, nla_data(actions),
1251+
nla_len(actions), last, clone_flow_key);
1252+
}
1253+
12161254
/* Execute a list of actions against 'skb'. */
12171255
static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
12181256
struct sw_flow_key *key,
@@ -1374,6 +1412,16 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
13741412

13751413
break;
13761414
}
1415+
1416+
case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
1417+
bool last = nla_is_last(a, rem);
1418+
1419+
err = execute_check_pkt_len(dp, skb, key, a, last);
1420+
if (last)
1421+
return err;
1422+
1423+
break;
1424+
}
13771425
}
13781426

13791427
if (unlikely(err)) {

‎net/openvswitch/flow_netlink.c‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static bool actions_may_change_flow(const struct nlattr *actions)
9191
case OVS_ACTION_ATTR_SET:
9292
case OVS_ACTION_ATTR_SET_MASKED:
9393
case OVS_ACTION_ATTR_METER:
94+
case OVS_ACTION_ATTR_CHECK_PKT_LEN:
9495
default:
9596
return true;
9697
}
@@ -2838,6 +2839,87 @@ static int validate_userspace(const struct nlattr *attr)
28382839
return 0;
28392840
}
28402841

2842+
static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
2843+
[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
2844+
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
2845+
[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
2846+
};
2847+
2848+
static int validate_and_copy_check_pkt_len(struct net *net,
2849+
const struct nlattr *attr,
2850+
const struct sw_flow_key *key,
2851+
struct sw_flow_actions **sfa,
2852+
__be16 eth_type, __be16 vlan_tci,
2853+
bool log, bool last)
2854+
{
2855+
const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
2856+
struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
2857+
struct check_pkt_len_arg arg;
2858+
int nested_acts_start;
2859+
int start, err;
2860+
2861+
err = nla_parse_strict(a, OVS_CHECK_PKT_LEN_ATTR_MAX, nla_data(attr),
2862+
nla_len(attr), cpl_policy, NULL);
2863+
if (err)
2864+
return err;
2865+
2866+
if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
2867+
!nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
2868+
return -EINVAL;
2869+
2870+
acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
2871+
acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
2872+
2873+
/* Both the nested action should be present. */
2874+
if (!acts_if_greater || !acts_if_lesser_eq)
2875+
return -EINVAL;
2876+
2877+
/* validation done, copy the nested actions. */
2878+
start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
2879+
log);
2880+
if (start < 0)
2881+
return start;
2882+
2883+
arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
2884+
arg.exec_for_lesser_equal =
2885+
last || !actions_may_change_flow(acts_if_lesser_eq);
2886+
arg.exec_for_greater =
2887+
last || !actions_may_change_flow(acts_if_greater);
2888+
2889+
err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
2890+
sizeof(arg), log);
2891+
if (err)
2892+
return err;
2893+
2894+
nested_acts_start = add_nested_action_start(sfa,
2895+
OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
2896+
if (nested_acts_start < 0)
2897+
return nested_acts_start;
2898+
2899+
err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
2900+
eth_type, vlan_tci, log);
2901+
2902+
if (err)
2903+
return err;
2904+
2905+
add_nested_action_end(*sfa, nested_acts_start);
2906+
2907+
nested_acts_start = add_nested_action_start(sfa,
2908+
OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
2909+
if (nested_acts_start < 0)
2910+
return nested_acts_start;
2911+
2912+
err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
2913+
eth_type, vlan_tci, log);
2914+
2915+
if (err)
2916+
return err;
2917+
2918+
add_nested_action_end(*sfa, nested_acts_start);
2919+
add_nested_action_end(*sfa, start);
2920+
return 0;
2921+
}
2922+
28412923
static int copy_action(const struct nlattr *from,
28422924
struct sw_flow_actions **sfa, bool log)
28432925
{
@@ -2884,6 +2966,7 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
28842966
[OVS_ACTION_ATTR_POP_NSH] = 0,
28852967
[OVS_ACTION_ATTR_METER] = sizeof(u32),
28862968
[OVS_ACTION_ATTR_CLONE] = (u32)-1,
2969+
[OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
28872970
};
28882971
const struct ovs_action_push_vlan *vlan;
28892972
int type = nla_type(a);
@@ -3085,6 +3168,19 @@ static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
30853168
break;
30863169
}
30873170

3171+
case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
3172+
bool last = nla_is_last(a, rem);
3173+
3174+
err = validate_and_copy_check_pkt_len(net, a, key, sfa,
3175+
eth_type,
3176+
vlan_tci, log,
3177+
last);
3178+
if (err)
3179+
return err;
3180+
skip_copy = true;
3181+
break;
3182+
}
3183+
30883184
default:
30893185
OVS_NLERR(log, "Unknown Action type %d", type);
30903186
return -EINVAL;
@@ -3183,6 +3279,75 @@ static int clone_action_to_attr(const struct nlattr *attr,
31833279
return err;
31843280
}
31853281

3282+
static int check_pkt_len_action_to_attr(const struct nlattr *attr,
3283+
struct sk_buff *skb)
3284+
{
3285+
struct nlattr *start, *ac_start = NULL;
3286+
const struct check_pkt_len_arg *arg;
3287+
const struct nlattr *a, *cpl_arg;
3288+
int err = 0, rem = nla_len(attr);
3289+
3290+
start = nla_nest_start(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
3291+
if (!start)
3292+
return -EMSGSIZE;
3293+
3294+
/* The first nested attribute in 'attr' is always
3295+
* 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3296+
*/
3297+
cpl_arg = nla_data(attr);
3298+
arg = nla_data(cpl_arg);
3299+
3300+
if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
3301+
err = -EMSGSIZE;
3302+
goto out;
3303+
}
3304+
3305+
/* Second nested attribute in 'attr' is always
3306+
* 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3307+
*/
3308+
a = nla_next(cpl_arg, &rem);
3309+
ac_start = nla_nest_start(skb,
3310+
OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
3311+
if (!ac_start) {
3312+
err = -EMSGSIZE;
3313+
goto out;
3314+
}
3315+
3316+
err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3317+
if (err) {
3318+
nla_nest_cancel(skb, ac_start);
3319+
goto out;
3320+
} else {
3321+
nla_nest_end(skb, ac_start);
3322+
}
3323+
3324+
/* Third nested attribute in 'attr' is always
3325+
* OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3326+
*/
3327+
a = nla_next(a, &rem);
3328+
ac_start = nla_nest_start(skb,
3329+
OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
3330+
if (!ac_start) {
3331+
err = -EMSGSIZE;
3332+
goto out;
3333+
}
3334+
3335+
err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3336+
if (err) {
3337+
nla_nest_cancel(skb, ac_start);
3338+
goto out;
3339+
} else {
3340+
nla_nest_end(skb, ac_start);
3341+
}
3342+
3343+
nla_nest_end(skb, start);
3344+
return 0;
3345+
3346+
out:
3347+
nla_nest_cancel(skb, start);
3348+
return err;
3349+
}
3350+
31863351
static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
31873352
{
31883353
const struct nlattr *ovs_key = nla_data(a);
@@ -3277,6 +3442,12 @@ int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
32773442
return err;
32783443
break;
32793444

3445+
case OVS_ACTION_ATTR_CHECK_PKT_LEN:
3446+
err = check_pkt_len_action_to_attr(a, skb);
3447+
if (err)
3448+
return err;
3449+
break;
3450+
32803451
default:
32813452
if (nla_put(skb, type, nla_len(a), nla_data(a)))
32823453
return -EMSGSIZE;

0 commit comments

Comments
 (0)