aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-keystone2/odp_classification.c
blob: c77a37f0f7ada13753368219166363487de82e13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
/* Copyright (c) 2014, Linaro Limited
 * Copyright (c) 2015, Texas Instruments Incorporated
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp/classification.h>
#include <odp/queue.h>
#include <odp/byteorder.h>
#include <odp/pool.h>
#include <odp/packet_io.h>
#include <odp/spinlock.h>

#include <odp/plat/debug.h>
#include <odp/plat/shared_resource.h>

#include <odp_internal.h>
#include <ccan/list/list.h>

#include "pa_lut_entry.h"
#include "cppi_flow.h"
#include "odp_packet_io_internal.h"
#include "odp_cls_internal.h"
#include "odp_pool_internal.h"
#include "odp_queue_internal.h"

#define ODP_CONFIG_COS_ENTRIES		 16
#define ODP_CONFIG_PMR_ENTRIES		128
#define ODP_CONFIG_PMRSET_ENTRIES	 64
#define ODP_CONFIG_PMRS_IN_PMRSET	  5
#define ODP_CONFIG_PMR_LINK_ENTRIES	128

#define ODP_CONFIG_MAX_TERM_SIZE	 16

static const char odp_cos_table_name[] = "odp_cos_entries";
static const char pmr_table_name[] = "pmr_entries";

enum term_layer {
	L2_TERM = 0,
	L3_TERM,
	L4_TERM,
};
struct term_params_s {
	uint8_t lenght;
	enum term_layer layer;
};

static struct term_params_s pmr_term_params[] = {
		[ODP_PMR_ETHTYPE_0] = { .lenght = 2, .layer = L2_TERM},
		[ODP_PMR_VLAN_ID_0] = { .lenght = 2, .layer = L2_TERM},
		[ODP_PMR_DMAC]      = { .lenght = 6, .layer = L2_TERM},
		[ODP_PMR_IPPROTO]   = { .lenght = 1, .layer = L3_TERM},
		[ODP_PMR_SIP_ADDR]  = { .lenght = 4, .layer = L3_TERM},
		[ODP_PMR_DIP_ADDR]  = { .lenght = 4, .layer = L3_TERM},
		[ODP_PMR_SIP6_ADDR] = { .lenght = 16, .layer = L3_TERM},
		[ODP_PMR_DIP6_ADDR] = { .lenght = 16, .layer = L3_TERM},
		[ODP_PMR_IPSEC_SPI] = { .lenght = 4, .layer = L3_TERM},
		[ODP_PMR_UDP_DPORT] = { .lenght = 2, .layer = L4_TERM},
		[ODP_PMR_TCP_DPORT] = { .lenght = 2, .layer = L4_TERM},
};

typedef uint8_t cos_id_t;

typedef struct cos_entry_s {
	odp_queue_t queue;		/* Associated Queue */
	odp_pool_t  pool;		/* Associated Buffer pool */
	size_t headroom;		/* Headroom for this CoS */
	shres_head_t entry_head;
	struct list_head pmr_out;
	struct list_head pmr_in;
	uint8_t pmr_out_cnt;
	uint8_t pmr_in_cnt;
	paLnkHandle_t l2_vlink;
	paLnkHandle_t l3_vlink;
	cppi_flow_entry_t *cppi_flow;
	pa_entry_t *pa_out_entry;
	char name[ODP_COS_NAME_LEN];	/* name */
	union {
		struct {
			uint8_t l2:1;
			uint8_t l3:1;
			uint8_t l4:1;
		};
		uint8_t all;
	} pmr_layers;
	struct {
		uint8_t activated:1;
	} flags;
} cos_entry_t;

typedef struct term_vals_s {
	odp_pmr_term_e term;
	union {
		uint8_t val[16];
		uint32_t val_int32;
		uint64_t val_int64[2];
	};
	uint8_t mask[16];
} term_vals_t;

typedef struct pmr_s {
	int term_num;
	term_vals_t terms[ODP_CONFIG_PMRS_IN_PMRSET];
	shres_head_t entry_head;

	/* CoS links */
	struct list_node src_cos_node;
	struct list_node dst_cos_node;
	struct list_node affected_node;
	cos_entry_t *src_cos, *dst_cos; /* redundant ? */
	pa_entry_t *pa_entry;

	odp_pktio_t pktio;
	enum term_layer layer;
	struct {
		uint8_t activated:1;
		uint8_t default_pmr:1;
	} flags;
} pmr_entry_t;

static shres_table_t *cos_tbl;
static shres_table_t *pmr_tbl;

static paRouteInfo2_t route_lut1_continue ODP_UNUSED = {
		.dest = pa_DEST_CONTINUE_PARSE_LUT1,
		.customType = pa_CUSTOM_TYPE_NONE,
};
static paRouteInfo2_t route_lut2_continue ODP_UNUSED = {
		.dest = pa_DEST_CONTINUE_PARSE_LUT2,
		.customType = pa_CUSTOM_TYPE_NONE,
};
static paRouteInfo2_t route_discard ODP_UNUSED = {
		.dest = pa_DEST_DISCARD,
};

static int cos_activate(cos_entry_t *entry);
static int cos_deactivate(cos_entry_t *entry);
static int pmr_activate(pmr_entry_t *pmr);
static int pmr_deactivate(pmr_entry_t *pmr);
static int cos_tree_activate(cos_entry_t *entry);
static int cos_tree_deactivate(cos_entry_t *entry);
static odp_pktio_t cos_get_pktio(cos_entry_t *entry, int deact);
static odp_pktio_t _cos_get_pktio(cos_entry_t *entry, int deact);

/**
 * Lock for classification tree update. Updates should not be frequent, so
 * global lock should be fine for now.
 *
 * @todo: implement more granular locking */
static odp_spinlock_t classification_lock;
static int tree_deep;
static int act_tree_deep = 0;

static inline void cls_tree_lock(void)
{
	odp_spinlock_lock(&classification_lock);
}

static inline void cls_tree_unlock(void)
{
	odp_spinlock_unlock(&classification_lock);
}

static inline cos_entry_t *_odp_cos_entry(odp_cos_t cos)
{
	return (cos_entry_t *)(void *)cos;
}

static inline odp_cos_t _odp_cos_from_entry(cos_entry_t *entry)
{
	return (odp_cos_t)entry;
}

static inline pmr_entry_t *_odp_pmr_entry(odp_pmr_t pmr)
{
	return (pmr_entry_t *)(void *)pmr;
}

static inline odp_pmr_t _odp_pmr_from_entry(pmr_entry_t *entry)
{
	return (odp_pmr_t)entry;
}

static inline pmr_entry_t *_odp_pmr_set_entry(odp_pmr_set_t pmr_set)
{
	return (pmr_entry_t *)(void *)pmr_set;
}

static inline odp_pmr_set_t _odp_pmr_set_from_entry(pmr_entry_t *entry)
{
	return (odp_pmr_set_t)entry;
}

static inline void cos_add_pmr_in(cos_entry_t *cos, pmr_entry_t *pmr)
{
	list_add(&cos->pmr_in, &pmr->dst_cos_node);
	cos->pmr_in_cnt++;
}

static inline void cos_rm_pmr_in(cos_entry_t *cos, pmr_entry_t *pmr)
{
	list_del_from(&cos->pmr_in, &pmr->dst_cos_node);
	cos->pmr_in_cnt--;
}

