pre_test.h revision 5927d1dd
1/*
2  Ido Barnea
3  Cisco Systems, Inc.
4*/
5
6/*
7  Copyright (c) 2016-2016 Cisco Systems, Inc.
8
9  Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12
13  http://www.apache.org/licenses/LICENSE-2.0
14
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20*/
21
22#ifndef __PRE_TEST_H__
23#define __PRE_TEST_H__
24
25#include <iostream>
26#include <common/Network/Packet/Arp.h>
27#include <common/Network/Packet/MacAddress.h>
28#include "bp_sim.h"
29#include "trex_defs.h"
30
31class CPreTestStats {
32 public:
33    uint32_t m_rx_arp; // how many ARP packets we received
34    uint32_t m_tx_arp; // how many ARP packets we sent
35
36 public:
37    void clear() {
38        m_rx_arp = 0;
39        m_tx_arp = 0;
40    }
41};
42
43class CPretestOnePortInfo {
44    friend class CPretest;
45    enum CPretestOnePortInfoStates {
46        RESOLVE_NEEDED,
47        RESOLVE_NOT_NEEDED,
48    };
49
50 public:
51    CPretestOnePortInfo();
52    ~CPretestOnePortInfo();
53    void add_src(uint32_t ip, uint16_t vlan, MacAddress mac);
54    void add_dst(uint32_t ip, uint16_t vlan);
55    void add_src(uint16_t ip[8], uint16_t vlan, MacAddress mac);
56    void add_dst(uint16_t ip[8], uint16_t vlan);
57    bool get_mac(uint32_t ip, uint16_t vlan, uint8_t *mac);
58    bool get_mac(uint16_t ip[8], uint16_t vlan, uint8_t *mac);
59    bool get_mac(COneIPInfo *ip, uint8_t *mac);
60    COneIPInfo *get_src(uint16_t vlan, uint8_t ip_ver);
61    void set_port_id(uint16_t port_id)  {m_port_id = port_id;}
62    void dump(FILE *fd, char *offset);
63    bool is_loopback() {return m_is_loopback;}
64    CPreTestStats get_stats() {return m_stats;}
65    bool resolve_needed();
66    void send_grat_arp_all();
67    void send_arp_req_all();
68
69 private:
70    COneIPv4Info *find_ip(uint32_t ip, uint16_t vlan);
71    COneIPv4Info *find_next_hop(uint32_t ip, uint16_t vlan);
72    COneIPv6Info *find_ipv6(uint16_t *ip, uint16_t vlan);
73    bool get_mac(COneIPInfo *ip, uint16_t vlan, uint8_t *mac, uint8_t ip_ver);
74
75 private:
76    bool m_is_loopback;
77    CPretestOnePortInfoStates m_state;
78    CPreTestStats m_stats;
79    uint16_t m_port_id;
80    std::vector<COneIPInfo *> m_src_info;
81    std::vector<COneIPInfo *> m_dst_info;
82};
83
84class CPretest {
85 public:
86    CPretest(uint16_t max_ports) {
87        m_max_ports = max_ports;
88        for (int i =0; i < max_ports; i++) {
89            m_port_info[i].set_port_id(i);
90        }
91    }
92    void add_ip(uint16_t port, uint32_t ip, uint16_t vlan, MacAddress src_mac);
93    void add_ip(uint16_t port, uint32_t ip, MacAddress src_mac);
94    void add_next_hop(uint16_t port, uint32_t ip, uint16_t vlan);
95    void add_next_hop(uint16_t port, uint32_t ip);
96    void add_ip(uint16_t port, uint16_t ip[8], uint16_t vlan, MacAddress src_mac);
97    void add_ip(uint16_t port, uint16_t ip[8], MacAddress src_mac);
98    void add_next_hop(uint16_t port, uint16_t ip[8], uint16_t vlan);
99    void add_next_hop(uint16_t port, uint16_t ip[8]);
100    bool get_mac(uint16_t port, uint32_t ip, uint16_t vlan, uint8_t *mac);
101    bool get_mac(uint16_t port, uint16_t ip[8], uint16_t vlan, uint8_t *mac);
102    CPreTestStats get_stats(uint16_t port_id);
103    bool is_loopback(uint16_t port);
104    bool resolve_all();
105    void send_arp_req_all();
106    void send_grat_arp_all();
107    bool is_arp(const uint8_t *p, uint16_t pkt_size, ArpHdr *&arp, uint16_t &vlan_tag);
108    void get_results(CManyIPInfo &resolved_ips);
109    void dump(FILE *fd);
110    void test();
111
112 private:
113    int handle_rx(int port, int queue_id);
114
115 private:
116    CPretestOnePortInfo m_port_info[TREX_MAX_PORTS];
117    uint16_t m_max_ports;
118};
119
120#endif
121