blob: 867c4581e5108b5b1bf5cd69c25f66438e4fe4ce [file] [log] [blame]
Shad Ansari01b0e652018-04-05 21:02:53 +00001/*
2 Copyright (C) 2018 Open Networking Foundation
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <iostream>
19#include <memory>
20#include <string>
21
22#include "Queue.h"
23#include <iostream>
24#include <sstream>
25
26#include "server.h"
27#include "indications.h"
28
29extern "C"
30{
31#include <bcmos_system.h>
32#include <bal_api.h>
33#include <bal_api_end.h>
34}
35#include <openolt.grpc.pb.h>
36
37using grpc::Server;
38using grpc::ServerBuilder;
39using grpc::ServerContext;
40using grpc::ServerWriter;
41
42const char *serverPort = "0.0.0.0:9191";
43
44class OpenoltService final : public openolt::Openolt::Service {
45
46 Status ActivateOnu(
47 ServerContext* context,
48 const openolt::Onu* request,
49 openolt::Empty* response) override {
50 return ActivateOnu_(
51 request->intf_id(),
52 request->onu_id(),
53 ((request->serial_number()).vendor_id()).c_str(),
54 ((request->serial_number()).vendor_specific()).c_str());
55 }
56
57 Status OmciMsgOut(
58 ServerContext* context,
59 const openolt::OmciMsg* request,
60 openolt::Empty* response) override {
61 return OmciMsgOut_(
62 request->intf_id(),
63 request->onu_id(),
64 request->pkt());
65 }
66
Shad Ansarif2e27a42018-04-26 22:37:38 +000067 Status OnuPacketOut(
68 ServerContext* context,
69 const openolt::OnuPacket* request,
70 openolt::Empty* response) override {
71 return OnuPacketOut_(
72 request->intf_id(),
73 request->onu_id(),
74 request->pkt());
75 }
76
Shad Ansari01b0e652018-04-05 21:02:53 +000077 Status FlowAdd(
78 ServerContext* context,
79 const openolt::Flow* request,
80 openolt::Empty* response) override {
81 return FlowAdd_(
82 request->onu_id(),
83 request->flow_id(),
84 request->flow_type(),
85 request->access_intf_id(),
86 request->network_intf_id(),
87 request->gemport_id(),
88 request->classifier(),
89 request->action());
90 }
91
92 Status EnableIndication(
93 ServerContext* context,
94 const ::openolt::Empty* request,
95 ServerWriter<openolt::Indication>* writer) override {
96 while (1) {
97 auto oltInd = oltIndQ.pop();
98 writer->Write(oltInd);
99 //oltInd.release_olt_ind()
100 }
101 return Status::OK;
102 }
103};
104
105void RunServer() {
106 OpenoltService service;
107 std::string server_address(serverPort);
108 ServerBuilder builder;
109
110 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
111 builder.RegisterService(&service);
112
113 std::unique_ptr<Server> server(builder.BuildAndStart());
114
115 std::cout << "Server listening on " << server_address << std::endl;
116
117 server->Wait();
118}
119
120Status Enable_() {
121 static bool enabled = false;
122 bcmbal_access_terminal_cfg acc_term_obj;
123 bcmbal_access_terminal_key key = { };
124
125 if (!enabled) {
126 std::cout << "Enable OLT" << std::endl;
127 key.access_term_id = DEFAULT_ATERM_ID;
128 BCMBAL_CFG_INIT(&acc_term_obj, access_terminal, key);
129 BCMBAL_CFG_PROP_SET(&acc_term_obj, access_terminal, admin_state, BCMBAL_STATE_UP);
130 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(acc_term_obj.hdr))) {
131 std::cout << "ERROR: Failed to enable OLT" << std::endl;
132 return Status(grpc::StatusCode::INTERNAL, "Failed to enable OLT");
133 }
134 enabled = true;
135 }
136 return Status::OK;
137}
138
139Status EnablePonIf_(uint32_t intf_id) {
140 bcmbal_interface_cfg interface_obj;
141 bcmbal_interface_key interface_key;
142
143 interface_key.intf_id = intf_id;
144 interface_key.intf_type = BCMBAL_INTF_TYPE_PON;
145
146 BCMBAL_CFG_INIT(&interface_obj, interface, interface_key);
147 BCMBAL_CFG_PROP_SET(&interface_obj, interface, admin_state, BCMBAL_STATE_UP);
148
149 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(interface_obj.hdr))) {
150 std::cout << "ERROR: Failed to enable PON interface: " << intf_id << std::endl;
151 return Status(grpc::StatusCode::INTERNAL, "Failed to enable PON interface");
152 }
153
154 return Status::OK;
155}
156
157Status ActivateOnu_(uint32_t intf_id, uint32_t onu_id,
158 const char *vendor_id, const char *vendor_specific) {
159
160 bcmbal_subscriber_terminal_cfg sub_term_obj = {};
161 bcmbal_subscriber_terminal_key subs_terminal_key;
162 bcmbal_serial_number serial_num = {};
163 bcmbal_registration_id registration_id = {};
164
165 std::cout << "Enabling ONU " << onu_id << " on PON " << intf_id << std::endl;
166 std::cout << "Vendor Id " << vendor_id
167 << "Vendor Specific Id " << vendor_specific
168 << std::endl;
169
170 subs_terminal_key.sub_term_id = onu_id;
171 subs_terminal_key.intf_id = intf_id;
172 BCMBAL_CFG_INIT(&sub_term_obj, subscriber_terminal, subs_terminal_key);
173
174 memcpy(serial_num.vendor_id, vendor_id, 4);
175 memcpy(serial_num.vendor_specific, vendor_specific, 4);
176 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, serial_number, serial_num);
177
178 // FIXME - Use a default (all zeros) registration id.
179 memset(registration_id.arr, 0, sizeof(registration_id.arr));
180 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, registration_id, registration_id);
181
182 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, admin_state, BCMBAL_STATE_UP);
183
184 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(sub_term_obj.hdr))) {
185 std::cout << "ERROR: Failed to enable ONU: " << std::endl;
186 return Status(grpc::StatusCode::INTERNAL, "Failed to enable ONU");
187 }
188
189 return SchedAdd_(intf_id, onu_id, mk_agg_port_id(onu_id));
190
191 //return Status::OK;
192}
193
194#define MAX_CHAR_LENGTH 20
195#define MAX_OMCI_MSG_LENGTH 44
196Status OmciMsgOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
197 bcmbal_u8_list_u32_max_2048 buf; /* A structure with a msg pointer and length value */
198 bcmos_errno err = BCM_ERR_OK;
199
200 /* The destination of the OMCI packet is a registered ONU on the OLT PON interface */
201 bcmbal_dest proxy_pkt_dest;
202
203 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_ITU_OMCI_CHANNEL;
204 proxy_pkt_dest.u.itu_omci_channel.sub_term_id = onu_id;
205 proxy_pkt_dest.u.itu_omci_channel.intf_id = intf_id;
206
207 // ???
208 if ((pkt.size()/2) > MAX_OMCI_MSG_LENGTH) {
209 buf.len = MAX_OMCI_MSG_LENGTH;
210 } else {
211 buf.len = pkt.size()/2;
212 }
213
214 /* Send the OMCI packet using the BAL remote proxy API */
215 uint16_t idx1 = 0;
216 uint16_t idx2 = 0;
217 uint8_t arraySend[buf.len];
218 char str1[MAX_CHAR_LENGTH];
219 char str2[MAX_CHAR_LENGTH];
220 memset(&arraySend, 0, buf.len);
221
222 std::cout << "Sending omci msg to ONU of length is "
223 << buf.len
224 << std::endl;
225
226 for (idx1=0,idx2=0; idx1<((buf.len)*2); idx1++,idx2++) {
227 sprintf(str1,"%c", pkt[idx1]);
228 sprintf(str2,"%c", pkt[++idx1]);
229 strcat(str1,str2);
230 arraySend[idx2] = strtol(str1, NULL, 16);
231 }
232
233 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
Shad Ansarif2e27a42018-04-26 22:37:38 +0000234 memcpy(buf.val, (uint8_t *)arraySend, buf.len);
Shad Ansari01b0e652018-04-05 21:02:53 +0000235
236 std::cout << "After converting bytes to hex "
237 << buf.val << buf.len << std::endl;
238
Shad Ansarif2e27a42018-04-26 22:37:38 +0000239 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
Shad Ansari01b0e652018-04-05 21:02:53 +0000240
241 std::cout << "OMCI request msg of length " << buf.len
242 << " sent to ONU" << onu_id
243 << " through PON " << intf_id << std::endl;
244
245 free(buf.val);
246
247 return Status::OK;
248}
249
Shad Ansarif2e27a42018-04-26 22:37:38 +0000250Status OnuPacketOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
251 bcmos_errno err = BCM_ERR_OK;
252 bcmbal_dest proxy_pkt_dest;
253 bcmbal_u8_list_u32_max_2048 buf;
254
255 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_SUB_TERM,
256 proxy_pkt_dest.u.sub_term.sub_term_id = onu_id;
257 proxy_pkt_dest.u.sub_term.intf_id = intf_id;
258
259 buf.len = pkt.size();
260 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
261 memcpy(buf.val, (uint8_t *)pkt.data(), buf.len);
262
263 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
264
265 std::cout << "Packet out of length " << buf.len
266 << " sent to ONU" << onu_id
267 << " through PON " << intf_id << std::endl;
268
269 free(buf.val);
270
271 return Status::OK;
272}
273
Shad Ansari01b0e652018-04-05 21:02:53 +0000274Status FlowAdd_(uint32_t onu_id,
275 uint32_t flow_id, const std::string flow_type,
276 uint32_t access_intf_id, uint32_t network_intf_id,
277 uint32_t gemport_id,
278 const ::openolt::Classifier& classifier,
279 const ::openolt::Action& action) {
280 bcmos_errno err;
281 bcmbal_flow_cfg cfg;
282 bcmbal_flow_key key = { };
283
284 std::cout << "flow add -"
285 << " intf_id:" << access_intf_id
286 << " onu_id:" << onu_id
287 << " flow_id:" << flow_id
288 << " flow_type:" << flow_type
289 << " gemport_id:" << gemport_id
290 << " network_intf_id:" << network_intf_id
291 << std::endl;
292
293 key.flow_id = flow_id;
294 if (flow_type.compare("upstream") == 0 ) {
295 key.flow_type = BCMBAL_FLOW_TYPE_UPSTREAM;
296 } else if (flow_type.compare("downstream") == 0) {
297 key.flow_type = BCMBAL_FLOW_TYPE_DOWNSTREAM;
298 } else {
299 std::cout << "Invalid flow type " << flow_type << std::endl;
300 return Status::CANCELLED;
301 }
302
303 BCMBAL_CFG_INIT(&cfg, flow, key);
304
305 BCMBAL_CFG_PROP_SET(&cfg, flow, admin_state, BCMBAL_STATE_UP);
306 BCMBAL_CFG_PROP_SET(&cfg, flow, access_int_id, access_intf_id);
307 BCMBAL_CFG_PROP_SET(&cfg, flow, network_int_id, network_intf_id);
308 BCMBAL_CFG_PROP_SET(&cfg, flow, sub_term_id, onu_id);
309 BCMBAL_CFG_PROP_SET(&cfg, flow, svc_port_id, gemport_id);
310
311 {
312 bcmbal_classifier val = { };
313
314 if (classifier.o_tpid()) {
315 val.o_tpid = classifier.o_tpid();
316 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_TPID;
317 }
318
319 if (classifier.o_vid()) {
320 val.o_vid = classifier.o_vid();
321 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_VID;
322 }
323
324 if (classifier.i_tpid()) {
325 val.i_tpid = classifier.i_tpid();
326 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_TPID;
327 }
328
329 if (classifier.i_vid()) {
330 val.i_vid = classifier.i_vid();
331 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_VID;
332 }
333
334 if (classifier.o_pbits()) {
335 val.o_pbits = classifier.o_pbits();
336 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_PBITS;
337 }
338
339 if (classifier.i_pbits()) {
340 val.i_pbits = classifier.i_pbits();
341 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_PBITS;
342 }
343
344 if (classifier.eth_type()) {
345 val.ether_type = classifier.eth_type();
346 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_ETHER_TYPE;
347 }
348
349 /*
350 if (classifier.dst_mac()) {
351 val.dst_mac = classifier.dst_mac();
352 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_MAC;
353 }
354
355 if (classifier.src_mac()) {
356 val.src_mac = classifier.src_mac();
357 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_MAC;
358 }
359 */
360
361 if (classifier.ip_proto()) {
362 val.ip_proto = classifier.ip_proto();
363 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_IP_PROTO;
364 }
365
366 /*
367 if (classifier.dst_ip()) {
368 val.dst_ip = classifier.dst_ip();
369 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_IP;
370 }
371
372 if (classifier.src_ip()) {
373 val.src_ip = classifier.src_ip();
374 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_IP;
375 }
376 */
377
378 if (classifier.src_port()) {
379 val.src_port = classifier.src_port();
380 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_PORT;
381 }
382
383 if (classifier.dst_port()) {
384 val.dst_port = classifier.dst_port();
385 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_PORT;
386 }
387
388 if (!classifier.pkt_tag_type().empty()) {
389 if (classifier.pkt_tag_type().compare("untagged") == 0) {
390 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_UNTAGGED;
391 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
392 } else if (classifier.pkt_tag_type().compare("single_tag") == 0) {
393 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_SINGLE_TAG;
394 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
395 } else if (classifier.pkt_tag_type().compare("double_tag") == 0) {
396 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_DOUBLE_TAG;
397 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
398 }
399 }
400
401 BCMBAL_CFG_PROP_SET(&cfg, flow, classifier, val);
402 }
403
404 {
405 bcmbal_action val = { };
406
407 const ::openolt::ActionCmd& cmd = action.cmd();
408
409 if (cmd.add_outer_tag()) {
410 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_ADD_OUTER_TAG;
411 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
412 }
413
414 if (cmd.remove_outer_tag()) {
415 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_REMOVE_OUTER_TAG;
416 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
417 }
418
419 if (cmd.trap_to_host()) {
420 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_TRAP_TO_HOST;
421 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
422 }
423
424 if (action.o_vid()) {
425 val.o_vid = action.o_vid();
426 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_VID;
427 }
428
429 if (action.o_pbits()) {
430 val.o_pbits = action.o_pbits();
431 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_PBITS;
432 }
433
434 if (action.o_tpid()) {
435 val.o_tpid = action.o_tpid();
436 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_TPID;
437 }
438
439 if (action.i_vid()) {
440 val.i_vid = action.i_vid();
441 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_VID;
442 }
443
444 if (action.i_pbits()) {
445 val.i_pbits = action.i_pbits();
446 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_PBITS;
447 }
448
449 if (action.i_tpid()) {
450 val.i_tpid = action.i_tpid();
451 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_TPID;
452 }
453
454 BCMBAL_CFG_PROP_SET(&cfg, flow, action, val);
455 }
456
457 {
458 bcmbal_tm_sched_id val;
459 val = (bcmbal_tm_sched_id) mk_sched_id(onu_id);
460 BCMBAL_CFG_PROP_SET(&cfg, flow, dba_tm_sched_id, val);
461 }
462
463 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
464 std::cout << "ERROR: flow add failed" << std::endl;
465 return Status(grpc::StatusCode::INTERNAL, "flow add failed");
466 }
467
468 return Status::OK;
469}
470
471Status SchedAdd_(int intf_id, int onu_id, int agg_port_id) {
472 bcmbal_tm_sched_cfg cfg;
473 bcmbal_tm_sched_key key = { };
474 bcmbal_tm_sched_type sched_type;
475
476 key.id = mk_sched_id(onu_id);
477 key.dir = BCMBAL_TM_SCHED_DIR_US;
478
479 BCMBAL_CFG_INIT(&cfg, tm_sched, key);
480
481 {
482 bcmbal_tm_sched_owner val = { };
483
484 val.type = BCMBAL_TM_SCHED_OWNER_TYPE_AGG_PORT;
485 val.u.agg_port.intf_id = (bcmbal_intf_id) intf_id;
486 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_INTF_ID;
487 val.u.agg_port.sub_term_id = (bcmbal_sub_id) onu_id;
488 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_SUB_TERM_ID;
489
490 val.u.agg_port.agg_port_id = (bcmbal_aggregation_port_id) agg_port_id;
491 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_AGG_PORT_ID;
492
493 BCMBAL_CFG_PROP_SET(&cfg, tm_sched, owner, val);
494 }
495
496 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
497 std::cout << "ERROR: Failed to create upstream DBA sched"
498 << " id:" << key.id
499 << " intf_id:" << intf_id
500 << " onu_id:" << onu_id << std::endl;
501 return Status(grpc::StatusCode::INTERNAL, "Failed to create upstream DBA sched");
502 //return 1;
503 }
504 std::cout << "create upstream DBA sched"
505 << " id:" << key.id
506 << " intf_id:" << intf_id
507 << " onu_id:" << onu_id << std::endl;
508
509 return Status::OK;
510 //return 0;
511}