Commands.h
1 /*
2  * PFPSim: Library for the Programmable Forwarding Plane Simulation Framework
3  *
4  * Copyright (C) 2016 Concordia Univ., Montreal
5  * Samar Abdi
6  * Umair Aftab
7  * Gordon Bailey
8  * Faras Dewal
9  * Shafigh Parsazad
10  * Eric Tremblay
11  *
12  * Copyright (C) 2016 Ericsson
13  * Bochra Boughzala
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
28  * 02110-1301, USA.
29  */
30 
31 #ifndef CORE_CP_COMMANDS_H_
32 #define CORE_CP_COMMANDS_H_
33 
34 #include <string>
35 #include <vector>
36 #include <iostream>
37 #include <iomanip>
38 #include <memory>
39 
40 namespace pfp {
41 namespace cp {
42 
43 typedef std::vector<uint8_t> Bytes;
44 
45 // Command Parameter types
46 
47 
48 class Action {
49  public:
50  explicit Action(std::string name);
51  void add_param(Bytes param);
52  void print();
53  const std::vector<Bytes> & get_params() const;
54  const std::string & get_name() const;
55  private:
56  std::string name;
57  std::vector<Bytes> params;
58 };
59 
60 class MatchKey {
61  public:
62  enum Type {
63  EXACT, LPM, TERNARY
64  };
65 
66  explicit MatchKey(Bytes v);
67 
68  virtual void print(std::ostream & os) const = 0;
69  virtual ~MatchKey() = default;
70 
71  const Bytes & get_data() const;
72  virtual size_t get_prefix_len() const;
73  virtual const Bytes & get_mask() const;
74  virtual Type get_type() const = 0;
75  protected:
76  const Bytes data;
77 };
78 
79 
80 class ExactKey : public MatchKey {
81  public:
82  explicit ExactKey(Bytes v);
83  void print(std::ostream & os) const override;
84  virtual ~ExactKey() = default;
85  Type get_type() const override;
86 };
87 
88 class LpmKey : public MatchKey {
89  public:
90  LpmKey(Bytes data, size_t prefix_len);
91  void print(std::ostream & os) const override;
92  virtual ~LpmKey() = default;
93  size_t get_prefix_len() const override;
94  Type get_type() const override;
95 
96  private:
97  const size_t prefix_len;
98 };
99 
100 class TernaryKey : public MatchKey {
101  public:
102  TernaryKey(Bytes data, Bytes mask);
103  void print(std::ostream & os) const override;
104  virtual ~TernaryKey() = default;
105  const Bytes & get_mask() const override;
106  Type get_type() const override;
107 
108  private:
109  const Bytes mask;
110 };
111 
112 typedef std::unique_ptr<MatchKey> MatchKeyUPtr;
113 typedef std::vector<MatchKeyUPtr> MatchKeyContainer;
114 
115 // Commands
116 
117 class CommandProcessor;
118 class CommandResult;
119 class ResultProcessor;
120 
121 // enable_shared_from_this allows the commands to pass a smart pointer to
122 // themselves to the result that they generate, so that the processor of the
123 // result has information about the command it came from
124 class Command : public std::enable_shared_from_this<Command> {
125  public:
126  virtual void print() = 0;
127  virtual std::shared_ptr<CommandResult> process(CommandProcessor *p) = 0;
128  virtual void set_table_name(std::string s);
129  const std::string & get_table_name() const;
130  virtual ~Command() = default;
131 
132  // Generate a failure response
133  std::shared_ptr<CommandResult> failure_result();
134 
135  protected:
136  std::string table_name;
137 };
138 
139 #define OVERRIDE_PROCESS() \
140  std::shared_ptr<CommandResult> process(CommandProcessor *p) override
141 
142 class InsertCommand : public Command {
143  public:
144  void print() override;
145  OVERRIDE_PROCESS();
146  void set_match_keys(MatchKeyContainer * v);
147  void set_action(Action * a);
148  virtual ~InsertCommand() = default;
149  const MatchKeyContainer & get_keys() const;
150  const Action & get_action() const;
151 
152  // Build a response for a successful insert command. The handle should
153  // be some type of unique identifier for the inserted entry.
154  std::shared_ptr<CommandResult> success_result(size_t handle);
155 
156  private:
157  std::unique_ptr<MatchKeyContainer> keys;
158  std::unique_ptr<Action> action;
159 };
160 
161 class ModifyCommand : public Command {
162  public:
163  void print() override;
164  OVERRIDE_PROCESS();
165  void set_handle(size_t h);
166  size_t get_handle() const;
167  void set_action(Action * a);
168  const Action & get_action() const;
169  virtual ~ModifyCommand() = default;
170 
171  std::shared_ptr<CommandResult> success_result();
172 
173  private:
174  size_t handle;
175  std::unique_ptr<Action> action;
176 };
177 
178 class DeleteCommand : public Command {
179  public:
180  void print() override;
181  OVERRIDE_PROCESS();
182  void set_handle(size_t h);
183  size_t get_handle() const;
184  virtual ~DeleteCommand() = default;
185 
186  std::shared_ptr<CommandResult> success_result();
187 
188  private:
189  size_t handle;
190 };
191 
192 class BootCompleteCommand : public Command {
193  public:
194  void print() override;
195  OVERRIDE_PROCESS();
196  virtual ~BootCompleteCommand() = default;
197 };
198 
199 #undef OVERRIDE_PROCESS
200 
201 // CommandProcessor
202 
203 #define DECLARE_PROCESS(TYPE) \
204  friend class TYPE; \
205  virtual std::shared_ptr<CommandResult> process(TYPE* t) = 0
206 
208  public:
209  std::shared_ptr<CommandResult>
210  accept_command(const std::shared_ptr<Command> & cmd);
211 
212  protected:
213  DECLARE_PROCESS(InsertCommand);
214  DECLARE_PROCESS(ModifyCommand);
215  DECLARE_PROCESS(DeleteCommand);
216  DECLARE_PROCESS(BootCompleteCommand);
217 };
218 #undef DECLARE_PROCESS
219 
220 
221 // Results
222 
223 #define OVERRIDE_PROCESS() \
224  void process(ResultProcessor *p) override
225 
227  public:
228  virtual ~CommandResult() = default;
229  virtual void process(ResultProcessor *p) = 0;
230 
231  explicit CommandResult(std::shared_ptr<Command> cmd);
232 
233  public:
234  const std::shared_ptr<Command> command;
235 };
236 
237 class InsertResult : public CommandResult {
238  public:
239  virtual ~InsertResult() = default;
240  OVERRIDE_PROCESS();
241 
242  InsertResult(std::shared_ptr<Command> cmd, size_t handle);
243 
244  const size_t handle;
245 };
246 
247 class ModifyResult : public CommandResult {
248  public:
249  virtual ~ModifyResult() = default;
250  OVERRIDE_PROCESS();
251 
252  using CommandResult::CommandResult;
253 };
254 
255 class DeleteResult : public CommandResult {
256  public:
257  virtual ~DeleteResult() = default;
258  OVERRIDE_PROCESS();
259 
260  using CommandResult::CommandResult;
261 };
262 
263 class FailedResult : public CommandResult {
264  public:
265  virtual ~FailedResult() = default;
266  OVERRIDE_PROCESS();
267 
268  using CommandResult::CommandResult;
269 
270  const std::string message;
271 };
272 #undef OVERRIDE_PROCESS
273 
274 #define DECLARE_PROCESS(TYPE) \
275  friend class TYPE; \
276  virtual void process(TYPE* t) = 0
277 
279  public:
280  void accept_result(const std::shared_ptr<CommandResult> & res);
281 
282  protected:
283  DECLARE_PROCESS(ModifyResult);
284  DECLARE_PROCESS(InsertResult);
285  DECLARE_PROCESS(DeleteResult);
286  DECLARE_PROCESS(FailedResult);
287 };
288 #undef DECLARE_PROCESS
289 
290 
291 }; // namespace cp
292 }; // namespace pfp
293 
294 std::ostream & operator<< (std::ostream & os, const pfp::cp::Bytes & b);
295 std::ostream & operator<< (std::ostream & os, const pfp::cp::MatchKey & k);
296 
297 #endif // CORE_CP_COMMANDS_H_
Definition: Commands.h:255
Definition: Commands.h:207
Definition: Commands.h:80
Definition: Commands.h:161
Definition: Commands.h:263
Definition: Commands.h:100
Definition: Commands.h:237
Definition: Commands.h:60
Definition: Commands.h:278
Definition: Commands.h:48
Definition: Commands.h:226
Definition: Commands.h:247
Definition: Commands.h:178
Definition: Commands.h:192
Definition: Commands.h:142
Definition: Commands.h:88
Definition: Commands.h:124
PacketBase.h.
Definition: ConfigurationParameters.cpp:36