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