static inline void cos_add_pmr_out(cos_entry_t *cos, pmr_entry_t *pmr)
{
	list_add(&cos->pmr_out,  &pmr->src_cos_node);
	cos->pmr_out_cnt++;
}

static inline void cos_rm_pmr_out(cos_entry_t *cos, pmr_entry_t *pmr)
{
	list_del_from(&cos->pmr_out,  &pmr->src_cos_node);
	cos->pmr_out_cnt--;
}

static inline int __odp_pmr_destroy(pmr_entry_t *entry)
{
	return shres_free(pmr_tbl, entry);
}

static void pmr_disconnect_source(pmr_entry_t *entry)
{
	if (entry->src_cos) {
		cos_rm_pmr_out(entry->src_cos, entry);
		entry->src_cos = NULL;
	} else if (entry->pktio != ODP_PKTIO_INVALID) {
		_pktio_rm_pmr_out(entry->pktio, &entry->src_cos_node);
		entry->pktio = ODP_PKTIO_INVALID;
	}
}

static void pmr_disconnect(pmr_entry_t *entry)
{
	if (entry->dst_cos) {
		cos_rm_pmr_in(entry->dst_cos, entry);
		entry->dst_cos = NULL;
	}

	pmr_disconnect_source(entry);
}

static int cos_free_cb(void *data)
{
	pmr_entry_t *pmr, *npmr;
	cos_entry_t *entry = data;

	act_tree_deep = 0;
	if (cos_tree_deactivate(entry) < 0)
		return -1;

	/* remove CoS and disconnect it's PMRs from the tree */
	list_for_each_safe(&entry->pmr_in, pmr, npmr, dst_cos_node) {
		if (pmr->flags.default_pmr) {
			/* delete dummy default PMRs for default CoS */
			if (pmr->pktio != ODP_PKTIO_INVALID)
				_pktio_rm_pmr_default(pmr->pktio);
			else
				__odp_pmr_destroy(pmr);
		} else {
			pmr_disconnect(pmr);
		}
	}

	list_for_each_safe(&entry->pmr_out, pmr, npmr, src_cos_node)
		pmr_disconnect(pmr);

	if (entry->pool != ODP_POOL_INVALID)
		_odp_pool_put(entry->pool);

	if (entry->queue != ODP_QUEUE_INVALID)
		_queue_put(entry->queue);

	ODP_DBG("CoS \"%s\" destroyed\n", entry->name);
	return 0;
}

/*
 * For now we suppose that PMR is only one entry and
 * cannot be reused in several trees
 */
static int pmr_free_cb(void *data)
{
	int ret = 0;
	pmr_entry_t *pmr_in;
	pmr_entry_t *entry = data;
	int cos_deactivate = 1;

	/* deactivate */
	if (entry->flags.activated) {
		list_for_each(&entry->dst_cos->pmr_in, pmr_in, dst_cos_node) {
			if (entry != pmr_in && pmr_in->flags.activated) {
				cos_deactivate = 0;
				break;
			}
		}

		if (cos_deactivate) {
			/* here PMR also will be deactivated */
			act_tree_deep = 0;
			ret = cos_tree_deactivate(entry->dst_cos);
		} else {
			ret = pmr_deactivate(entry);
		}
	}

	if (ODP_DEBUG_PRINT == 1 && !ret) {
		if ((entry->src_cos || entry->pktio != ODP_PKTIO_INVALID) &&
		    entry->dst_cos)
			ODP_DBG("\"%s\" - PMR - \"%s\" destroyed\n",
				entry->pktio ? _pktio_name(entry->pktio) :
				entry->src_cos->name, entry->dst_cos->name);
		else
			ODP_DBG("PMR destroyed\n");
	}

	/* disconnect from the tree */
	entry->flags.default_pmr = 0;
	pmr_disconnect(entry);

	return ret;
}

int odp_classification_init_global(void)
{
	cos_tbl = shres_table_create(odp_cos_table_name,
				     cos_entry_t,
				     ODP_CONFIG_COS_ENTRIES,
				     cos_free_cb);
	if (cos_tbl == SHRES_TABLE_INVALID)
		return -1;

	pmr_tbl = shres_table_create(pmr_table_name, pmr_entry_t,
				     ODP_CONFIG_PMR_ENTRIES +
				     ODP_CONFIG_PMRSET_ENTRIES, pmr_free_cb);
	if (pmr_tbl == SHRES_TABLE_INVALID)
		return -1;

	odp_spinlock_init(&classification_lock);

	return 0;
}

int odp_classification_term_global(void)
{
	int num;
	int ret = 0;

	num = shres_table_destroy(odp_cos_table_name);
	if (num < 0)
		ret = -1;

	num = shres_table_destroy(pmr_table_name);
	if (num < 0)
		ret = -1;

	return ret;
}

unsigned long long odp_pmr_terms_cap(void)
{
	unsigned long long term_cap = 0;

	term_cap |= (1 << ODP_PMR_ETHTYPE_0);
	term_cap |= (1 << ODP_PMR_VLAN_ID_0);
	term_cap |= (1 << ODP_PMR_DMAC);
	term_cap |= (1 << ODP_PMR_IPPROTO);
	term_cap |= (1 << ODP_PMR_UDP_DPORT);
	term_cap |= (1 << ODP_PMR_TCP_DPORT);
	term_cap |= (1 << ODP_PMR_SIP_ADDR);
	term_cap |= (1 << ODP_PMR_DIP_ADDR);
	term_cap |= (1 << ODP_PMR_SIP6_ADDR);
	term_cap |= (1 << ODP_PMR_DIP6_ADDR);
	term_cap |= (1 << ODP_PMR_IPSEC_SPI);
	return term_cap;
}

unsigned odp_pmr_terms_avail(void)
{
	uint32_t entries =  shres_table_num_entries(pmr_tbl);
	uint32_t entries_alloc =  shres_table_num_entries_allocated(pmr_tbl);
	ODP_AS_STR(entries >= entries_alloc, "entries >= entries_alloc");
	return entries - entries_alloc;
}

int _pmr_hard_update(odp_pmr_t pmr)
{
	pmr_entry_t *entry = _odp_pmr_entry(pmr);

	if (pmr_deactivate(entry) < 0)
		return -1;
	return pmr_activate(entry);
}

static inline int __odp_cos_set_pool(odp_cos_t cos, odp_pool_t pool)
{
	if (cos == ODP_COS_INVALID || pool == ODP_POOL_INVALID) {
		cls_tree_unlock();
		ODP_ERR("Invalid input parameters\n");
		return -1;
	}

	cos_entry_t *entry = _odp_cos_entry(cos);

	if (entry->flags.activated) {
		cls_tree_unlock();
		ODP_ERR("Modifying attached CoS is not supported yet\n");
		return -1;
	}

	if (_odp_pool_get(pool) < 0) {
		cls_tree_unlock();
		ODP_ERR("Pool is not allocated\n");
		return -1;
	}

	entry->pool = pool;
	return 0;
}

int odp_cls_cos_pool_set(odp_cos_t cos, odp_pool_t pool)
{
	int ret;

	cls_tree_lock();
	ret = __odp_cos_set_pool(cos, pool);
	cls_tree_unlock();

	return ret;
}

