/
learn_client.cpp
94 lines (70 loc) · 2.61 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
/* 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;
typedef struct {
char src_addr[6];
uint16_t ingress_port;
} __attribute__((packed)) sample_t;
} // 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";
boost::shared_ptr<runtime::StandardClient> 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;
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::BmAddEntryOptions options;
client->bm_mt_add_entry(0, "smac", {match_param},
"_nop", std::vector<std::string>(),
options);
std::vector<std::string> action_data =
{std::string(reinterpret_cast<const char *>(&sample->ingress_port), 2)};
client->bm_mt_add_entry(0, "dmac", {match_param},
"forward", std::move(action_data),
options);
}
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;
}