Coder Social home page Coder Social logo

bitwise-range's Introduction

DONATE

My Paypal / Alipay: [email protected]

bitwise-range

OpenFlow and Open vSwitch support bitwise match on individual fields, but don't support range match directly.

man ovs-fields:

Range matches can be expressed as a collection of bitwise matches.

This piece of code works for this. It transforms a range of number (nonnegative integer) into a set of bitwise stuff. The number of results is designed to be as few as possible.

There is also an encapsulation for IPv4 address range.

corresponding content in ovs-fields manual

    Some  types of matches on individual fields cannot be expressed directly with OpenFlow and
    Open vSwitch. These can be expressed indirectly.

        Range match, e.g. ``1000 ≤ tcp_dst ≤ 1999’’
            The value of the field must lie within a numerical range, for  example,  TCP
            destination ports between 1000 and 1999.

            Range matches can be expressed as a collection of bitwise matches. For exam‐
            ple, suppose that the goal is to match TCP source ports 1000 to 1999, inclu‐
            sive. The binary representations of 1000 and 1999 are:

            01111101000
            11111001111

            The following series of bitwise matches will match 1000 and 1999 and all the
            values in between:

            01111101xxx
            0111111xxxx
            10xxxxxxxxx
            110xxxxxxxx
            1110xxxxxxx
            11110xxxxxx
            1111100xxxx

            which can be written as the following matches:

            tcp,tp_src=0x03e8/0xfff8
            tcp,tp_src=0x03f0/0xfff0
            tcp,tp_src=0x0400/0xfe00
            tcp,tp_src=0x0600/0xff00
            tcp,tp_src=0x0700/0xff80
            tcp,tp_src=0x0780/0xffc0
            tcp,tp_src=0x07c0/0xfff0

example

>>> from bitwise_range import BitwiseRange, BitwiseIPv4Range

>>> BitwiseRange(0b0100, 0b1110, 4).print_result_x()
01xx
10xx
110x
1110

>>> BitwiseRange(1000, 1999).print_result_x()
01111101xxx
0111111xxxx
10xxxxxxxxx
110xxxxxxxx
1110xxxxxxx
11110xxxxxx
1111100xxxx

>>> BitwiseRange(1000, 1999, 16).print_result_hex()
0x3e8/0xfff8
0x3f0/0xfff0
0x400/0xfe00
0x600/0xff00
0x700/0xff80
0x780/0xffc0
0x7c0/0xfff0

>>> BitwiseIPv4Range("10.0.1.0", "10.0.2.100").print_result_ipv4()
10.0.1.0/24
10.0.2.0/26
10.0.2.64/27
10.0.2.96/30
10.0.2.100/32

bitwise-range's People

Contributors

wangsitan avatar

Stargazers

Alex Xuan avatar

Watchers

James Cloos avatar  avatar

Forkers

heshengyuan1311

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.