Beispiel #1
0
    def test_layer_4_checksums(self):

        #: :type port: xenavalkyrie.xena_port.XenaPort
        port = self.xm.session.reserve_ports([self.port1],
                                             force=False,
                                             reset=True)[self.port1]

        #: :type tcp_stream: xenavalkyrie.xena_stream.XenaStream
        tcp_stream = port.add_stream('tcp stream')

        eth = Ethernet(src_s='22:22:22:22:22:22')
        eth.dst_s = '11:11:11:11:11:11'
        vlan = Dot1Q(vid=17, prio=3)
        eth.vlan.append(vlan)
        ip = IP()
        tcp = TCP()
        headers = eth + ip + tcp
        tcp_stream.set_packet_headers(headers, l4_checksum=False)
        headerprotocol = tcp_stream.get_attribute('ps_headerprotocol')
        assert 'tcpcheck' not in headerprotocol.lower()
        tcp_stream.set_packet_headers(headers, l4_checksum=True)
        headerprotocol = tcp_stream.get_attribute('ps_headerprotocol')
        assert 'tcpcheck' in headerprotocol.lower()
        resulting_headers = tcp_stream.get_packet_headers()
        l4 = resulting_headers.upper_layer.upper_layer
        assert l4.sum == 0

        #: :type udp_stream: xenavalkyrie.xena_stream.XenaStream
        udp_stream = port.add_stream('udp stream')

        eth = Ethernet(src_s='44:44:44:44:44:44')
        eth.dst_s = '33:33:33:33:33:33'
        ip6 = IP6()
        udp = UDP()
        headers = eth + ip6 + udp
        udp_stream.set_packet_headers(headers, l4_checksum=False)
        headerprotocol = udp_stream.get_attribute('ps_headerprotocol')
        assert 'udpcheck' not in headerprotocol.lower()
        udp_stream.set_packet_headers(headers, l4_checksum=True)
        headerprotocol = udp_stream.get_attribute('ps_headerprotocol')
        assert 'udpcheck' in headerprotocol.lower()
        resulting_headers = udp_stream.get_packet_headers()
        l4 = resulting_headers.upper_layer.upper_layer
        assert l4.sum == 0
Beispiel #2
0
 def create_mac_obj(cls, utg_mac):
     headerObj = Ethernet(src_s=utg_mac.sa.value)
     headerObj.dst_s = utg_mac.da.value
     l2_proto = utg_mac._l2_proto
     if l2_proto == TGEnums.L2_PROTO.ETHERNETII:
         if utg_mac._ethertype._current_val != utg_mac._ethertype._default_val:
             headerObj.type = int(utg_mac._ethertype._current_val, 16)
     else:
         headerObj.type = None
     return headerObj
Beispiel #3
0
 def _apply_mac(self):
     pass  # TODO PACKET BUILDER
     eth = Ethernet(src_s=self.packet.mac.sa.value)
     eth.dst_s = self.packet.mac.da.value
     #TODO fix ethertype
     if self.packet.l2_proto == TGEnums.L2_PROTO.ETHERNETII:
         if self.packet.ethertype != self.packet._ethertype._default_val:
             eth.type = int(self.packet.ethertype, 16)
     else:
         eth.type = None
     self._headers = eth
     self._config_field_modifier(4, self.packet.mac.da)
     self._config_field_modifier(10, self.packet.mac.sa)
     if len(self.packet.vlans) > 0:
         for vlan in self.packet.vlans:
             vid = int(self.packet.vlans[vlan].vid.value)
             cfi = int(self.packet.vlans[vlan].cfi)
             prio = self.packet.vlans[vlan].priority
             vlanObj = Dot1Q(vid=vid, cfi=cfi, prio=prio)
             eth.vlan.append(vlanObj)
Beispiel #4
0
def configuration():
    """ Reserve ports.
        Wait for ports up.
        Load configuration on one port.
        Build configuration on the second port.
    """

    global ports

    #: :type header: pypacker.layer12.ethernet
    #: :type p0_s0: xenavalkyrie.xena_stream.XenaStream
    #: :type p1_s0: xenavalkyrie.xena_stream.XenaStream
    #: :type modifier: xenavalkyrie.xena_strea.XenaModifier

    ports = xm.session.reserve_ports([port0, port1], True)
    ports[port0].wait_for_up(16)
    ports[port1].wait_for_up(16)

    # Load configuration on port-0.
    ports[port0].load_config(config0)

    # Get port-0/stream-0 object.
    p0_s0 = ports[port0].streams[0]

    # Get Multi-parameter query with get_attributes which returns all attributes values as dict.
    ps_config = p0_s0.get_attributes()
    print('{} info:\n{}'.format(p0_s0.name, json.dumps(ps_config, indent=1)))

    # Get packet headers.
    headers = p0_s0.get_packet_headers()
    print('{} headers:\n{}'.format(p0_s0.name, headers))
    # Access any header and field by name with nice string representation.
    print('{} MAC SRC: {}'.format(p0_s0.name, headers.src_s))
    print('{} VLAN ID: {}'.format(p0_s0.name, headers.vlan[0].vid))
    print('{} IP DST: {}'.format(p0_s0.name, headers.ip.dst_s))

    # Add stream on port-1
    p1_s0 = ports[port1].add_stream('new stream')

    # Set ps_packetlimit and ps_ratepps with set_attributes which sets list of attributes.
    p1_s0.set_attributes(ps_packetlimit=80, ps_ratepps=10)

    # Get single parameter query with get_attribute which returns the attribute value as str.
    ps_packetlimit = p1_s0.get_attribute('ps_packetlimit')
    ps_ratepps = p1_s0.get_attribute('ps_ratepps')
    print('{} info:\nps_packetlimit: {}\nps_ratepps: {}'.format(
        p1_s0.name, ps_packetlimit, ps_ratepps))

    # Set headers - all fields can be set with the constructor or by direct access after creation.
    eth = Ethernet(src_s='22:22:22:22:22:22')
    eth.dst_s = '11:11:11:11:11:11'
    vlan = Dot1Q(vid=17)
    eth.vlan.append(vlan)
    # In order to add header simply concatenate it.
    ip6 = IP6()
    tcp = TCP()
    headers = eth + ip6 + tcp
    p1_s0.set_packet_headers(headers)

    # Add modifier - all parameters can be set with the constructor or by direct access after creation.
    modifier = p1_s0.add_modifier(position=4)
    modifier.min_val = 100
    modifier.max_val = 200

    # Save new configuration.
    ports[port1].save_config(save_config)