odp_pool_t odp_cls_cos_pool(odp_cos_t cos)
{
	odp_pool_t pool;
	cos_entry_t *entry = _odp_cos_entry(cos);

	entry = _odp_cos_entry(cos);

	shres_lock(entry);
	pool = entry->pool;
	shres_unlock(entry);

	return pool;
}

static inline int __odp_cos_queue_set(odp_cos_t cos, odp_queue_t queue)
{
	if (cos == ODP_COS_INVALID || queue == ODP_QUEUE_INVALID) {
		cls_tree_unlock();
		ODP_ERR("Invalid input parameters\n");
		return -1;
	}

	cos_entry_t *entry = _odp_cos_entry(cos);

	if (entry->flags.activated) {
		cls_tree_unlock();
		ODP_ERR("Modifying active CoS is not supported yet\n");
		return -1;
	}

	if (_queue_get(queue) < 0) {
		cls_tree_unlock();
		ODP_ERR("Queue is not allocated\n");
		return -1;
	}

	entry->queue = queue;
	return 0;
}

int odp_cos_queue_set(odp_cos_t cos, odp_queue_t queue)
{
	int ret;

	cls_tree_lock();
	ret = __odp_cos_queue_set(cos, queue);
	cls_tree_unlock();

	return ret;
}

odp_queue_t odp_cos_queue(odp_cos_t cos)
{
	odp_queue_t queue;
	cos_entry_t *entry = _odp_cos_entry(cos);

	entry = _odp_cos_entry(cos);

	shres_lock(entry);
	queue = entry->queue;
	shres_unlock(entry);

	return queue;
}

void odp_cls_cos_param_init(odp_cls_cos_param_t *param)
{
	param->queue = ODP_QUEUE_INVALID;
	param->pool = ODP_POOL_INVALID;
	param->drop_policy = ODP_COS_DROP_NEVER;
}

odp_cos_t odp_cls_cos_create(const char *name,
			     odp_cls_cos_param_t *param)
{
	odp_cos_t cos;
	cos_entry_t *entry;

	entry = shres_alloc(cos_tbl, typeof(*entry));
	if (!entry) {
		ODP_ERR("Couldn't allocate CoS\n");
		return ODP_COS_INVALID;
	}

	strncpy(entry->name, name, ODP_COS_NAME_LEN - 1);
	entry->name[ODP_COS_NAME_LEN - 1] = 0;
	entry->queue = ODP_QUEUE_INVALID;
	entry->pool = ODP_POOL_INVALID;
	entry->cppi_flow = NULL;
	entry->headroom = ODP_CONFIG_PACKET_HEADROOM;
	list_head_init(&entry->pmr_in);
	list_head_init(&entry->pmr_out);
	entry->pmr_in_cnt = 0;
	entry->pmr_out_cnt = 0;
	entry->l2_vlink = NULL;
	entry->l3_vlink = NULL;
	entry->pmr_layers.all = 0;
	entry->flags.activated = 0;
	ODP_DBG("CoS \"%s\" created\n", entry->name);

	cos = _odp_cos_from_entry(entry);
	if (param->pool != ODP_POOL_INVALID &&
	    __odp_cos_set_pool(cos, param->pool))
		goto err;
	if (param->queue != ODP_QUEUE_INVALID &&
	    __odp_cos_queue_set(cos, param->queue))
		goto err;

	if (odp_cos_drop_set(cos, param->drop_policy))
		goto err;

	return _odp_cos_from_entry(entry);

err:
	shres_free(cos_tbl, _odp_cos_entry(cos));
	return ODP_COS_INVALID;
}

int odp_cos_destroy(odp_cos_t cos)
{
	int ret;

	cls_tree_lock();
	if (cos == ODP_COS_INVALID) {
		cls_tree_unlock();
		ODP_ERR("Invalid input parameters\n");
		return -1;
	}

	ret = shres_free(cos_tbl, _odp_cos_entry(cos));
	cls_tree_unlock();
	return ret;
}

int odp_cos_drop_set(odp_cos_t cos_id ODP_UNUSED, odp_cls_drop_t drop_policy)
{
	if (drop_policy == ODP_COS_DROP_NEVER)
		return 0;

	if (drop_policy == ODP_COS_DROP_POOL)
		return 0;

	ODP_UNIMPLEMENTED();
	return -1;
}

int odp_cos_with_l2_priority(odp_pktio_t pktio_in ODP_UNUSED,
			     uint8_t num_qos ODP_UNUSED,
			     uint8_t qos_table[] ODP_UNUSED,
			     odp_cos_t cos_table[] ODP_UNUSED)
{
	ODP_UNIMPLEMENTED();
	return -1;
}

int odp_cos_with_l3_qos(odp_pktio_t pktio_in ODP_UNUSED,
			uint32_t num_qos ODP_UNUSED,
			uint8_t qos_table[] ODP_UNUSED,
			odp_cos_t cos_table[] ODP_UNUSED,
			odp_bool_t l3_preference ODP_UNUSED)
{
	ODP_UNIMPLEMENTED();
	return -1;
}

static int pmr_term_validate(odp_pmr_term_e term, uint32_t val_sz)
{
	unsigned long long cap = odp_pmr_terms_cap();

	if (!(cap & (1 << term))) {
		ODP_ERR("Unsupported term: %d\n", term);
		return -1;
	}

	if (pmr_term_params[term].lenght != val_sz) {
		ODP_ERR("Wrong term size: %u\n", val_sz);
		return -1;
	}

	return 0;
}

static int pmr_check_layer_overlap(pmr_entry_t *entry)
{
	uint32_t pmr_word = 0;
	odp_pmr_term_e term;
	pmr_entry_t *pmr;

	/* read PMR internal terms */
	for (int i = 0; i < entry->term_num; i++)
		pmr_word |= (1 << entry->terms[i].term);

	/* check source PMR overlap */
	if (entry->src_cos &&
	    entry->src_cos->pmr_layers.all & (1 << entry->layer)) {
		if (entry->src_cos->pmr_in_cnt > 1) {
			ODP_ERR("Cannot cascade from CoS with multiple input PMRs one of them the same layer, CoS = %s\n",
				entry->src_cos->name);
			return -1;
		}

		/* check if not overlap with src PMR */
		pmr = list_top(&entry->src_cos->pmr_in,
			       typeof(*pmr), dst_cos_node);
		for (int i = 0; i < pmr->term_num; i++) {
			term = pmr->terms[i].term;
			if (pmr_word & (1 << term)) {
				ODP_ERR("Cannot cascade from CoS with the same input PMR, CoS = %s\n",
					entry->src_cos->name);
				return -1;
			}

			pmr_word |= (1 << term);
		}
	}

	/* check potential destination PMRs overlap */
	int dst_host_has_out_layer = 0;
	list_for_each(&entry->dst_cos->pmr_out, pmr, src_cos_node) {
		if (pmr->layer != entry->layer)
			continue;

		dst_host_has_out_layer = 1;
		for (int i = 0; i < pmr->term_num; i++) {
			if (!(pmr_word & (1 << pmr->terms[i].term)))
				continue;

			ODP_ERR("Cannot cascade to CoS with the same output PMRs, CoS = %s\n",
				entry->dst_cos->name);
			return -1;
		}
	}

	if (dst_host_has_out_layer && entry->dst_cos->pmr_in_cnt > 1) {
		ODP_ERR("Cannot cascade to CoS with the same layer output PMRs and multiple inputs, CoS = %s\n",
			entry->dst_cos->name);
		return -1;
	}

	return 0;
}

