blob: b5fe90de5bb9e2b0a500f9dfd68b0ee6bc5a6984 [file] [log] [blame]
Shad Ansarib7b0ced2018-05-11 21:53:32 +00001/*
Nicolas Palpacuerb78def42018-06-07 12:55:26 -04002 Copyright (C) 2018 Open Networking Foundation
Shad Ansarib7b0ced2018-05-11 21:53:32 +00003
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 "core.h"
27#include "indications.h"
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -040028#include "stats_collection.h"
Shad Ansarib7b0ced2018-05-11 21:53:32 +000029
30extern "C"
31{
32#include <bcmos_system.h>
33#include <bal_api.h>
34#include <bal_api_end.h>
35}
36
37Status Enable_() {
38 static bool enabled = false;
39 bcmbal_access_terminal_cfg acc_term_obj;
40 bcmbal_access_terminal_key key = { };
41
42 if (!enabled) {
43 std::cout << "Enable OLT" << std::endl;
44 key.access_term_id = DEFAULT_ATERM_ID;
45 BCMBAL_CFG_INIT(&acc_term_obj, access_terminal, key);
46 BCMBAL_CFG_PROP_SET(&acc_term_obj, access_terminal, admin_state, BCMBAL_STATE_UP);
47 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(acc_term_obj.hdr))) {
48 std::cout << "ERROR: Failed to enable OLT" << std::endl;
49 return Status(grpc::StatusCode::INTERNAL, "Failed to enable OLT");
50 }
51 enabled = true;
52 }
53 return Status::OK;
54}
55
56Status EnablePonIf_(uint32_t intf_id) {
57 bcmbal_interface_cfg interface_obj;
58 bcmbal_interface_key interface_key;
59
60 interface_key.intf_id = intf_id;
61 interface_key.intf_type = BCMBAL_INTF_TYPE_PON;
62
63 BCMBAL_CFG_INIT(&interface_obj, interface, interface_key);
64 BCMBAL_CFG_PROP_SET(&interface_obj, interface, admin_state, BCMBAL_STATE_UP);
65
66 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(interface_obj.hdr))) {
67 std::cout << "ERROR: Failed to enable PON interface: " << intf_id << std::endl;
68 return Status(grpc::StatusCode::INTERNAL, "Failed to enable PON interface");
69 }
70
71 return Status::OK;
72}
73
74Status ActivateOnu_(uint32_t intf_id, uint32_t onu_id,
75 const char *vendor_id, const char *vendor_specific) {
76
77 bcmbal_subscriber_terminal_cfg sub_term_obj = {};
78 bcmbal_subscriber_terminal_key subs_terminal_key;
79 bcmbal_serial_number serial_num = {};
80 bcmbal_registration_id registration_id = {};
81
82 std::cout << "Enabling ONU " << onu_id << " on PON " << intf_id << std::endl;
83 std::cout << "Vendor Id " << vendor_id
84 << "Vendor Specific Id " << vendor_specific
85 << std::endl;
86
87 subs_terminal_key.sub_term_id = onu_id;
88 subs_terminal_key.intf_id = intf_id;
89 BCMBAL_CFG_INIT(&sub_term_obj, subscriber_terminal, subs_terminal_key);
90
91 memcpy(serial_num.vendor_id, vendor_id, 4);
92 memcpy(serial_num.vendor_specific, vendor_specific, 4);
93 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, serial_number, serial_num);
94
95 // FIXME - Use a default (all zeros) registration id.
96 memset(registration_id.arr, 0, sizeof(registration_id.arr));
97 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, registration_id, registration_id);
98
99 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, admin_state, BCMBAL_STATE_UP);
100
101 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(sub_term_obj.hdr))) {
102 std::cout << "ERROR: Failed to enable ONU: " << std::endl;
103 return Status(grpc::StatusCode::INTERNAL, "Failed to enable ONU");
104 }
105
106 return SchedAdd_(intf_id, onu_id, mk_agg_port_id(onu_id));
107
108 //return Status::OK;
109}
110
111#define MAX_CHAR_LENGTH 20
112#define MAX_OMCI_MSG_LENGTH 44
113Status OmciMsgOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
114 bcmbal_u8_list_u32_max_2048 buf; /* A structure with a msg pointer and length value */
115 bcmos_errno err = BCM_ERR_OK;
116
117 /* The destination of the OMCI packet is a registered ONU on the OLT PON interface */
118 bcmbal_dest proxy_pkt_dest;
119
120 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_ITU_OMCI_CHANNEL;
121 proxy_pkt_dest.u.itu_omci_channel.sub_term_id = onu_id;
122 proxy_pkt_dest.u.itu_omci_channel.intf_id = intf_id;
123
124 // ???
125 if ((pkt.size()/2) > MAX_OMCI_MSG_LENGTH) {
126 buf.len = MAX_OMCI_MSG_LENGTH;
127 } else {
128 buf.len = pkt.size()/2;
129 }
130
131 /* Send the OMCI packet using the BAL remote proxy API */
132 uint16_t idx1 = 0;
133 uint16_t idx2 = 0;
134 uint8_t arraySend[buf.len];
135 char str1[MAX_CHAR_LENGTH];
136 char str2[MAX_CHAR_LENGTH];
137 memset(&arraySend, 0, buf.len);
138
139 std::cout << "Sending omci msg to ONU of length is "
140 << buf.len
141 << std::endl;
142
143 for (idx1=0,idx2=0; idx1<((buf.len)*2); idx1++,idx2++) {
144 sprintf(str1,"%c", pkt[idx1]);
145 sprintf(str2,"%c", pkt[++idx1]);
146 strcat(str1,str2);
147 arraySend[idx2] = strtol(str1, NULL, 16);
148 }
149
150 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
151 memcpy(buf.val, (uint8_t *)arraySend, buf.len);
152
153 std::cout << "After converting bytes to hex "
154 << buf.val << buf.len << std::endl;
155
156 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
157
158 std::cout << "OMCI request msg of length " << buf.len
159 << " sent to ONU" << onu_id
160 << " through PON " << intf_id << std::endl;
161
162 free(buf.val);
163
164 return Status::OK;
165}
166
167Status OnuPacketOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
168 bcmos_errno err = BCM_ERR_OK;
169 bcmbal_dest proxy_pkt_dest;
170 bcmbal_u8_list_u32_max_2048 buf;
171
172 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_SUB_TERM,
173 proxy_pkt_dest.u.sub_term.sub_term_id = onu_id;
174 proxy_pkt_dest.u.sub_term.intf_id = intf_id;
175
176 buf.len = pkt.size();
177 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
178 memcpy(buf.val, (uint8_t *)pkt.data(), buf.len);
179
180 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
181
182 std::cout << "Packet out of length " << buf.len
183 << " sent to ONU" << onu_id
184 << " through PON " << intf_id << std::endl;
185
186 free(buf.val);
187
188 return Status::OK;
189}
190
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400191Status UplinkPacketOut_(uint32_t intf_id, const std::string pkt) {
192 bcmos_errno err = BCM_ERR_OK;
193 bcmbal_dest proxy_pkt_dest;
194 bcmbal_u8_list_u32_max_2048 buf;
195
196 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_NNI,
197 proxy_pkt_dest.u.nni.intf_id = intf_id;
198
199 buf.len = pkt.size();
200 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
201 memcpy(buf.val, (uint8_t *)pkt.data(), buf.len);
202
203 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
204
205 std::cout << "Packet out of length " << buf.len
206 << " sent through uplink port " << intf_id << std::endl;
207
208 free(buf.val);
209
210 return Status::OK;
211}
212
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000213Status FlowAdd_(uint32_t onu_id,
214 uint32_t flow_id, const std::string flow_type,
215 uint32_t access_intf_id, uint32_t network_intf_id,
216 uint32_t gemport_id,
217 const ::openolt::Classifier& classifier,
218 const ::openolt::Action& action) {
219 bcmos_errno err;
220 bcmbal_flow_cfg cfg;
221 bcmbal_flow_key key = { };
222
223 std::cout << "flow add -"
224 << " intf_id:" << access_intf_id
225 << " onu_id:" << onu_id
226 << " flow_id:" << flow_id
227 << " flow_type:" << flow_type
228 << " gemport_id:" << gemport_id
229 << " network_intf_id:" << network_intf_id
230 << std::endl;
231
232 key.flow_id = flow_id;
233 if (flow_type.compare("upstream") == 0 ) {
234 key.flow_type = BCMBAL_FLOW_TYPE_UPSTREAM;
235 } else if (flow_type.compare("downstream") == 0) {
236 key.flow_type = BCMBAL_FLOW_TYPE_DOWNSTREAM;
237 } else {
238 std::cout << "Invalid flow type " << flow_type << std::endl;
239 return Status::CANCELLED;
240 }
241
242 BCMBAL_CFG_INIT(&cfg, flow, key);
243
244 BCMBAL_CFG_PROP_SET(&cfg, flow, admin_state, BCMBAL_STATE_UP);
245 BCMBAL_CFG_PROP_SET(&cfg, flow, access_int_id, access_intf_id);
246 BCMBAL_CFG_PROP_SET(&cfg, flow, network_int_id, network_intf_id);
247 BCMBAL_CFG_PROP_SET(&cfg, flow, sub_term_id, onu_id);
248 BCMBAL_CFG_PROP_SET(&cfg, flow, svc_port_id, gemport_id);
249
250 {
251 bcmbal_classifier val = { };
252
253 if (classifier.o_tpid()) {
254 val.o_tpid = classifier.o_tpid();
255 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_TPID;
256 }
257
258 if (classifier.o_vid()) {
259 val.o_vid = classifier.o_vid();
260 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_VID;
261 }
262
263 if (classifier.i_tpid()) {
264 val.i_tpid = classifier.i_tpid();
265 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_TPID;
266 }
267
268 if (classifier.i_vid()) {
269 val.i_vid = classifier.i_vid();
270 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_VID;
271 }
272
273 if (classifier.o_pbits()) {
274 val.o_pbits = classifier.o_pbits();
275 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_PBITS;
276 }
277
278 if (classifier.i_pbits()) {
279 val.i_pbits = classifier.i_pbits();
280 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_PBITS;
281 }
282
283 if (classifier.eth_type()) {
284 val.ether_type = classifier.eth_type();
285 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_ETHER_TYPE;
286 }
287
288 /*
289 if (classifier.dst_mac()) {
290 val.dst_mac = classifier.dst_mac();
291 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_MAC;
292 }
293
294 if (classifier.src_mac()) {
295 val.src_mac = classifier.src_mac();
296 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_MAC;
297 }
298 */
299
300 if (classifier.ip_proto()) {
301 val.ip_proto = classifier.ip_proto();
302 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_IP_PROTO;
303 }
304
305 /*
306 if (classifier.dst_ip()) {
307 val.dst_ip = classifier.dst_ip();
308 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_IP;
309 }
310
311 if (classifier.src_ip()) {
312 val.src_ip = classifier.src_ip();
313 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_IP;
314 }
315 */
316
317 if (classifier.src_port()) {
318 val.src_port = classifier.src_port();
319 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_PORT;
320 }
321
322 if (classifier.dst_port()) {
323 val.dst_port = classifier.dst_port();
324 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_PORT;
325 }
326
327 if (!classifier.pkt_tag_type().empty()) {
328 if (classifier.pkt_tag_type().compare("untagged") == 0) {
329 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_UNTAGGED;
330 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
331 } else if (classifier.pkt_tag_type().compare("single_tag") == 0) {
332 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_SINGLE_TAG;
333 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
334 } else if (classifier.pkt_tag_type().compare("double_tag") == 0) {
335 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_DOUBLE_TAG;
336 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
337 }
338 }
339
340 BCMBAL_CFG_PROP_SET(&cfg, flow, classifier, val);
341 }
342
343 {
344 bcmbal_action val = { };
345
346 const ::openolt::ActionCmd& cmd = action.cmd();
347
348 if (cmd.add_outer_tag()) {
349 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_ADD_OUTER_TAG;
350 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
351 }
352
353 if (cmd.remove_outer_tag()) {
354 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_REMOVE_OUTER_TAG;
355 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
356 }
357
358 if (cmd.trap_to_host()) {
359 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_TRAP_TO_HOST;
360 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
361 }
362
363 if (action.o_vid()) {
364 val.o_vid = action.o_vid();
365 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_VID;
366 }
367
368 if (action.o_pbits()) {
369 val.o_pbits = action.o_pbits();
370 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_PBITS;
371 }
372
373 if (action.o_tpid()) {
374 val.o_tpid = action.o_tpid();
375 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_TPID;
376 }
377
378 if (action.i_vid()) {
379 val.i_vid = action.i_vid();
380 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_VID;
381 }
382
383 if (action.i_pbits()) {
384 val.i_pbits = action.i_pbits();
385 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_PBITS;
386 }
387
388 if (action.i_tpid()) {
389 val.i_tpid = action.i_tpid();
390 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_TPID;
391 }
392
393 BCMBAL_CFG_PROP_SET(&cfg, flow, action, val);
394 }
395
396 {
397 bcmbal_tm_sched_id val;
398 val = (bcmbal_tm_sched_id) mk_sched_id(onu_id);
399 BCMBAL_CFG_PROP_SET(&cfg, flow, dba_tm_sched_id, val);
400 }
401
402 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
403 std::cout << "ERROR: flow add failed" << std::endl;
404 return Status(grpc::StatusCode::INTERNAL, "flow add failed");
405 }
406
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400407 register_new_flow(key);
408
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000409 return Status::OK;
410}
411
412Status SchedAdd_(int intf_id, int onu_id, int agg_port_id) {
413 bcmbal_tm_sched_cfg cfg;
414 bcmbal_tm_sched_key key = { };
415 bcmbal_tm_sched_type sched_type;
416
417 key.id = mk_sched_id(onu_id);
418 key.dir = BCMBAL_TM_SCHED_DIR_US;
419
420 BCMBAL_CFG_INIT(&cfg, tm_sched, key);
421
422 {
423 bcmbal_tm_sched_owner val = { };
424
425 val.type = BCMBAL_TM_SCHED_OWNER_TYPE_AGG_PORT;
426 val.u.agg_port.intf_id = (bcmbal_intf_id) intf_id;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400427 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_INTF_ID;
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000428 val.u.agg_port.sub_term_id = (bcmbal_sub_id) onu_id;
429 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_SUB_TERM_ID;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400430 val.u.agg_port.agg_port_id = (bcmbal_aggregation_port_id) agg_port_id;
431 val.u.agg_port.presence_mask = val.u.agg_port.presence_mask | BCMBAL_TM_SCHED_OWNER_AGG_PORT_ID_AGG_PORT_ID;
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000432
433 BCMBAL_CFG_PROP_SET(&cfg, tm_sched, owner, val);
434 }
435
436 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
437 std::cout << "ERROR: Failed to create upstream DBA sched"
438 << " id:" << key.id
439 << " intf_id:" << intf_id
440 << " onu_id:" << onu_id << std::endl;
441 return Status(grpc::StatusCode::INTERNAL, "Failed to create upstream DBA sched");
442 //return 1;
443 }
444 std::cout << "create upstream DBA sched"
445 << " id:" << key.id
446 << " intf_id:" << intf_id
447 << " onu_id:" << onu_id << std::endl;
448
449 return Status::OK;
450 //return 0;
451}