From: Thomas F Herbert <thomasfherbert-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Pravin Shelar <pshelar-l0M0P4e3n4LQT0dZR+AlfA@public.gmane.org>
Cc: "dev-yBygre7rU0TnMu66kgdUjQ@public.gmane.org"
<dev-yBygre7rU0TnMu66kgdUjQ@public.gmane.org>,
netdev <netdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
therbert-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org
Subject: Re: [PATCH net-next V15 3/3] 802.1AD: Flow handling, actions, vlan parsing and netlink attributes
Date: Tue, 13 Oct 2015 18:39:06 +0100 [thread overview]
Message-ID: <561D41BA.7030300@gmail.com> (raw)
In-Reply-To: <CALnjE+qdqBrzZ_p6RCHLr63tSWTKPnTWhQuWsJ8dvAmq-bf9rQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
Pravin,
Thanks for the review.
On 10/13/15 7:47 AM, Pravin Shelar wrote:
> On Sat, Oct 10, 2015 at 4:40 PM, Thomas F Herbert
> <thomasfherbert@gmail.com> wrote:
>> Add support for 802.1ad including the ability to push and pop double
>> tagged vlans. Add support for 802.1ad to netlink parsing and flow
>> conversion. Uses double nested encap attributes to represent double
>> tagged vlan. Inner TPID encoded along with ctci in nested attributes.
>>
>> Signed-off-by: Thomas F Herbert <thomasfherbert@gmail.com>
>> ---
>> net/openvswitch/actions.c | 6 +-
>> net/openvswitch/flow.c | 92 +++++++++++++++++++----
>> net/openvswitch/flow.h | 11 ++-
>> net/openvswitch/flow_netlink.c | 166 +++++++++++++++++++++++++++++++++++++----
>> net/openvswitch/vport-netdev.c | 4 +-
>> 5 files changed, 245 insertions(+), 34 deletions(-)
>>
> ...
>
>> diff --git a/net/openvswitch/flow.c b/net/openvswitch/flow.c
>> index c8db44a..0f9479c 100644
>> --- a/net/openvswitch/flow.c
>> +++ b/net/openvswitch/flow.c
>> @@ -305,21 +305,83 @@ static bool icmp6hdr_ok(struct sk_buff *skb)
>> static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
>> {
>> struct qtag_prefix {
>> - __be16 eth_type; /* ETH_P_8021Q */
>> + __be16 eth_type; /* ETH_P_8021Q or ETH_P_8021AD */
>> __be16 tci;
>> };
>> - struct qtag_prefix *qp;
>> + struct qtag_prefix *qp = (struct qtag_prefix *)skb->data;
>>
>> - if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16)))
>> + if (likely(skb_vlan_tag_present(skb))) {
>> + key->eth.vlan.tci = htons(skb->vlan_tci);
>> + key->eth.vlan.tpid = skb->vlan_proto;
>> +
>> + /* Case where upstream
>> + * processing has already stripped the outer vlan tag.
>> + */
>> + if (unlikely(skb->vlan_proto == htons(ETH_P_8021AD))) {
>> + if (unlikely(skb->len < sizeof(struct qtag_prefix) +
>> + sizeof(__be16))) {
>> + key->eth.vlan.tci = 0;
>> + return 0;
>> + }
>> +
>> + if (unlikely(!pskb_may_pull(skb,
>> + sizeof(struct qtag_prefix) +
>> + sizeof(__be16))))
>> + return -ENOMEM;
>> +
>> + qp = (struct qtag_prefix *)skb->data;
>> + key->eth.cvlan.tci =
>> + qp->tci | htons(VLAN_TAG_PRESENT);
>> + key->eth.cvlan.tpid = qp->eth_type;
>> +
>> + __skb_pull(skb, sizeof(struct qtag_prefix));
>> + }
>> return 0;
>>
>> - if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
>> - sizeof(__be16))))
>> - return -ENOMEM;
>> + } else if (qp->eth_type == htons(ETH_P_8021AD)) {
>> +
>> + if (unlikely(skb->len < sizeof(struct qtag_prefix) +
>> + sizeof(__be16)))
>> + return 0;
>> +
>> + if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
>> + sizeof(__be16))))
>> + return -ENOMEM;
>> +
>> + qp = (struct qtag_prefix *)skb->data;
>> + key->eth.vlan.tci = qp->tci | htons(VLAN_TAG_PRESENT);
>> + key->eth.vlan.tpid = qp->eth_type;
>> +
>> + __skb_pull(skb, sizeof(struct qtag_prefix));
>>
>> - qp = (struct qtag_prefix *) skb->data;
>> - key->eth.tci = qp->tci | htons(VLAN_TAG_PRESENT);
>> - __skb_pull(skb, sizeof(struct qtag_prefix));
>> + if (unlikely(skb->len < sizeof(struct qtag_prefix) +
>> + sizeof(__be16)))
>> + return 0;
>> +
>> + if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
>> + sizeof(__be16))))
>> + return -ENOMEM;
>> +
> There is no check for inner protocol in the packet.
Yes, and the code should be generic to use eth_type_vlan() for both
inner and outer TPIDs.
I think your pseudo code below fixes that.
>
>> + key->eth.cvlan.tci = qp->tci | htons(VLAN_TAG_PRESENT);
>> + key->eth.cvlan.tpid = qp->eth_type;
>> +
>> + __skb_pull(skb, sizeof(struct qtag_prefix));
>> +
>> + return 0;
>> +
>> + } else if (qp->eth_type == htons(ETH_P_8021Q)) {
>> + if (unlikely(skb->len < sizeof(struct qtag_prefix) +
>> + sizeof(__be16)))
>> + return 0;
>> +
>> + if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
>> + sizeof(__be16))))
>> + return -ENOMEM;
>> + key->eth.vlan.tci = qp->tci | htons(VLAN_TAG_PRESENT);
>> + key->eth.vlan.tpid = qp->eth_type;
>> +
>> + __skb_pull(skb, sizeof(struct qtag_prefix));
>> + }
>>
>> return 0;
>> }
> I see lot of duplicate code here. How about code below:
>
> struct qtag_prefix {
> __be16 eth_type; /* ETH_P_8021Q or ETH_P_8021AD */
> __be16 tci;
> };
>
> /* Return < 0 on memory error
> * Return == 0 on non vlan or incomplete packet packet
> * Return > 0 on successfully parsing vlan tag.
> */
> static int parse_vlan_tag(__be16 vlan_proto, struct sk_buff *skb,
> struct vlan_tag *cvlan)
> {
> if (likely(!eth_type_vlan(skb->vlan_proto)))
> return 0;
>
> if (unlikely(skb->len < sizeof(struct qtag_prefix) + sizeof(__be16))) {
> vlan->tci = 0;
> return 0;
> }
>
> if (unlikely(!pskb_may_pull(skb, sizeof(struct qtag_prefix) +
> sizeof(__be16))))
> return -ENOMEM;
>
> qp = (struct qtag_prefix *)skb->data;
> key->eth.cvlan.tci = qp->tci | htons(VLAN_TAG_PRESENT);
> key->eth.cvlan.tpid = qp->eth_type;
>
> __skb_pull(skb, sizeof(struct qtag_prefix));
> return 1;
> }
This makes for cleaner code and certainly better for maintainability so
I have just implemented it for this next revision. However, note that
with this change, we incur the overhead of an additional function call
for single tagged vlan packets.
>
> static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
> {
> struct qtag_prefix *qp = (struct qtag_prefix *)skb->data;
> int res;
>
> if (likely(skb_vlan_tag_present(skb))) {
> key->eth.vlan.tci = htons(skb->vlan_tci);
> key->eth.vlan.tpid = skb->vlan_proto;
>
> /* Case where upstream
> * processing has already stripped the outer vlan tag.
> */
> res = parse_vlan_tag(skb->vlan_proto, skb, &key->eth.cvlan);
> if (res < 0)
> return res;
> /* Since this was inner tag, return zero in either
> success or failure
> * in parsing inner tag.
> */
> return 0;
> }
> res = parse_vlan_tag(qp->eth_type, skb, &key->eth.vlan);
> if (res <= 0)
> return res;
>
> res = parse_vlan_tag(key->eth.vlan.tpid, skb, &key->eth.vlan);
> if (res <= 0)
> return res;
>
> return 0;
> }
>
>> diff --git a/net/openvswitch/flow.h b/net/openvswitch/flow.h
>> index fe527d2..539494e 100644
>> --- a/net/openvswitch/flow.h
>> +++ b/net/openvswitch/flow.h
>> @@ -68,7 +68,16 @@ struct sw_flow_key {
>> struct {
>> u8 src[ETH_ALEN]; /* Ethernet source address. */
>> u8 dst[ETH_ALEN]; /* Ethernet destination address. */
>> - __be16 tci; /* 0 if no VLAN, VLAN_TAG_PRESENT set otherwise. */
>> + struct {
>> + __be16 tpid; /* Outer Vlan type 802.1q or 802.1ad.*/
>> + __be16 tci; /* 0 if no VLAN, VLAN_TAG_PRESENT */
>> + /* set otherwise. */
>> + } vlan;
>> + struct {
>> + __be16 tpid; /* Inner Vlan DL_type 802.1q.*/
>> + __be16 tci; /* 0 if no CVLAN, VLAN_TAG_PRESENT */
>> + /* set otherwise. */
>> + } cvlan;
> We need to define structure for vlan tag key here for the pseudo code to work.
>
>> __be16 type; /* Ethernet frame type. */
>> } eth;
>> union {
>> diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c
>> index c92d6a2..af06683 100644
>> --- a/net/openvswitch/flow_netlink.c
>> +++ b/net/openvswitch/flow_netlink.c
>> @@ -811,6 +811,33 @@ static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
>> return 0;
>> }
>>
>> static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
>> u64 attrs, const struct nlattr **a,
>> bool is_mask, bool log)
>> @@ -845,7 +872,7 @@ static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
>> return -EINVAL;
>> }
>>
>> - SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
>> + SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
>> attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
>> }
>>
>> @@ -1064,6 +1091,86 @@ static void mask_set_nlattr(struct nlattr *attr, u8 val)
>> nlattr_set(attr, val, ovs_key_lens);
>> }
>>
>> +static int parse_vlan_from_nlattrs(const struct nlattr **nla,
>> + struct sw_flow_match *match,
>> + u64 *key_attrs, bool *ie_valid,
>> + const struct nlattr **a, bool is_mask,
>> + bool log)
>> +{
>> + int err;
>> + const struct nlattr *encap;
>> +
> ...
>
>> + u64 mask_v_attrs = 0;
>> +
>> + err = parse_flow_mask_nlattrs(*nla, a, &mask_v_attrs, log);
>> + if (err)
>> + return err;
>> +
>> + if (mask_v_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
>> + if (!*ie_valid) {
>> + OVS_NLERR(log, "Encap mask attribute is set for non-CVLAN frame.");
>> + err = -EINVAL;
>> + return err;
>> + }
>> + encap = a[OVS_KEY_ATTR_ENCAP];
>> +
>> + err = cust_vlan_from_nlattrs(match, a, is_mask, log);
>> + if (err)
>> + return err;
>> + *nla = encap;
>> +
> There is no checking for ATTR_VLAN or ATTR_ETHERTYPE. This can result
> in null pointer deference in cust_vlan_from_nlattrs().
The original vlan code does not check for these attribs in the masked
case. It does check for them in the non-masked case and then sets a
boolean and checks it in the masked case. I do the same thing for the
inner vlan. I check for the attributes in the non-masked case and set a
boolean and check the boolean in the masked case. Why is this not
sufficient?
>
>> + mask_v_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
>> + mask_v_attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
>> + mask_v_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
>> +
>> + *key_attrs |= mask_v_attrs;
>> + err = parse_flow_mask_nlattrs(*nla, a, key_attrs, log);
>> + if (err)
>> + return err;
>> + }
>> + }
>> + return 0;
>> +}
>> +
> ...
>
>> if (swkey->eth.type == htons(ETH_P_802_2)) {
>> /*
>> @@ -1525,6 +1659,8 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
>> unencap:
>> if (encap)
>> nla_nest_end(skb, encap);
>> + if (in_encap)
>> + nla_nest_end(skb, in_encap);
>>
> As pointed in last review, inner encap attribute should be terminated first.
Yes, I agree that inner should be first. Sorry I forgot to change that
after the last review.
_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev
next prev parent reply other threads:[~2015-10-13 17:39 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-10 23:40 [PATCH net-next V15 0/3] openvswitch: Add support for 802.1ad Thomas F Herbert
[not found] ` <1444520433-1958-1-git-send-email-thomasfherbert-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-10 23:40 ` [PATCH net-next V15 1/3] openvswitch: 802.1ad uapi changes Thomas F Herbert
2015-10-10 23:40 ` [PATCH net-next V15 3/3] 802.1AD: Flow handling, actions, vlan parsing and netlink attributes Thomas F Herbert
2015-10-13 6:47 ` Pravin Shelar
[not found] ` <CALnjE+qdqBrzZ_p6RCHLr63tSWTKPnTWhQuWsJ8dvAmq-bf9rQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-10-13 17:39 ` Thomas F Herbert [this message]
2015-10-13 18:14 ` Pravin Shelar
[not found] ` <CALnjE+rFgnRZ0awed1eox62zEeFKaCXoAucv6wS4MVBFK_RLRA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-10-15 12:50 ` Thomas F Herbert
2015-10-10 23:40 ` [PATCH net-next V15 2/3] Check for vlan ethernet types for 8021.q or 802.1ad Thomas F Herbert
2015-10-12 13:52 ` Sergei Shtylyov
2015-10-13 2:58 ` [PATCH net-next V15 0/3] openvswitch: Add support for 802.1ad David Miller
2015-10-13 6:48 ` Pravin Shelar
[not found] ` <20151012.195841.292062617131376194.davem-fT/PcQaiUtIeIZ0/mPfg9Q@public.gmane.org>
2015-10-13 8:47 ` Thomas F Herbert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=561D41BA.7030300@gmail.com \
--to=thomasfherbert-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=dev-yBygre7rU0TnMu66kgdUjQ@public.gmane.org \
--cc=netdev-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=pshelar-l0M0P4e3n4LQT0dZR+AlfA@public.gmane.org \
--cc=therbert-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).