예제 #1
0
def get_vlan_info(p):
    """Collect vlan stage information from matches/modifications in the
    policy. Returns a set of tuples, where each tuple is of the form

    (offset, nbits, total_stages)

    that occurred in the policy. In general there should be at most one element
    in this set! """
    from pyretic.core.language import (parallel, sequential, DerivedPolicy,
                                       match, modify, _match, _modify)
    if isinstance(p, match) or isinstance(p, modify):
        fmap = (_match(**p.map).map if isinstance(p, match) else _modify(
            **p.map).map)
        if 'vlan_id' in fmap:
            if 'vlan_offset' not in fmap:
                print "Invalid fmap:", fmap
                print p
            return set([(fmap['vlan_offset'], fmap['vlan_nbits'],
                         fmap['vlan_total_stages'])])
        else:
            return set()
    elif isinstance(p, parallel) or isinstance(p, sequential):
        return reduce(lambda acc, x: acc | get_vlan_info(x), p.policies, set())
    elif isinstance(p, DerivedPolicy):
        return get_vlan_info(p.policy)
    else:
        return set()
예제 #2
0
파일: netkat.py 프로젝트: 15ramky/pyretic
def get_vlan_info(p):
    """Collect vlan stage information from matches/modifications in the
    policy. Returns a set of tuples, where each tuple is of the form

    (offset, nbits, total_stages)

    that occurred in the policy. In general there should be at most one element
    in this set! """
    from pyretic.core.language import (parallel, sequential,
                                       DerivedPolicy, match, modify, _match,
                                       _modify)
    if isinstance(p, match) or isinstance(p, modify):
        fmap = (_match(**p.map).map if isinstance(p, match) else
                _modify(**p.map).map)
        if 'vlan_id' in fmap:
            if 'vlan_offset' not in fmap:
                print "Invalid fmap:", fmap
                print p
            return set([(fmap['vlan_offset'], fmap['vlan_nbits'],
                         fmap['vlan_total_stages'])])
        else:
            return set()
    elif isinstance(p, parallel) or isinstance(p, sequential):
        return reduce(lambda acc, x: acc | get_vlan_info(x), p.policies, set())
    elif isinstance(p, DerivedPolicy):
        return get_vlan_info(p.policy)
    else:
        return set()
예제 #3
0
파일: netkat.py 프로젝트: 15ramky/pyretic
def to_pol(p):
  from pyretic.core.language import (match, modify, identity, drop, negate, union,
                                     parallel, intersection, ingress_network,
                                     egress_network, sequential, fwd, if_,
                                     FwdBucket, DynamicPolicy, DerivedPolicy,
                                     Controller, _modify, CountBucket)
  from pyretic.lib.path import QuerySwitch
  from pyretic.lib.netflow import NetflowBucket
  from pyretic.evaluations.Tests.common_modules.stanford_forwarding import StanfordForwarding
  if isinstance(p, match):
    return mk_filter(to_pred(p))
  elif p is identity:
    return mk_filter({ "type": "true" })
  elif p is drop:
    return mk_filter({ "type": "false" })
  elif isinstance(p, modify):
    return mod_to_pred(_modify(**p.map).map)
  elif isinstance(p, negate):
    return mk_filter(to_pred(p))
  elif isinstance(p, union):
    return mk_filter(to_pred(p))
  elif isinstance(p, parallel):
    return mk_union(map(to_pol, p.policies))
  #elif isinstance(p, disjoint):
    #return mk_disjoint(map(to_pol, p.policies))
  elif isinstance(p, intersection):
    return mk_filter(to_pred(p))
  elif isinstance(p, sequential):
    return mk_seq(map(to_pol, p.policies))
  elif isinstance(p, fwd):
    return mk_mod(mk_header("location", physical(p.outport)))
  elif isinstance(p, if_):
    c = to_pred(p.pred)
    return mk_union([mk_seq([mk_filter(c), to_pol(p.t_branch)]),
                     mk_seq([mk_filter({ "type": "neg", "pred": c }), to_pol(p.f_branch)])])    
  elif isinstance(p, FwdBucket) or p is Controller:
      return {"type" : "mod", "header" : "location", "value": {"type" : "pipe", "name" : str(id(p))}}
  elif isinstance(p, CountBucket) or isinstance(p, NetflowBucket):
      return {"type" : "mod", "header" : "location", "value": {"type" : "query", "name" : str(id(p))}}
  elif isinstance(p, ingress_network) or isinstance(p, egress_network) or isinstance(p, DynamicPolicy):
      return to_pol(p.policy)
  elif isinstance(p, DerivedPolicy):
      return to_pol(p.policy)
  elif isinstance(p, QuerySwitch):
      # TODO: is there a neater way of incorporating QuerySwitch?
      return to_pol(cls_to_pol(p.netkat_compile()[0]))
  elif isinstance(p, StanfordForwarding):
      return to_pol(cls_to_pol(p.compile()))
  else:
      raise TypeError("unknown policy %s %s" % (type(p), repr(p)))
