netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel T. Lee" <danieltimlee@gmail.com>
To: Jesper Dangaard Brouer <brouer@redhat.com>
Cc: "David S . Miller" <davem@davemloft.net>,
	netdev <netdev@vger.kernel.org>
Subject: Re: [v2 2/3] samples: pktgen: add helper functions for IP(v4/v6) CIDR parsing
Date: Sun, 15 Sep 2019 00:02:37 +0900	[thread overview]
Message-ID: <CAEKGpzjgp2+XLd340MRPJ9FSHPusb13zjZafTvRrSuRy3kOgjw@mail.gmail.com> (raw)
In-Reply-To: <20190913144305.4bf38c04@carbon>

On Fri, Sep 13, 2019 at 9:43 PM Jesper Dangaard Brouer
<brouer@redhat.com> wrote:
>
> On Thu, 12 Sep 2019 03:48:06 +0900
> "Daniel T. Lee" <danieltimlee@gmail.com> wrote:
>
> > This commit adds CIDR parsing and IP validate helper function to parse
> > single IP or range of IP with CIDR. (e.g. 198.18.0.0/15)
> >
> > Helpers will be used in prior to set target address in samples/pktgen.
> >
> > Signed-off-by: Daniel T. Lee <danieltimlee@gmail.com>
> > ---
> >  samples/pktgen/functions.sh | 122 ++++++++++++++++++++++++++++++++++++
> >  1 file changed, 122 insertions(+)
> >
> > diff --git a/samples/pktgen/functions.sh b/samples/pktgen/functions.sh
> > index 4af4046d71be..8be5a6b6c097 100644
> [...]
>
> > +# Given a single IP(v4/v6) or CIDR, return minimum and maximum IP addr.
> > +function parse_addr()
> > +{
> > +    # check function is called with (funcname)6
> > +    [[ ${FUNCNAME[1]: -1} == 6 ]] && local IP6=6
> > +    local bitlen=$[ IP6 ? 128 : 32 ]
> > +    local octet=$[ IP6 ? 16 : 8 ]
> > +
> > +    local addr=$1
> > +    local net prefix
> > +    local min_ip max_ip
> > +
> > +    IFS='/' read net prefix <<< $addr
> > +    [[ $IP6 ]] && net=$(extend_addr6 $net)
> > +    validate_addr$IP6 $net
> > +
> > +    if [[ $prefix -gt $bitlen ]]; then
> > +        err 5 "Invalid prefix: $prefix"
> > +    elif [[ -z $prefix ]]; then
> > +        min_ip=$net
> > +        max_ip=$net
> > +    else
> > +        # defining array for converting Decimal 2 Binary
> > +        # 00000000 00000001 00000010 00000011 00000100 ...
> > +        local d2b='{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}{0..1}'
> > +        [[ $IP6 ]] && d2b+=$d2b
> > +        eval local D2B=($d2b)
>
> I must say this is a rather cool shell/bash trick to use an array for
> converting decimal numbers into binary.
>

Thank you for the compliment and for the detailed review.

> > +
> > +        local shift=$[ bitlen-prefix ]
>
> Using a variable named 'shift' is slightly problematic for shell/bash
> code.  It works, but it is just confusing.
>
> > +        local min_mask max_mask
> > +        local min max
> > +        local ip_bit
> > +        local ip sep
> > +
> > +        # set separator for each IP(v4/v6)
> > +        [[ $IP6 ]] && sep=: || sep=.
> > +        IFS=$sep read -ra ip <<< $net
> > +
> > +        min_mask="$(printf '1%.s' $(seq $prefix))$(printf '0%.s' $(seq $shift))"
> > +        max_mask="$(printf '0%.s' $(seq $prefix))$(printf '1%.s' $(seq $shift))"
>
> Also a surprising shell trick to get binary numbers out of a prefix number.
>
> > +
> > +        # calculate min/max ip with &,| operator
> > +        for i in "${!ip[@]}"; do
> > +            digit=$[ IP6 ? 16#${ip[$i]} : ${ip[$i]} ]
> > +            ip_bit=${D2B[$digit]}
> > +
> > +            idx=$[ octet*i ]
> > +            min[$i]=$[ 2#$ip_bit & 2#${min_mask:$idx:$octet} ]
> > +            max[$i]=$[ 2#$ip_bit | 2#${max_mask:$idx:$octet} ]
> > +            [[ $IP6 ]] && { min[$i]=$(printf '%X' ${min[$i]});
> > +                            max[$i]=$(printf '%X' ${max[$i]}); }
> > +        done
> > +
> > +        min_ip=$(IFS=$sep; echo "${min[*]}")
> > +        max_ip=$(IFS=$sep; echo "${max[*]}")
> > +    fi
> > +
> > +    echo $min_ip $max_ip
> > +}
>
> If you just fix the variable name 'shift' to something else, then I'm
> happy with this patch.
>
> Again, I'm very impressed with your shell/bash skills, I were certainly
> challenged when reviewing this :-)
>

I'll change the variable name to 'remain'.

Once again, I really appreciate your time and effort for the review.
Thank you.

Best,
Daniel

> --
> Best regards,
>   Jesper Dangaard Brouer
>   MSc.CS, Principal Kernel Engineer at Red Hat
>   LinkedIn: http://www.linkedin.com/in/brouer

  reply	other threads:[~2019-09-14 15:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-11 18:48 [v2 1/3] samples: pktgen: make variable consistent with option Daniel T. Lee
2019-09-11 18:48 ` [v2 2/3] samples: pktgen: add helper functions for IP(v4/v6) CIDR parsing Daniel T. Lee
2019-09-12 15:59   ` Jesper Dangaard Brouer
2019-09-12 17:53     ` Daniel T. Lee
2019-09-13 12:32       ` Jesper Dangaard Brouer
2019-09-13 12:43   ` Jesper Dangaard Brouer
2019-09-14 15:02     ` Daniel T. Lee [this message]
2019-09-11 18:48 ` [v2 3/3] samples: pktgen: allow to specify destination IP range (CIDR) Daniel T. Lee
2019-09-13 12:31   ` Jesper Dangaard Brouer
2019-09-13 12:37     ` Toke Høiland-Jørgensen
2019-09-14 14:51       ` Daniel T. Lee
2019-09-13 12:04 ` [v2 1/3] samples: pktgen: make variable consistent with option Jesper Dangaard Brouer

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=CAEKGpzjgp2+XLd340MRPJ9FSHPusb13zjZafTvRrSuRy3kOgjw@mail.gmail.com \
    --to=danieltimlee@gmail.com \
    --cc=brouer@redhat.com \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.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).