static int pmr_l2_connection_validate(pmr_entry_t *entry)
{
	if (entry->src_cos) {
		if (entry->src_cos->pmr_in_cnt > 1) {
			ODP_ERR("Can't create L2 PMR from source CoS with multiple input PMRs yet\n");
			return -1;
		}

		if (entry->src_cos->pmr_layers.l3 ||
		    entry->src_cos->pmr_layers.l4) {
			ODP_ERR("L2 PMR can be cascaded to L2 PMR only, CoS = %s\n",
				entry->src_cos);
			return -1;
		}
	} else if (entry->pktio != ODP_PKTIO_INVALID) {
		if (_pktio_pmr_out_cnt(entry->pktio) > 1 &&
		    entry->flags.default_pmr) {
			ODP_ERR("default CoS has to be connected first!\n");
			return -1;
		}
	} else {
		ODP_ERR("PMR w/o source CoS cannot be activated\n");
		return -1;
	}


	if (!entry->flags.default_pmr) {
		odp_pktio_t pktio = cos_get_pktio(entry->dst_cos, 0);

		if (pktio != ODP_PKTIO_INVALID &&
		    !odp_pktio_promisc_mode(pktio)) {
			for (int i = 0; i < entry->term_num; i++) {
				if (entry->terms[i].term != ODP_PMR_DMAC)
					continue;
				ODP_ERR("You cannot add DMAC PMR in not promiscuous mode\n");
				return -1;
			}
		}
	}

	if (pmr_check_layer_overlap(entry) < 0)
		return -1;

	return 0;
}

static int pmr_l3_connection_validate(pmr_entry_t *entry)
{
	if (pmr_check_layer_overlap(entry) < 0)
		return -1;

	if (entry->src_cos && entry->src_cos->pmr_layers.l4) {
			ODP_ERR("L3 PMR can't be cascaded from L4 PMR, CoS = %s\n",
				entry->src_cos->name);
			return -1;
	}

	if (entry->pktio != ODP_PKTIO_INVALID) {
		if (_pktio_pmr_default(entry->pktio) == ODP_PMR_INVAL) {
			/*
			 * TODO: in case of inq_default create dummy default
			 * CoS and use it as the source for L3.
			 */
			ODP_ERR("L3 PMR can't be cascaded from pktio w/o default CoS\n");
			return -1;
		}
	}

	return 0;
}

static int pmr_l4_connection_validate(pmr_entry_t *entry)
{
	if (entry->term_num > 1) {
		ODP_ERR("You cannot create PMRset with UDP and TCP\n");
		return -1;
	}

	if (entry->src_cos && entry->src_cos->pmr_layers.l4) {
		ODP_ERR("L4 PMR can't be cascaded from CoS with L4 PMR, CoS = %s\n",
			entry->src_cos->name);
		return -1;
	}

	return 0;
}

static int pmr_connection_validate(pmr_entry_t *entry)
{
	int ret;

	switch (entry->layer) {
	case L2_TERM:
		ret = pmr_l2_connection_validate(entry);
		break;
	case L3_TERM:
		ret = pmr_l3_connection_validate(entry);
		break;
	case L4_TERM:
		ret = pmr_l4_connection_validate(entry);
		break;
	default:
		ODP_ABORT("Unexpected term layer\n");
		return -1;
	}

	return ret;
}

/**
 * cos_get_pktio - get source pktio.
 * @entry - entry for looking from
 * @deact - equal 1 if deactivation patch, 0 if activation
 *
 * returns src pktio or ODP_PKTIO_INVALID if pktio is not connected or ambiguous
 */
static odp_pktio_t cos_get_pktio(cos_entry_t *entry, int deact)
{
	tree_deep = 0;
	return _cos_get_pktio(entry, deact);
}

static odp_pktio_t _cos_get_pktio(cos_entry_t *entry, int deact)
{
	pmr_entry_t *pmr_entry;
	odp_pktio_t local_pktio = ODP_PKTIO_INVALID;
	odp_pktio_t other_pktio = ODP_PKTIO_INVALID;

	tree_deep++;
	list_for_each(&entry->pmr_in, pmr_entry, dst_cos_node) {
		if (pmr_entry->src_cos == NULL &&
		    pmr_entry->pktio != ODP_PKTIO_INVALID) {
			if (local_pktio == ODP_PKTIO_INVALID)
				local_pktio = pmr_entry->pktio;
			else if (local_pktio != pmr_entry->pktio)
				goto err;
		} else if (pmr_entry->src_cos->flags.activated || deact) {
			ODP_AS_STR(tree_deep < ODP_CONFIG_COS_ENTRIES,
				   "CoS number overhead");

			other_pktio =
				_cos_get_pktio(pmr_entry->src_cos, deact);
			if (other_pktio == ODP_PKTIO_INVALID)
				goto err;

			if (local_pktio == ODP_PKTIO_INVALID)
				local_pktio = other_pktio;
			else if (other_pktio != local_pktio)
				goto err;
		}
	}

	tree_deep--;
	return local_pktio;
err:
	tree_deep--;
	return ODP_PKTIO_INVALID;
}

static int _pmr_dmac(pmr_entry_t *entry, struct pa_entry_params_s *init_params,
		     term_vals_t *term_val)
{
	odp_pktio_t pktio = cos_get_pktio(entry->dst_cos, 0);
	int promisc_mode = odp_pktio_promisc_mode(pktio);

	if ((!promisc_mode && entry->flags.default_pmr) ||
	    (promisc_mode && !entry->flags.default_pmr)) {
		memcpy(&init_params->l2.eth_info.dst,
		       term_val->val, pa_MAC_ADDR_SIZE);
		init_params->l2.eth_info.validBitMap |= pa_ETH_INFO_VALID_DST;
	} else if (!entry->flags.default_pmr && !promisc_mode) {
		return -1;
	}

	return 0;
}

/*
 * It's an activation function for L2 PMR and it's supposed that
 * all links are connected above.
 */
