summaryrefslogtreecommitdiffstats
path: root/src/trex_client_config.cpp
blob: f9ad0e6cad065c9ed94cd8ba8309d7c6f0358670 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 Itay Marom
 Cisco Systems, Inc.
*/

/*
Copyright (c) 2016 Cisco Systems, Inc.

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.
*/

#include <stdexcept>
#include <sstream>
#include <fstream>

#include <yaml-cpp/yaml.h>

#include "utl_yaml.h"
#include "trex_client_config.h"
#include "common/basic_utils.h"
#include "bp_sim.h"

void
ClientGroup::dump() const {

    std::cout << "IP start:       " << ip_to_str(m_ip_start) << "\n";
    std::cout << "IP end:         " << ip_to_str(m_ip_end) << "\n";

    std::cout << "Init. MAC addr: ";
    for (int i = 0; i < 6; i++) {
        printf("%lx:", ( (m_init_mac >> ( (6-i) * 8)) & 0xFF ) );
    }
    std::cout << "\n";

    std::cout << "Init. VLAN:     " << m_init_vlan << "\n";

    std::cout << "Res. MAC addr:  ";
    for (int i = 0; i < 6; i++) {
        printf("%lx:", ( (m_res_mac >> ( (6-i) * 8)) & 0xFF ) );
    }
    std::cout << "\n";

    std::cout << "Res. VLAN:      " << m_res_vlan << "\n";
}


/**
 * loads a YAML file containing 
 * the client groups configuration 
 * 
 */
void
ClientGroupsDB::load_yaml_file(const std::string &filename) {
    std::stringstream ss;

    m_filename = filename;

    if (!utl_is_file_exists(filename)){
        ss << "file does not exists";
        throw std::runtime_error(ss.str());
    }

    std::ifstream fin(filename);
    YAML::Parser parser(fin);
    YAML::Node root;

    parser.GetNextDocument(root);

    for (int i = 0; i < root.size(); i++) {
        const YAML::Mark &mark = root[i].GetMark();
        ClientGroup group;
        bool rc;

        /* ip_start */
        rc = utl_yaml_read_ip_addr(root[i], "ip_start", group.m_ip_start);
        if (!rc) {
            yaml_parse_err("'ip_start' field does not exists", &mark);
        }

        /* ip_end */
        rc = utl_yaml_read_ip_addr(root[i], "ip_end",   group.m_ip_end);
        if (!rc) {
            yaml_parse_err("'ip_end' field does not exists", &mark);
        }

        /* sanity check */
        if (group.m_ip_end <= group.m_ip_start) {
            yaml_parse_err("IP group range must be positive and include at least one entry", &mark);
        }

        /* init_mac */
        rc = utl_yaml_read_mac_addr(root[i], "init_mac", group.m_init_mac);
        if (!rc) {
            yaml_parse_err("'init_mac' field does not exists", &mark);
        }

        /* res_mac */
        rc = utl_yaml_read_mac_addr(root[i], "res_mac", group.m_res_mac);
        if (!rc) {
            yaml_parse_err("'res_mac' field does not exists", &mark);
        }


        root[i]["init_vlan"] >> group.m_init_vlan;
        root[i]["res_vlan"]  >> group.m_res_vlan;
        root[i]["count"]     >> group.m_count;


        /* add to map with copying */
        m_groups[group.m_ip_start] = group;
    }

    verify();

    m_is_empty = false;

}

void
ClientGroupsDB::verify() const {
    std::stringstream ss;
    uint32_t monotonic = 0;

    /* check that no interval overlaps */
    
    for (const auto &p : m_groups) {
        const ClientGroup &group = p.second;

        if ( (monotonic > 0 ) && (group.m_ip_start <= monotonic) ) {
            ss << "IP '" << ip_to_str(group.m_ip_start) << "' - '" << ip_to_str(group.m_ip_end) << "' overlaps with other groups";
            yaml_parse_err(ss.str());
        }

        monotonic = group.m_ip_end;
    }
}

/**
 * lookup function 
 * should be fast 
 * 
 */
ClientGroup *
ClientGroupsDB::lookup(uint32_t ip) {
    
    /* check the cache */
    if ( (m_cache_group) && (m_cache_group->contains(ip)) ) {
        return m_cache_group;
    }

    std::map<uint32_t ,ClientGroup>::iterator it;
    
    it = m_groups.upper_bound(ip);

    /* if the first element in the map is bigger - its not in the map */
    if (it == m_groups.begin()) {
        return NULL;
    }

    /* go one back - we know it's not on begin so we have at least one back */
    it--;

    ClientGroup &group = (*it).second;

    /* because this is a non overlapping intervals
       if IP exists it must be in this group
       because if it exists in some other previous group
       its end_range >= ip so start_range > ip
       and so start_range is the upper_bound
     */
    if (group.contains(ip)) {
        /* save as cache */
        m_cache_group = &group;
        return &group;
    } else {
        return NULL;
    }

}

ClientGroup *
ClientGroupsDB::lookup(const std::string &ip) {
    uint32_t addr = (uint32_t)inet_addr(ip.c_str());
    addr = PKT_NTOHL(addr);

    return lookup(addr);
}


void
ClientGroupsDB::yaml_parse_err(const std::string &err, const YAML::Mark *mark) const {
    std::stringstream ss;

    if (mark) {
        ss << "\n*** '" << m_filename << "' - YAML parsing error at line " << mark->line << ": ";
    } else {
        ss << "\n*** '" << m_filename << "' - YAML parsing error: ";
    }

    ss << err;
    throw std::runtime_error(ss.str());
}