예제 #4
0
def to_pol(p):
  from pyretic.core.language import (match, modify, identity, drop, negate, union,
                                     parallel, intersection, ingress_network,
                                     egress_network, sequential, fwd, if_,
                                     FwdBucket, DynamicPolicy, DerivedPolicy,
                                     Controller, _modify, CountBucket)
  from pyretic.lib.netflow import NetflowBucket
  if isinstance(p, match):
    return mk_filter(to_pred(p))
  elif p == identity:
    return mk_filter({ "type": "true" })
  elif p == drop:
    return mk_filter({ "type": "false" })
  elif isinstance(p, modify):
    return mod_to_pred(_modify(**p.map).map)
  elif isinstance(p, negate):
    return mk_filter(to_pred(p))
  elif isinstance(p, union):
    return mk_filter(to_pred(p))
  elif isinstance(p, parallel):
    return mk_union(map(to_pol, p.policies))
  #elif isinstance(p, disjoint):
    #return mk_disjoint(map(to_pol, p.policies))
  elif isinstance(p, intersection):
    return mk_filter(to_pred(p))
  elif isinstance(p, sequential):
    return mk_seq(map(to_pol, p.policies))
  elif isinstance(p, fwd):
    return mk_mod(mk_header("location", physical(p.outport)))
  elif isinstance(p, if_):
    c = to_pred(p.pred)
    return mk_union([mk_seq([mk_filter(c), to_pol(p.t_branch)]),
                     mk_seq([mk_filter({ "type": "neg", "pred": c }), to_pol(p.f_branch)])])    
  elif isinstance(p, FwdBucket) or p is Controller:
      return {"type" : "mod", "header" : "location", "value": {"type" : "pipe", "name" : str(id(p))}}
  elif isinstance(p, CountBucket) or isinstance(p, NetflowBucket):
      return {"type" : "mod", "header" : "location", "value": {"type" : "query", "name" : str(id(p))}}
  elif isinstance(p, ingress_network) or isinstance(p, egress_network) or isinstance(p, DynamicPolicy):
      return to_pol(p.policy)
  elif isinstance(p, DerivedPolicy):
      return to_pol(p.policy)
  else:
    raise TypeError("unknown policy %s %s" % (type(p), repr(p)))
예제 #5
0
def sample_vuntagging(sw_ports, network_links):
    """ Temporary helper equivalent of virtual_untagging() policy. """
    edge_net = get_hsa_edge_policy(sw_ports, network_links)
    return ((edge_net >> _modify(vlan_id=0, vlan_pcp=0)) + ~edge_net)
예제 #6
0
def to_pol(p):
    from pyretic.core.language import (match, modify, identity, drop, negate,
                                       union, parallel, intersection,
                                       ingress_network, egress_network,
                                       sequential, fwd, if_, FwdBucket,
                                       DynamicPolicy, DerivedPolicy,
                                       Controller, _modify, CountBucket)
    from pyretic.lib.path import QuerySwitch
    from pyretic.lib.netflow import NetflowBucket
    from pyretic.evaluations.Tests.common_modules.stanford_forwarding import StanfordForwarding
    if isinstance(p, match):
        return mk_filter(to_pred(p))
    elif p is identity:
        return mk_filter({"type": "true"})
    elif p is drop:
        return mk_filter({"type": "false"})
    elif isinstance(p, modify):
        return mod_to_pred(_modify(**p.map).map)
    elif isinstance(p, negate):
        return mk_filter(to_pred(p))
    elif isinstance(p, union):
        return mk_filter(to_pred(p))
    elif isinstance(p, parallel):
        return mk_union(map(to_pol, p.policies))
    #elif isinstance(p, disjoint):
    #return mk_disjoint(map(to_pol, p.policies))
    elif isinstance(p, intersection):
        return mk_filter(to_pred(p))
    elif isinstance(p, sequential):
        return mk_seq(map(to_pol, p.policies))
    elif isinstance(p, fwd):
        return mk_mod(mk_header("location", physical(p.outport)))
    elif isinstance(p, if_):
        c = to_pred(p.pred)
        return mk_union([
            mk_seq([mk_filter(c), to_pol(p.t_branch)]),
            mk_seq([mk_filter({
                "type": "neg",
                "pred": c
            }), to_pol(p.f_branch)])
        ])
    elif isinstance(p, FwdBucket) or p is Controller:
        return {
            "type": "mod",
            "header": "location",
            "value": {
                "type": "pipe",
                "name": str(id(p))
            }
        }
    elif isinstance(p, CountBucket) or isinstance(p, NetflowBucket):
        return {
            "type": "mod",
            "header": "location",
            "value": {
                "type": "query",
                "name": str(id(p))
            }
        }
    elif isinstance(p, ingress_network) or isinstance(
            p, egress_network) or isinstance(p, DynamicPolicy):
        return to_pol(p.policy)
    elif isinstance(p, DerivedPolicy):
        return to_pol(p.policy)
    elif isinstance(p, QuerySwitch):
        # TODO: is there a neater way of incorporating QuerySwitch?
        return to_pol(cls_to_pol(p.netkat_compile()[0]))
    elif isinstance(p, StanfordForwarding):
        return to_pol(cls_to_pol(p.compile()))
    else:
        raise TypeError("unknown policy %s %s" % (type(p), repr(p)))