static int pmr_l2_activate(pmr_entry_t *entry, odp_queue_t queue)
{
	paRouteInfo2_t route_host = {
		.validBitMap = 0,
		.dest = pa_DEST_HOST,
		.flowId = cppi_flow_id(entry->dst_cos->cppi_flow),
		.queue = _odp_queue_to_qmss_queue(queue),
		.swInfo0 = _odp_queue_sched_tag(queue),
		.mRouteIndex = pa_NO_MULTI_ROUTE,
	};

	struct pa_entry_params_s init_params = {
		.type = PA_LUT_TYPE_L2,
		.l2 = {
			.params = {
				.validBitMap = pa_PARAM_VALID_NEXTLINK,
				.nextLink = entry->dst_cos->l2_vlink,
				.routeInfo = &route_lut1_continue,
				.nextRtFail = &route_host,
			},
		},
	};

	ODP_AS_STR(entry->src_cos == NULL,
		   "Cascading of L2 PMRs is not implemented yet");

	paEthInfo2_t *eth_info = &init_params.l2.eth_info;
	eth_info->inport = _odp_pktio_port_id(entry->pktio);
	eth_info->validBitMap |= pa_ETH_INFO_VALID_INPORT;

	/* Copy initial parameters from previous L2 PMR */
	if (entry->src_cos && entry->src_cos->pmr_layers.l2) {
		pmr_entry_t *pmr_in = list_top(&entry->src_cos->pmr_in,
					       pmr_entry_t, dst_cos_node);
		memcpy(eth_info,
		       &pa_entry_params(pmr_in->pa_entry)->l2.eth_info,
		       sizeof(*eth_info));
	}

	/* cycle in case of PMR match set */
	for (int i = 0; i < entry->term_num; i++) {
		term_vals_t *term_val = &entry->terms[i];
		switch (term_val->term) {
		case ODP_PMR_DMAC:
			if (_pmr_dmac(entry, &init_params, term_val) < 0)
				ODP_ABORT("You cannot add DMAC PMR in not promiscuous mode\n");
			break;
		case ODP_PMR_ETHTYPE_0:
			memcpy(&eth_info->ethertype, term_val->val,
			       pmr_term_params[term_val->term].lenght);
			eth_info->validBitMap |= pa_ETH_INFO_VALID_VLAN;
			break;
		case ODP_PMR_VLAN_ID_0:
			memcpy(&eth_info->vlan, term_val->val,
			       pmr_term_params[term_val->term].lenght);
			eth_info->validBitMap |= pa_ETH_INFO_VALID_VLAN;
			break;
		default:
			ODP_ABORT("Unknown L2 PMR term\n");
			break;
		}
	}

	entry->pa_entry = pa_create_entry(&init_params);
	if (!entry->pa_entry) {
		ODP_ERR("failed to create MAC entry\n");
		return -1;
	}

	return 0;
}

static int pmr_l3_activate(pmr_entry_t *entry, odp_queue_t queue)
{
	cos_entry_t *src_cos_entry;
	paRouteInfo2_t route_host = {
		.dest = pa_DEST_HOST,
		.swInfo0 = _odp_queue_sched_tag(queue),
		.flowId = cppi_flow_id(entry->dst_cos->cppi_flow),
		.queue = _odp_queue_to_qmss_queue(queue),
	};

	src_cos_entry = entry->src_cos != NULL ? entry->src_cos :
		_odp_pmr_entry(_pktio_pmr_default(entry->pktio))->dst_cos;

	struct pa_entry_params_s l3_init_params = {
		.type = PA_LUT_TYPE_L3,
		.l3 = {
			.params = {
				.validBitMap = pa_PARAM_VALID_PREVLINK |
				pa_PARAM_VALID_NEXTLINK,
				.routeInfo = &route_lut2_continue,
				.nextRtFail = &route_host,
				.prevLink = src_cos_entry->l2_vlink,
				.nextLink = entry->dst_cos->l3_vlink,
			},
		},
	};

	/* Copy initial parameters from previous L3 PMR */
	paIpInfo2_t *ip_info = &l3_init_params.l3.ip_info;
	if (entry->src_cos && entry->src_cos->pmr_layers.l3) {
		pmr_entry_t *pmr_in = list_top(&entry->src_cos->pmr_in,
					       pmr_entry_t, dst_cos_node);

		memcpy(ip_info, &pa_entry_params(pmr_in->pa_entry)->l3.ip_info,
		       sizeof(*ip_info));
	}

	for (int i = 0; i < entry->term_num; i++) {
		odp_pmr_term_e term = entry->terms[i].term;
		struct term_params_s term_params = pmr_term_params[term];

		switch (term) {
		case ODP_PMR_SIP_ADDR:
		{
			uint32_t ip_addr;
			ip_addr = odp_cpu_to_be_32(entry->terms[i].val_int32);
			ip_info->ipType = pa_IPV4;
			memcpy(&ip_info->src.ipv4,
			       (char *)&ip_addr, term_params.lenght);
			ip_info->validBitMap |= pa_IP_INFO_VALID_SRC;
			break;
		}
		case ODP_PMR_DIP_ADDR:
		{
			uint32_t ip_addr;
			ip_addr = odp_cpu_to_be_32(entry->terms[i].val_int32);
			ip_info->ipType = pa_IPV4;
			memcpy(&ip_info->dst.ipv4,
			       (char *)&ip_addr, term_params.lenght);
			ip_info->validBitMap |= pa_IP_INFO_VALID_DST;
			break;
		}
		case ODP_PMR_SIP6_ADDR:
			ip_info->ipType = pa_IPV6;
			memcpy(&ip_info->src.ipv6,
			       entry->terms[i].val, term_params.lenght);
			ip_info->validBitMap |= pa_IP_INFO_VALID_SRC;
			break;
		case ODP_PMR_DIP6_ADDR:
			ip_info->ipType = pa_IPV6;
			memcpy(&ip_info->dst.ipv6,
			       entry->terms[i].val, term_params.lenght);
			ip_info->validBitMap |= pa_IP_INFO_VALID_DST;
			break;
		case ODP_PMR_IPPROTO:
			memcpy(&ip_info->proto,
			       entry->terms[i].val, term_params.lenght);
			ip_info->validBitMap |= pa_IP_INFO_VALID_PROTO;
			break;
		case ODP_PMR_IPSEC_SPI:
			memcpy(&ip_info->spi,
			       entry->terms[i].val, term_params.lenght);
			ip_info->validBitMap |= pa_IP_INFO_VALID_SPI;
			break;
		default:
			ODP_ABORT("Unexpected term\n");
		}
	}

	entry->pa_entry = pa_create_entry(&l3_init_params);
	if (!entry->pa_entry) {
		ODP_ERR("failed to create L3 entry\n");
		return -1;
	}

	return 0;
}

static int pmr_l4_activate(pmr_entry_t *entry, odp_queue_t queue)
{
	cos_entry_t *src_cos_entry;
	odp_pmr_term_e term = entry->terms[0].term;

	paRouteInfo2_t route_host = {
		.dest = pa_DEST_HOST,
		.swInfo0 = _odp_queue_sched_tag(queue),
		.flowId = cppi_flow_id(entry->dst_cos->cppi_flow),
		.queue = _odp_queue_to_qmss_queue(queue),
	};

	src_cos_entry = entry->src_cos != NULL ? entry->src_cos :
		_odp_pmr_entry(_pktio_pmr_default(entry->pktio))->dst_cos;

	struct pa_entry_params_s init_params = {
		.type = PA_LUT_TYPE_L4,
		.l4 = {
			.route = &route_host,
			.prev_link = src_cos_entry->l3_vlink,
		},
	};

	switch (term) {
	case ODP_PMR_UDP_DPORT:
	case ODP_PMR_TCP_DPORT:
		memcpy(&init_params.l4.dest_port, entry->terms[0].val,
		       pmr_term_params[term].lenght);
		break;
	default:
		ODP_ABORT("Unexpected term\n");
	}

	entry->pa_entry = pa_create_entry(&init_params);
	if (!entry->pa_entry) {
		ODP_ERR("failed to create L4 entry\n");
		return -1;
	}
	return 0;
}

