aboutsummaryrefslogtreecommitdiffstats
path: root/resources/traffic_scripts/arp_request.py
blob: 8c5b9c7c47581fc8e95424a17acf804f336c727c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
6
#!/usr/bin/env python

# Copyright (c) 2016 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Send an ARP request and verify the reply"""

import sys

from scapy.all import Ether, ARP

from resources.libraries.python.PacketVerifier import Interface
from resources.libraries.python.TrafficScriptArg import TrafficScriptArg


def parse_arguments():
    """Parse arguments of the script passed through command line

    :return: tuple of parsed arguments
    """
    args = TrafficScriptArg(['src_if', 'src_mac', 'dst_mac',
                             'src_ip', 'dst_ip'])

    # check for mandatory parameters
    params = (args.get_arg('tx_if'),
              args.get_arg('src_mac'),
              args.get_arg('dst_mac'),
              args.get_arg('src_ip'),
              args.get_arg('dst_ip'))
    if None in params:
        raise Exception('Missing mandatory parameter(s)!')

    return params


def arp_request_test():
    """Send ARP request, expect a reply and verify its fields.

    returns: test status
    :raises RuntimeError: ARP reply timeout.
    """
    test_passed = False
    (src_if, src_mac, dst_mac, src_ip, dst_ip) = parse_arguments()

    interface = Interface(src_if)

    # build an ARP request
    arp_request = (Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') /
                   ARP(psrc=src_ip, hwsrc=src_mac, pdst=dst_ip,
                       hwdst='ff:ff:ff:ff:ff:ff'))

    # send the request
    interface.send_pkt(arp_request)

    try:
        # wait for APR reply
        ether = interface.recv_pkt()

        if not ether:
            raise RuntimeError("ARP reply timeout")

        # verify received packet

        if not ether.haslayer(ARP):
            raise RuntimeError('Unexpected packet: does not contain ARP ' +
                               'header "{}"'.format(ether.__repr__()))

        arp = ether['ARP']
        arp_reply = 2

        if arp.op != arp_reply:
            raise RuntimeError('expected op={}, received {}'.format(arp_reply,
                                                                    arp.op))
        if arp.ptype != 0x800:
            raise RuntimeError('expected ptype=0x800, received {}'.
                               format(arp.ptype))
        if arp.hwlen != 6:
            raise RuntimeError('expected hwlen=6, received {}'.
                               format(arp.hwlen))
        if arp.plen != 4:
            raise RuntimeError('expected plen=4, received {}'.format(arp.plen))
        if arp.hwsrc != dst_mac:
            raise RuntimeError('expected hwsrc={}, received {}'.
                               format(dst_mac, arp.hwsrc))
        if arp.psrc != dst_ip:
            raise RuntimeError('expected psrc={}, received {}'.
                               format(dst_ip, arp.psrc))
        if arp.hwdst != src_mac:
            raise RuntimeError('expected hwdst={}, received {}'.
                               format(src_mac, arp.hwdst))
        if arp.pdst != src_ip:
            raise RuntimeError('expected pdst={}, received {}'.
                               format(src_ip, arp.pdst))
        test_passed = True

    except RuntimeError as ex:
        print 'Error occurred: {}'.format(ex)

    return test_passed


def main():
    """Run the test and collect result"""
    if arp_request_test():
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == '__main__':
    main()
ss="o">->rx_count = clib_min (vec_len (vd->rxqs), VMXNET3_RXQ_MAX); vec_foreach_index (qid, vd->rxqs) { vmxnet3_rxq_t *rxq = vec_elt_at_index (vd->rxqs, qid); vl_api_vmxnet3_rx_list_t *rx_list = &mp->rx_list[qid]; ASSERT (qid < VMXNET3_RXQ_MAX); rx_list->rx_qsize = htons (rxq->size); rx_list->rx_next = htons (rxq->rx_comp_ring.next); for (rid = 0; rid < VMXNET3_RX_RING_SIZE; rid++) { ring = &rxq->rx_ring[rid]; rx_list->rx_fill[rid] = htons (ring->fill); rx_list->rx_produce[rid] = htons (ring->produce); rx_list->rx_consume[rid] = htons (ring->consume); } } mp->tx_count = clib_min (vec_len (vd->txqs), VMXNET3_TXQ_MAX); vec_foreach_index (qid, vd->txqs) { vmxnet3_txq_t *txq = vec_elt_at_index (vd->txqs, qid); vl_api_vmxnet3_tx_list_t *tx_list = &mp->tx_list[qid]; ASSERT (qid < VMXNET3_TXQ_MAX); tx_list->tx_qsize = htons (txq->size); tx_list->tx_next = htons (txq->tx_comp_ring.next); tx_list->tx_produce = htons (txq->tx_ring.produce); tx_list->tx_consume = htons (txq->tx_ring.consume); } vl_api_send_msg (reg, (u8 *) mp); } /** * @brief Message handler for vmxnet3_dump API. * @param mp vl_api_vmxnet3_dump_t * mp the api message */ static void vl_api_vmxnet3_dump_t_handler (vl_api_vmxnet3_dump_t * mp) { vmxnet3_main_t *vmxm = &vmxnet3_main; vnet_main_t *vnm = vnet_get_main (); vnet_sw_interface_t *swif; vmxnet3_device_t *vd; u8 *if_name = 0; vl_api_registration_t *reg; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* *INDENT-OFF* */ pool_foreach (vd, vmxm->devices, ({ swif = vnet_get_sw_interface (vnm, vd->sw_if_index); if_name = format (if_name, "%U%c", format_vnet_sw_interface_name, vnm, swif, 0); send_vmxnet3_details (reg, vd, swif, if_name, mp->context); _vec_len (if_name) = 0; })); /* *INDENT-ON* */ vec_free (if_name); } #define vl_msg_name_crc_list #include <vmxnet3/vmxnet3_all_api_h.h> #undef vl_msg_name_crc_list static void setup_message_id_table (vmxnet3_main_t * vmxm, api_main_t * am) { #define _(id,n,crc) \ vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + vmxm->msg_id_base); foreach_vl_msg_name_crc_vmxnet3; #undef _ } /* set tup the API message handling tables */ clib_error_t * vmxnet3_plugin_api_hookup (vlib_main_t * vm) { vmxnet3_main_t *vmxm = &vmxnet3_main; api_main_t *am = &api_main; u8 *name; /* construct the API name */ name = format (0, "vmxnet3_%08x%c", api_version, 0); /* ask for a correctly-sized block of API message decode slots */ vmxm->msg_id_base = vl_msg_api_get_msg_ids ((char *) name, VL_MSG_FIRST_AVAILABLE); #define _(N,n) \ vl_msg_api_set_handlers((VL_API_##N + vmxm->msg_id_base), \ #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1); foreach_vmxnet3_plugin_api_msg; #undef _ /* set up the (msg_name, crc, message-id) table */ setup_message_id_table (vmxm, am); vec_free (name); return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */