blob: c77048c96d06d8f0a3746ae37bcbe55f1ecd2ab3 [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
Shad Ansaricb004c52018-05-30 18:07:23 +000095#if 0
96 // Commenting out as this is causing issues with onu activation
97 // with BAL 2.6 (Broadcom CS5248819).
98
Shad Ansarib7b0ced2018-05-11 21:53:32 +000099 // FIXME - Use a default (all zeros) registration id.
100 memset(registration_id.arr, 0, sizeof(registration_id.arr));
101 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, registration_id, registration_id);
Shad Ansaricb004c52018-05-30 18:07:23 +0000102#endif
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000103
104 BCMBAL_CFG_PROP_SET(&sub_term_obj, subscriber_terminal, admin_state, BCMBAL_STATE_UP);
105
106 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(sub_term_obj.hdr))) {
107 std::cout << "ERROR: Failed to enable ONU: " << std::endl;
108 return Status(grpc::StatusCode::INTERNAL, "Failed to enable ONU");
109 }
110
111 return SchedAdd_(intf_id, onu_id, mk_agg_port_id(onu_id));
112
113 //return Status::OK;
114}
115
116#define MAX_CHAR_LENGTH 20
117#define MAX_OMCI_MSG_LENGTH 44
118Status OmciMsgOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
119 bcmbal_u8_list_u32_max_2048 buf; /* A structure with a msg pointer and length value */
120 bcmos_errno err = BCM_ERR_OK;
121
122 /* The destination of the OMCI packet is a registered ONU on the OLT PON interface */
123 bcmbal_dest proxy_pkt_dest;
124
125 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_ITU_OMCI_CHANNEL;
126 proxy_pkt_dest.u.itu_omci_channel.sub_term_id = onu_id;
127 proxy_pkt_dest.u.itu_omci_channel.intf_id = intf_id;
128
129 // ???
130 if ((pkt.size()/2) > MAX_OMCI_MSG_LENGTH) {
131 buf.len = MAX_OMCI_MSG_LENGTH;
132 } else {
133 buf.len = pkt.size()/2;
134 }
135
136 /* Send the OMCI packet using the BAL remote proxy API */
137 uint16_t idx1 = 0;
138 uint16_t idx2 = 0;
139 uint8_t arraySend[buf.len];
140 char str1[MAX_CHAR_LENGTH];
141 char str2[MAX_CHAR_LENGTH];
142 memset(&arraySend, 0, buf.len);
143
144 std::cout << "Sending omci msg to ONU of length is "
145 << buf.len
146 << std::endl;
147
148 for (idx1=0,idx2=0; idx1<((buf.len)*2); idx1++,idx2++) {
149 sprintf(str1,"%c", pkt[idx1]);
150 sprintf(str2,"%c", pkt[++idx1]);
151 strcat(str1,str2);
152 arraySend[idx2] = strtol(str1, NULL, 16);
153 }
154
155 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
156 memcpy(buf.val, (uint8_t *)arraySend, buf.len);
157
158 std::cout << "After converting bytes to hex "
159 << buf.val << buf.len << std::endl;
160
161 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
162
163 std::cout << "OMCI request msg of length " << buf.len
164 << " sent to ONU" << onu_id
165 << " through PON " << intf_id << std::endl;
166
167 free(buf.val);
168
169 return Status::OK;
170}
171
172Status OnuPacketOut_(uint32_t intf_id, uint32_t onu_id, const std::string pkt) {
173 bcmos_errno err = BCM_ERR_OK;
174 bcmbal_dest proxy_pkt_dest;
175 bcmbal_u8_list_u32_max_2048 buf;
176
177 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_SUB_TERM,
178 proxy_pkt_dest.u.sub_term.sub_term_id = onu_id;
179 proxy_pkt_dest.u.sub_term.intf_id = intf_id;
180
181 buf.len = pkt.size();
182 buf.val = (uint8_t *)malloc((buf.len)*sizeof(uint8_t));
183 memcpy(buf.val, (uint8_t *)pkt.data(), buf.len);
184
185 err = bcmbal_pkt_send(0, proxy_pkt_dest, (const char *)(buf.val), buf.len);
186
187 std::cout << "Packet out of length " << buf.len
188 << " sent to ONU" << onu_id
189 << " through PON " << intf_id << std::endl;
190
191 free(buf.val);
192
193 return Status::OK;
194}
195
Nicolas Palpacuerb78def42018-06-07 12:55:26 -0400196Status UplinkPacketOut_(uint32_t intf_id, const std::string pkt) {
197 bcmos_errno err = BCM_ERR_OK;
198 bcmbal_dest proxy_pkt_dest;
199 bcmbal_u8_list_u32_max_2048 buf;
200
201 proxy_pkt_dest.type = BCMBAL_DEST_TYPE_NNI,
202 proxy_pkt_dest.u.nni.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 through uplink port " << intf_id << std::endl;
212
213 free(buf.val);
214
215 return Status::OK;
216}
217
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000218Status FlowAdd_(uint32_t onu_id,
219 uint32_t flow_id, const std::string flow_type,
220 uint32_t access_intf_id, uint32_t network_intf_id,
221 uint32_t gemport_id,
222 const ::openolt::Classifier& classifier,
223 const ::openolt::Action& action) {
224 bcmos_errno err;
225 bcmbal_flow_cfg cfg;
226 bcmbal_flow_key key = { };
227
228 std::cout << "flow add -"
229 << " intf_id:" << access_intf_id
230 << " onu_id:" << onu_id
231 << " flow_id:" << flow_id
232 << " flow_type:" << flow_type
233 << " gemport_id:" << gemport_id
234 << " network_intf_id:" << network_intf_id
235 << std::endl;
236
237 key.flow_id = flow_id;
238 if (flow_type.compare("upstream") == 0 ) {
239 key.flow_type = BCMBAL_FLOW_TYPE_UPSTREAM;
240 } else if (flow_type.compare("downstream") == 0) {
241 key.flow_type = BCMBAL_FLOW_TYPE_DOWNSTREAM;
242 } else {
243 std::cout << "Invalid flow type " << flow_type << std::endl;
244 return Status::CANCELLED;
245 }
246
247 BCMBAL_CFG_INIT(&cfg, flow, key);
248
249 BCMBAL_CFG_PROP_SET(&cfg, flow, admin_state, BCMBAL_STATE_UP);
250 BCMBAL_CFG_PROP_SET(&cfg, flow, access_int_id, access_intf_id);
251 BCMBAL_CFG_PROP_SET(&cfg, flow, network_int_id, network_intf_id);
252 BCMBAL_CFG_PROP_SET(&cfg, flow, sub_term_id, onu_id);
253 BCMBAL_CFG_PROP_SET(&cfg, flow, svc_port_id, gemport_id);
254
255 {
256 bcmbal_classifier val = { };
257
258 if (classifier.o_tpid()) {
259 val.o_tpid = classifier.o_tpid();
260 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_TPID;
261 }
262
263 if (classifier.o_vid()) {
264 val.o_vid = classifier.o_vid();
265 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_VID;
266 }
267
268 if (classifier.i_tpid()) {
269 val.i_tpid = classifier.i_tpid();
270 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_TPID;
271 }
272
273 if (classifier.i_vid()) {
274 val.i_vid = classifier.i_vid();
275 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_VID;
276 }
277
278 if (classifier.o_pbits()) {
279 val.o_pbits = classifier.o_pbits();
280 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_O_PBITS;
281 }
282
283 if (classifier.i_pbits()) {
284 val.i_pbits = classifier.i_pbits();
285 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_I_PBITS;
286 }
287
288 if (classifier.eth_type()) {
289 val.ether_type = classifier.eth_type();
290 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_ETHER_TYPE;
291 }
292
293 /*
294 if (classifier.dst_mac()) {
295 val.dst_mac = classifier.dst_mac();
296 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_MAC;
297 }
298
299 if (classifier.src_mac()) {
300 val.src_mac = classifier.src_mac();
301 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_MAC;
302 }
303 */
304
305 if (classifier.ip_proto()) {
306 val.ip_proto = classifier.ip_proto();
307 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_IP_PROTO;
308 }
309
310 /*
311 if (classifier.dst_ip()) {
312 val.dst_ip = classifier.dst_ip();
313 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_IP;
314 }
315
316 if (classifier.src_ip()) {
317 val.src_ip = classifier.src_ip();
318 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_IP;
319 }
320 */
321
322 if (classifier.src_port()) {
323 val.src_port = classifier.src_port();
324 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_SRC_PORT;
325 }
326
327 if (classifier.dst_port()) {
328 val.dst_port = classifier.dst_port();
329 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_DST_PORT;
330 }
331
332 if (!classifier.pkt_tag_type().empty()) {
333 if (classifier.pkt_tag_type().compare("untagged") == 0) {
334 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_UNTAGGED;
335 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
336 } else if (classifier.pkt_tag_type().compare("single_tag") == 0) {
337 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_SINGLE_TAG;
338 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
339 } else if (classifier.pkt_tag_type().compare("double_tag") == 0) {
340 val.pkt_tag_type = BCMBAL_PKT_TAG_TYPE_DOUBLE_TAG;
341 val.presence_mask = val.presence_mask | BCMBAL_CLASSIFIER_ID_PKT_TAG_TYPE;
342 }
343 }
344
345 BCMBAL_CFG_PROP_SET(&cfg, flow, classifier, val);
346 }
347
348 {
349 bcmbal_action val = { };
350
351 const ::openolt::ActionCmd& cmd = action.cmd();
352
353 if (cmd.add_outer_tag()) {
354 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_ADD_OUTER_TAG;
355 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
356 }
357
358 if (cmd.remove_outer_tag()) {
359 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_REMOVE_OUTER_TAG;
360 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
361 }
362
363 if (cmd.trap_to_host()) {
364 val.cmds_bitmask |= BCMBAL_ACTION_CMD_ID_TRAP_TO_HOST;
365 val.presence_mask |= BCMBAL_ACTION_ID_CMDS_BITMASK;
366 }
367
368 if (action.o_vid()) {
369 val.o_vid = action.o_vid();
370 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_VID;
371 }
372
373 if (action.o_pbits()) {
374 val.o_pbits = action.o_pbits();
375 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_PBITS;
376 }
377
378 if (action.o_tpid()) {
379 val.o_tpid = action.o_tpid();
380 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_O_TPID;
381 }
382
383 if (action.i_vid()) {
384 val.i_vid = action.i_vid();
385 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_VID;
386 }
387
388 if (action.i_pbits()) {
389 val.i_pbits = action.i_pbits();
390 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_PBITS;
391 }
392
393 if (action.i_tpid()) {
394 val.i_tpid = action.i_tpid();
395 val.presence_mask = val.presence_mask | BCMBAL_ACTION_ID_I_TPID;
396 }
397
398 BCMBAL_CFG_PROP_SET(&cfg, flow, action, val);
399 }
400
401 {
402 bcmbal_tm_sched_id val;
403 val = (bcmbal_tm_sched_id) mk_sched_id(onu_id);
404 BCMBAL_CFG_PROP_SET(&cfg, flow, dba_tm_sched_id, val);
405 }
406
407 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
408 std::cout << "ERROR: flow add failed" << std::endl;
409 return Status(grpc::StatusCode::INTERNAL, "flow add failed");
410 }
411
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400412 register_new_flow(key);
413
Shad Ansarib7b0ced2018-05-11 21:53:32 +0000414 return Status::OK;
415}
416
417Status SchedAdd_(int intf_id, int onu_id, int agg_port_id) {
418 bcmbal_tm_sched_cfg cfg;
419 bcmbal_tm_sched_key key = { };
420 bcmbal_tm_sched_type sched_type;
421
422 key.id = mk_sched_id(onu_id);
423 key.dir = BCMBAL_TM_SCHED_DIR_US;
424
425 BCMBAL_CFG_INIT(&cfg, tm_sched, key);
426
427 {
428 bcmbal_tm_sched_owner val = { };
429
430 val.type = BCMBAL_TM_SCHED_OWNER_TYPE_AGG_PORT;
431 val.u.agg_port.intf_id = (bcmbal_intf_id) intf_id;
Nicolas Palpacuer0f19b1a2018-06-07 17:29:31 -0400432 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 +0000433 val.u.agg_port.sub_term_id = (bcmbal_sub_id) onu_id;
434 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 -0400435 val.u.agg_port.agg_port_id = (bcmbal_aggregation_port_id) agg_port_id;
436 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 +0000437
438 BCMBAL_CFG_PROP_SET(&cfg, tm_sched, owner, val);
439 }
440
441 if (bcmbal_cfg_set(DEFAULT_ATERM_ID, &(cfg.hdr))) {
442 std::cout << "ERROR: Failed to create upstream DBA sched"
443 << " id:" << key.id
444 << " intf_id:" << intf_id
445 << " onu_id:" << onu_id << std::endl;
446 return Status(grpc::StatusCode::INTERNAL, "Failed to create upstream DBA sched");
447 //return 1;
448 }
449 std::cout << "create upstream DBA sched"
450 << " id:" << key.id
451 << " intf_id:" << intf_id
452 << " onu_id:" << onu_id << std::endl;
453
454 return Status::OK;
455 //return 0;
456}