odp_pmr_t odp_pmr_create(const odp_pmr_match_t *match)
{
	pmr_entry_t *entry;
	odp_pmr_term_e term = match->term;
	uint32_t val_sz = match->val_sz;

	const uint8_t *mask_u8 = match->mask;
	if (pmr_term_validate(term, val_sz))
		return ODP_PMR_INVAL;
	for (unsigned int i = 0; i < val_sz; i++) {
		/* Wildcarding is not supported by K2KH PA */
		if (mask_u8[i] != 0xFF)
			return ODP_PMR_INVAL;
	}

	entry = shres_alloc(pmr_tbl, typeof(*entry));
	if (!entry)
		return ODP_PMR_INVAL;

	entry->terms[0].term = term;
	memcpy(entry->terms[0].val, match->val, val_sz);
	memset(entry->terms[0].mask, 0xFF, val_sz);
	entry->term_num = 1;
	entry->layer = pmr_term_params[term].layer;
	entry->flags.activated = 0;
	entry->flags.default_pmr = 0;
	entry->pa_entry = NULL;
	entry->src_cos = NULL;
	entry->dst_cos = NULL;
	entry->pktio = ODP_PKTIO_INVALID;
	ODP_DBG("PMR created\n");
	return _odp_pmr_from_entry(entry);
}

int __odp_pmr_destroy_nolock(odp_pmr_t pmr)
{
	if (pmr == ODP_PMR_INVAL) {
		ODP_ERR("Invalid input parameters\n");
		return -1;
	}

	return __odp_pmr_destroy(_odp_pmr_entry(pmr));
}

int odp_pmr_destroy(odp_pmr_t pmr)
{
	int ret;

	cls_tree_lock();
	ret = __odp_pmr_destroy_nolock(pmr);
	cls_tree_unlock();
	return ret;
}

int odp_pmr_match_set_create(int num_terms, const odp_pmr_match_t *terms,
			     odp_pmr_set_t *pmr_set_id)
{
	uint32_t pmr_word;
	pmr_entry_t *entry;

	*pmr_set_id = ODP_PMR_SET_INVAL;

	if (num_terms > ODP_CONFIG_PMRS_IN_PMRSET) {
		ODP_ERR("no of terms greater than supported ODP_PMRTERM_MAX\n");
		return -1;
	}

	entry = shres_alloc(pmr_tbl, typeof(*entry));
	if (!entry)
		return -1;

	entry->flags.activated = 0;
	entry->flags.default_pmr = 0;
	entry->pa_entry = NULL;
	entry->src_cos = NULL;
	entry->dst_cos = NULL;
	entry->pktio = ODP_PKTIO_INVALID;
	ODP_DBG("PMR created\n");

	pmr_word = 0;
	entry->layer = pmr_term_params[terms[0].term].layer;
	for (int i = 0; i < num_terms; i++) {
		if (pmr_term_validate(terms[i].term, terms[i].val_sz) < 0)
			goto err;

		if (pmr_word & (1 << terms[i].term)) {
			ODP_ERR("Cannot create PMRset with the same PMRs\n");
			goto err;
		}
		pmr_word |= (1 << terms[i].term);

		if (pmr_term_params[terms[i].term].layer != entry->layer) {
			ODP_ERR("Cannot create PMRset with PMRs on different layers\n");
			goto err;
		}

		memcpy(entry->terms[i].val, terms[i].val, terms[i].val_sz);
		memset(entry->terms[i].mask, 0xFF, terms[i].val_sz);
		entry->terms[i].term = terms[i].term;
	}

	entry->term_num = num_terms;
	*pmr_set_id = _odp_pmr_set_from_entry(entry);
	return num_terms;
err:
	shres_free(pmr_tbl, entry);
	return -1;
}

int odp_pmr_match_set_destroy(odp_pmr_set_t pmr_set)
{
	int ret;

	cls_tree_lock();
	if (pmr_set == ODP_PMR_SET_INVAL) {
		cls_tree_unlock();
		ODP_ERR("Invalid input parameters\n");
		return -1;
	}

	pmr_entry_t *entry = _odp_pmr_set_entry(pmr_set);
	ret = shres_free(pmr_tbl, entry);
	if (ret < 0)
		pmr_set = ODP_PMR_SET_INVAL;
	cls_tree_unlock();
	return ret;
}

int odp_pktio_pmr_match_set_cos(odp_pmr_set_t pmr_set, odp_pktio_t src_pktio,
				odp_cos_t dst_cos)
{
	pmr_entry_t *entry = _odp_pmr_set_entry(pmr_set);
	odp_pmr_t pmr = _odp_pmr_from_entry(entry);

	return odp_pktio_pmr_cos(pmr, src_pktio, dst_cos);
}

static inline int odp_pktio_pmr_cos_nolock(odp_pmr_t pmr, odp_pktio_t src_pktio,
					   odp_cos_t dst_cos)
{
	if (pmr == ODP_PMR_INVAL ||
	    src_pktio == ODP_PKTIO_INVALID || dst_cos == ODP_COS_INVALID) {
		ODP_ERR("Invalid input parameters\n");
		return -1;
	}

	pmr_entry_t *entry = _odp_pmr_entry(pmr);

	/* save links to restore in case of error */
	cos_entry_t *res_dst_cos = entry->dst_cos;
	cos_entry_t *res_src_cos = entry->src_cos;
	odp_pktio_t res_pktio = entry->pktio;

	cos_entry_t *dst_cos_entry = _odp_cos_entry(dst_cos);
	/*
	 * TODO: if PMR is currently activated, then we need to
	 * allocate and activate another one and add it to the list
	 * of "under" PMRs. For now suppose that it's not activated.
	 */
	ODP_AS_STR(entry->flags.activated == 0,
		   "Currently a PMR cannot be used in several places.");

	/* connect PMR in the tree */
	entry->dst_cos = dst_cos_entry;
	entry->src_cos = NULL;
	entry->pktio = src_pktio;
	cos_add_pmr_in(dst_cos_entry, entry);
	_pktio_add_pmr_out(src_pktio, &entry->src_cos_node);

	if (pmr_connection_validate(entry) < 0)
		goto err;

	/* activate tree, including connected PMRs */
	act_tree_deep = 0;
	if (cos_tree_activate(dst_cos_entry) < 0)
		goto err;

	/* save layer info to dst CoS */
	dst_cos_entry->pmr_layers.all |= (1 << entry->layer);
	return 0;
err:
	_pktio_rm_pmr_out(src_pktio, &entry->src_cos_node);
	cos_rm_pmr_in(dst_cos_entry, entry);
	entry->pktio = res_pktio;
	entry->src_cos = res_src_cos;
	entry->dst_cos = res_dst_cos;
	return -1;
}

int odp_pktio_pmr_cos(odp_pmr_t pmr, odp_pktio_t src_pktio, odp_cos_t dst_cos)
{
	int ret;

	cls_tree_lock();
	ret = odp_pktio_pmr_cos_nolock(pmr, src_pktio, dst_cos);
	cls_tree_unlock();

	return ret;
}

int __odp_pktio_pmr_cos_nolock(odp_pmr_t pmr, odp_pktio_t src_pktio,
			       odp_cos_t dst_cos)
{
	return odp_pktio_pmr_cos_nolock(pmr, src_pktio, dst_cos);
}

