bmv2
Designing your own switch target with bmv2
Loading...
Searching...
No Matches
learning.h
Go to the documentation of this file.
1/* Copyright 2013-present Barefoot Networks, Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/*
17 * Antonin Bas (antonin@barefootnetworks.com)
18 *
19 */
20
22
23#ifndef BM_BM_SIM_LEARNING_H_
24#define BM_BM_SIM_LEARNING_H_
25
26#include <string>
27#include <vector>
28#include <memory>
29#include <functional>
30
31#include "device_id.h"
32#include "transport.h"
33#include "phv_forward.h"
34
35namespace bm {
36
37class Packet;
38
39// TODO(antonin): automate learning (i.e. make it target-independent)?
40
45 public:
46 using list_id_t = int;
47 using buffer_id_t = uint64_t;
48
49 enum LearnErrorCode {
50 SUCCESS = 0,
51 INVALID_LIST_ID,
52 INVALID_LIST_NAME,
53 ERROR
54 };
55
56 // the header size for notifications is always 32 bytes
57 struct msg_hdr_t {
58 char sub_topic[4];
59 s_device_id_t switch_id;
60 s_cxt_id_t cxt_id;
61 int list_id;
62 uint64_t buffer_id;
63 unsigned int num_samples;
64 } __attribute__((packed));
65
66 using LearnCb = std::function<void(const msg_hdr_t &, size_t,
67 std::unique_ptr<char[]>, void *)>;
68
69 virtual ~LearnEngineIface() { }
70
71 virtual void list_create(list_id_t list_id, const std::string &list_name,
72 size_t max_samples = 1,
73 unsigned int timeout_ms = 1000) = 0;
74
75 virtual void list_set_learn_writer(
76 list_id_t list_id, std::shared_ptr<TransportIface> learn_writer) = 0;
77
78 virtual void list_set_learn_cb(list_id_t list_id, const LearnCb &learn_cb,
79 void * cookie) = 0;
80
81 virtual void list_push_back_field(list_id_t list_id, header_id_t header_id,
82 int field_offset) = 0;
83
84 virtual void list_push_back_constant(list_id_t list_id,
85 const std::string &hexstring) = 0;
86
87 virtual void list_init(list_id_t list_id) = 0;
88
89 virtual LearnErrorCode list_set_timeout(list_id_t list_id,
90 unsigned int timeout_ms) = 0;
91
92 virtual LearnErrorCode list_set_max_samples(list_id_t list_id,
93 size_t max_samples) = 0;
94
95 virtual LearnErrorCode list_get_name_from_id(
96 list_id_t list_id, std::string *list_name) const = 0;
97
98 virtual LearnErrorCode list_get_id_from_name(
99 const std::string &list_name, list_id_t *list_id) const = 0;
100
108 virtual void learn(list_id_t list_id, const Packet &pkt) = 0;
109
110 virtual LearnErrorCode ack(list_id_t list_id, buffer_id_t buffer_id,
111 int sample_id) = 0;
112 virtual LearnErrorCode ack(list_id_t list_id, buffer_id_t buffer_id,
113 const std::vector<int> &sample_ids) = 0;
114 virtual LearnErrorCode ack_buffer(list_id_t list_id,
115 buffer_id_t buffer_id) = 0;
116
117 virtual void reset_state() = 0;
118
119 static std::unique_ptr<LearnEngineIface> make(device_id_t device_id = 0,
120 cxt_id_t cxt_id = 0);
121};
122
123} // namespace bm
124
125#endif // BM_BM_SIM_LEARNING_H_
Definition learning.h:44
virtual void learn(list_id_t list_id, const Packet &pkt)=0
Definition packet.h:98