/
learn_client.cpp
103 lines (80 loc) · 3.06 KB
/
learn_client.cpp
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
/* Copyright 2013-present Barefoot Networks, 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.
*/
/*
* Antonin Bas (antonin@barefootnetworks.com)
*
*/
#include <bm/bm_apps/learn.h>
#include <bm/Standard.h>
#include <bm/SimplePre.h>
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
namespace {
bm_apps::LearnListener *listener;
struct sample_t {
char src_addr[6];
uint16_t ingress_port;
} __attribute__((packed));
} // namespace
namespace runtime = bm_runtime::standard;
namespace {
void learn_cb(const bm_apps::LearnListener::MsgInfo &msg_info,
const char *data, void *cookie) {
(void) cookie;
std::cout << "CB with " << msg_info.num_samples << " samples\n";
auto client = listener->get_client();
assert(client);
for (unsigned int i = 0; i < msg_info.num_samples; i++) {
const sample_t *sample = ((const sample_t *) data) + i;
std::cout << "ingress port is " << ntohs(sample->ingress_port)
<< std::endl;
auto add_entry = [client](
const std::string &t_name, const runtime::BmMatchParams &match_params,
const std::string &a_name, const runtime::BmActionData &action_data) {
runtime::BmAddEntryOptions options;
try {
client->bm_mt_add_entry(0, t_name, match_params, a_name, action_data,
options);
} catch (runtime::InvalidTableOperation &ito) {
auto what = runtime::_TableOperationErrorCode_VALUES_TO_NAMES.find(
ito.code)->second;
std::cout << "Invalid table (" << t_name << ") operation ("
<< ito.code << "): " << what << std::endl;
}
};
runtime::BmMatchParam match_param;
match_param.type = runtime::BmMatchParamType::type::EXACT;
runtime::BmMatchParamExact match_param_exact;
match_param_exact.key =
std::string(sample->src_addr, sizeof(sample->src_addr));
match_param.__set_exact(match_param_exact);
runtime::BmMatchParams match_params({match_param});
add_entry("smac", match_params, "_nop", std::vector<std::string>());
std::vector<std::string> action_data =
{std::string(reinterpret_cast<const char *>(&sample->ingress_port), 2)};
add_entry("dmac", match_params, "forward", action_data);
}
client->bm_learning_ack_buffer(0, msg_info.list_id, msg_info.buffer_id);
}
} // namespace
int main() {
listener = new bm_apps::LearnListener("ipc:///tmp/bmv2-0-notifications.ipc");
listener->register_cb(learn_cb, nullptr);
listener->start();
while (true) std::this_thread::sleep_for(std::chrono::seconds(100));
return 0;
}