int odp_cos_pmr_cos(odp_pmr_t pmr, odp_cos_t src_cos, odp_cos_t dst_cos)
{
	cls_tree_lock();
	if (pmr == ODP_PMR_INVAL ||
	    src_cos == ODP_COS_INVALID || dst_cos == ODP_COS_INVALID) {
		cls_tree_unlock();
		return -1;
	}

	pmr_entry_t *entry = _odp_pmr_entry(pmr);
	cos_entry_t *dst_cos_entry = _odp_cos_entry(dst_cos);
	cos_entry_t *src_cos_entry = _odp_cos_entry(src_cos);

	/* save links to restore in case of error */
	cos_entry_t *res_dst_cos = entry->dst_cos;
	cos_entry_t *res_src_cos = entry->src_cos;

	entry->dst_cos = dst_cos_entry;
	entry->src_cos = src_cos_entry;

	cos_add_pmr_in(dst_cos_entry, entry);
	cos_add_pmr_out(src_cos_entry, entry);

	if (pmr_connection_validate(entry))
		goto err;

	/* if src CoS is not active - no need for activation */
	if (src_cos_entry->flags.activated) {
		/* activate tree, including connected PMRs */
		act_tree_deep = 0;
		if (cos_tree_activate(dst_cos_entry) < 0)
			goto err;
	}

	/* save layer info to dst CoS */
	dst_cos_entry->pmr_layers.all |= (1 << entry->layer);
	cls_tree_unlock();
	return 0;
err:
	cos_rm_pmr_out(src_cos_entry, entry);
	cos_rm_pmr_in(dst_cos_entry, entry);
	entry->src_cos = res_src_cos;
	entry->dst_cos = res_dst_cos;
	cls_tree_unlock();
	return -1;
}

static int cos_pool_queue_get(cos_entry_t *entry, odp_pool_t *pool,
			      odp_queue_t *queue)
{
	odp_pktio_t pktio;

	if (entry->pool == ODP_POOL_INVALID ||
	    entry->queue == ODP_QUEUE_INVALID) {
		pktio = cos_get_pktio(entry, 0);
		if (pktio == ODP_PKTIO_INVALID) {
			ODP_ERR("Ambiguous pktio when connecting %s CoS\n",
				entry->name);
			return -1;
		}
	}

	*pool = entry->pool == ODP_POOL_INVALID ? _pktio_in_pool(pktio) :
						 entry->pool;
	if (_odp_pool_get(*pool) < 0) {
		ODP_ERR("Pool was not allocated\n");
		return -1;
	}

	*queue = entry->queue == ODP_QUEUE_INVALID ? _pktio_inq_default(pktio) :
						    entry->queue;
	if (*queue == ODP_QUEUE_INVALID) {
		ODP_ERR("Cannot get inq_default for %s CoS\n", entry->name);
		goto put_pool;
	}

	if (_queue_get(*queue) < 0) {
		ODP_ERR("Queue was not allocated\n");
		goto put_pool;
	}

	return 0;

put_pool:
	if (entry->pool == ODP_POOL_INVALID)
		_odp_pool_put(*pool);
	return -1;
}

static int cos_activate(cos_entry_t *entry)
{
	odp_pool_t pool;
	odp_queue_t queue;

	paRouteInfo2_t route_host = {
		.validBitMap = 0,
		.dest = pa_DEST_HOST,
		.mRouteIndex = pa_NO_MULTI_ROUTE,
	};

	struct pa_entry_params_s l3_init_params = {
		.type = PA_LUT_TYPE_L3,
		.l3 = {
			.params = {
				.validBitMap = pa_PARAM_VALID_PREVLINK |
					       pa_PARAM_VALID_NEXTLINK,
				.routeInfo = &route_lut2_continue,
				.nextRtFail = &route_host,
			},
		},
	};

	if (cos_pool_queue_get(entry, &pool, &queue) < 0)
		return -1;

	entry->cppi_flow = _odp_pool_cppi_flow_create(pool,
						      _odp_pool_headroom(pool));
	if (!entry->cppi_flow) {
		ODP_ERR("Failed to create CPPI flow\n");
		goto put_pool_queue;
	}

	route_host.queue = _odp_queue_to_qmss_queue(queue);
	route_host.swInfo0 = _odp_queue_sched_tag(queue);
	route_host.flowId = cppi_flow_id(entry->cppi_flow);

	entry->l2_vlink = pa_add_virtual_link(pa_VIRTUAL_LNK_TYPE_MAC);
	if (!entry->l2_vlink) {
		ODP_ERR("Couldn't add virtual link\n");
		goto destroy_flow;
	}

	entry->l3_vlink = pa_add_virtual_link(pa_VIRTUAL_LNK_TYPE_OUTER_IP);
	if (!entry->l3_vlink) {
		ODP_ERR("Couldn't add virtual link\n");
		goto del_l2_vlink;
	}

	l3_init_params.l3.params.prevLink = entry->l2_vlink;
	l3_init_params.l3.params.nextLink = entry->l3_vlink;

	entry->pa_out_entry = pa_create_entry(&l3_init_params);
	if (!entry->pa_out_entry) {
		ODP_ERR("failed to create L3 entry\n");
		goto del_l3_vlink;
	}

	entry->flags.activated = 1;
	ODP_DBG("CoS \"%s\" activated\n", entry->name);
	return 0;
del_l3_vlink:
	pa_del_virtual_link(entry->l3_vlink);
	entry->l3_vlink = NULL;

del_l2_vlink:
	pa_del_virtual_link(entry->l2_vlink);
	entry->l2_vlink = NULL;
destroy_flow:
	cppi_flow_destroy(entry->cppi_flow);
	entry->cppi_flow = NULL;
put_pool_queue:
	_odp_pool_put(pool);
	_queue_put(queue);
	return -1;
}

static int cos_pool_queue_put(cos_entry_t *entry)
{
	odp_pool_t pool;
	odp_queue_t queue;
	odp_pktio_t pktio;

	if (entry->pool == ODP_POOL_INVALID ||
	    entry->queue == ODP_QUEUE_INVALID) {
		pktio = cos_get_pktio(entry, 1);
		if (pktio == ODP_PKTIO_INVALID) {
			ODP_ERR("Ambiguous pktio when deactivating %s CoS\n",
				entry->name);
			return -1;
		}
	}

	pool = entry->pool == ODP_POOL_INVALID ? _pktio_in_pool(pktio) :
						 entry->pool;
	_odp_pool_put(pool);

	queue = entry->queue == ODP_QUEUE_INVALID ? _pktio_inq_default(pktio) :
						    entry->queue;
	if (queue == ODP_QUEUE_INVALID) {
		ODP_ERR("Cannot get queue for %s CoS\n", entry->name);
		return -1;
	}
	_queue_put(queue);

	return 0;
}

static int cos_deactivate(cos_entry_t *entry)
{
	if (pa_destroy_entry(entry->pa_out_entry) < 0)
		return -1;
	if (cppi_flow_destroy(entry->cppi_flow) < 0)
		return -1;
	if (cos_pool_queue_put(entry) < 0)
		return -1;

	entry->flags.activated = 0;
	ODP_DBG("CoS \"%s\" deactivated\n", entry->name);
	return 0;
}

/*
 * This primitive function is called only if pmr can be
 * really activated, that is when src CoS is active and
 * dst CoS is active
 */
static int pmr_activate(pmr_entry_t *entry)
{
	int ret;
	odp_pool_t pool;
	odp_queue_t queue;

	if (cos_pool_queue_get(entry->dst_cos, &pool, &queue) < 0)
		return -1;

	switch (entry->layer) {
	case L2_TERM:
		ret = pmr_l2_activate(entry, queue);
		break;
	case L3_TERM:
		ret = pmr_l3_activate(entry, queue);
		break;
	case L4_TERM:
		ret = pmr_l4_activate(entry, queue);
		break;
	default:
		ret = -1;
		ODP_ABORT("Unexpected term layer\n");
	}

	if (ret) {
		ODP_ERR("failed to activate PMR\n");
		goto err;
	}

	if (cppi_flow_get(entry->dst_cos->cppi_flow) < 0)
		goto err;

	entry->flags.activated = 1;
	ODP_DBG("\"%s\" - PMR - \"%s\" activated\n", entry->pktio ?
		_pktio_name(entry->pktio) : entry->src_cos->name,
		entry->dst_cos->name);
	return 0;
err:
	_odp_pool_put(pool);
	_queue_put(queue);
	return -1;
}

static int pmr_deactivate(pmr_entry_t *entry)
{
	if (pa_destroy_entry(entry->pa_entry))
		return -1;

	entry->pa_entry = NULL;
	entry->flags.activated = 0;

	if (cos_pool_queue_put(entry->dst_cos) < 0)
		return -1;

	if (cppi_flow_put(entry->dst_cos->cppi_flow) < 0)
		return -1;

	ODP_DBG("\"%s\" - PMR - \"%s\" deactivated\n", entry->pktio ?
		_pktio_name(entry->pktio) : entry->src_cos->name,
		entry->dst_cos->name);

	return 0;
}

/**
 * cos_tree_activate - activate the tree after given CoS, including CoS itself and
 * all src PMRs connected to it, if the PMRs have activated src.
 * returns -1 when error and 0 on success
 */
static int cos_tree_activate(cos_entry_t *entry)
{
	pmr_entry_t *pmr;
	int activation = 0;

	/* activate "in" PMRs and CoS itself */
	list_for_each(&entry->pmr_in, pmr, dst_cos_node) {
		if (pmr->flags.activated)
			continue;

		/* skip PMRs that cannot be activated */
		if (pmr->src_cos && !pmr->src_cos->flags.activated)
			continue;

		/* activate given CoS if needed */
		if (!entry->flags.activated) {
			if (cos_activate(entry) < 0) {
				ODP_ERR("failed to activate CoS: %s\n",
					entry->name);
				goto err;
			}
			activation = 1;
			++act_tree_deep;
		}

		/* now activate PMR, as we have dst and src CoSes */
		if (pmr_activate(pmr) < 0) {
			ODP_ERR("failed to activate PMR of CoS: %s\n",
				entry->name);
			goto err;
		}
	}

	/* Cos was active initially, so no need to continue */
	if (!activation)
		return 0;

	list_for_each(&entry->pmr_out, pmr, src_cos_node) {
		ODP_AS_STR(act_tree_deep < ODP_CONFIG_COS_ENTRIES,
			   "CoS number overhead");
		if (!pmr->dst_cos)
			continue;
		if (cos_tree_activate(pmr->dst_cos) < 0)
			goto err;
	}

	--act_tree_deep;
	return 0;
err:
	ODP_ERR("Cannot activate some parts of the tree");
	act_tree_deep = 0;
	return -1;
}

/*
 * Deactivate the tree after given CoS. All tree after the CoS will be
 * unactivated, but if for some reason one of the CoSes after the tree
 * is on another active branch it's entry and after it will stay active.
 * It also invalidets all src PMR connected to given CoS.
 *
 * Returns -1 on error and 0 on success
 */
static int cos_tree_deactivate(cos_entry_t *entry)
{
	pmr_entry_t *pmr;

	/* deactivate "in" PMRs for root CoS */
	list_for_each(&entry->pmr_in, pmr, dst_cos_node) {
		if (!pmr->flags.activated)
			continue;

		if (act_tree_deep)
			return 0;

		if (pmr_deactivate(pmr) < 0)
			goto err;
	}

	if (entry->flags.activated) {
		if (cos_deactivate(entry) < 0) {
			ODP_ERR("failed to deactivate CoS: %s\n", entry->name);
			goto err;
		}
		++act_tree_deep;
	} else {
		return 0;
	}

	/* deactivate "out" PMRs and their dst CoSes*/
	list_for_each(&entry->pmr_out, pmr, src_cos_node) {
		if (!pmr->flags.activated)
			continue;

		if (pmr_deactivate(pmr) < 0)
			goto err;

		if (cos_tree_deactivate(pmr->dst_cos) < 0)
			goto err;
	}

	--act_tree_deep;
	return 0;

err:
	ODP_ERR("Cannot activate some parts of the tree\n");
	act_tree_deep = 0;
	return -1;
}

odp_cos_t _pmr_dst_cos(odp_pmr_t pmr)
{
	pmr_entry_t *entry = _odp_pmr_entry(pmr);
	return _odp_cos_from_entry(entry->dst_cos);
}

void _pmr_mark_default(odp_pmr_t default_pmr)
{
	pmr_entry_t *entry;

	entry = _odp_pmr_entry(default_pmr);
	entry->flags.default_pmr = 1;
}

int _cls_deactivate_pmr_list(struct list_head *pmr_list)
{
	pmr_entry_t *entry;

	list_for_each(pmr_list, entry, src_cos_node) {
		act_tree_deep = 0;
		if (cos_tree_deactivate(entry->dst_cos) < 0)
			return -1;
	}

	return 0;
}

int _cls_activate_pmr_list(struct list_head *pmr_list)
{
	pmr_entry_t *entry;

	list_for_each(pmr_list, entry, src_cos_node) {
		act_tree_deep = 0;
		if (cos_tree_activate(entry->dst_cos) < 0)
			return -1;
	}

	return 0;
}

int _cls_disconnect_pmr_list(struct list_head *pmr_list)
{
	pmr_entry_t *entry, *nentry;

	list_for_each_safe(pmr_list, entry, nentry, src_cos_node) {
		act_tree_deep = 0;
		if (cos_tree_deactivate(entry->dst_cos) < 0)
			return -1;

		pmr_disconnect_source(entry);
	}

	return 0;
}

static inline void _list_replace(struct list_head *old, struct list_head *new)
{
	if (list_empty(old))
		return;

	list_swap(&old->n, &new->n);
}

void _cls_replace_cos(odp_cos_t old_cos, odp_cos_t new_cos)
{
	pmr_entry_t *pmr_entry;
	cos_entry_t *old_entry = _odp_cos_entry(old_cos);
	cos_entry_t *new_entry = _odp_cos_entry(new_cos);

	_list_replace(&old_entry->pmr_in, &new_entry->pmr_in);
	_list_replace(&old_entry->pmr_out, &new_entry->pmr_out);
	new_entry->pmr_in_cnt = old_entry->pmr_in_cnt;
	new_entry->pmr_out_cnt = old_entry->pmr_out_cnt;
	new_entry->pmr_layers = old_entry->pmr_layers;
	new_entry->flags = old_entry->flags;

	list_for_each(&new_entry->pmr_in, pmr_entry, dst_cos_node)
		pmr_entry->dst_cos = new_entry;

	list_for_each(&new_entry->pmr_out, pmr_entry, src_cos_node)
		pmr_entry->src_cos = new_entry;

	list_head_init(&old_entry->pmr_in);
	list_head_init(&old_entry->pmr_out);
	old_entry->pmr_in_cnt = 0;
	old_entry->pmr_out_cnt = 0;
	old_entry->pmr_layers.all = 0;
	old_entry->flags.activated = 0;
}