Linux Audio

Check our new training course

Loading...
   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
/* main.c - Application main entry point */

/*
 * Copyright (c) 2018 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#define NET_LOG_LEVEL CONFIG_NET_TC_LOG_LEVEL

#include <logging/log.h>
LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);

#include <zephyr/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <sys/printk.h>
#include <linker/sections.h>

#include <ztest.h>

#include <net/ethernet.h>
#include <net/dummy.h>
#include <net/buf.h>
#include <net/net_ip.h>
#include <net/net_l2.h>
#include <net/udp.h>

#include "ipv6.h"

#define NET_LOG_ENABLED 1
#include "net_private.h"

#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
#define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

/* make this large enough so that we do not overflow the sent pkt array */
#define MAX_PKT_TO_SEND 4
#define MAX_PKT_TO_RECV 4

#define MAX_PRIORITIES 8
#define MAX_TC 8

static enum net_priority send_priorities[MAX_TC][MAX_PKT_TO_SEND];
static enum net_priority recv_priorities[MAX_TC][MAX_PKT_TO_RECV];

static enum net_priority tx_tc2prio[NET_TC_TX_COUNT];
static enum net_priority rx_tc2prio[NET_TC_RX_COUNT];

#define PORT 9999

static const char *test_data = "Test data to be sent";

/* Interface 1 addresses */
static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
					0, 0, 0, 0, 0, 0, 0, 0x1 } } };

/* Interface 2 addresses */
static struct in6_addr my_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
					0, 0, 0, 0, 0, 0, 0, 0x1 } } };

/* Interface 3 addresses */
static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 2, 0, 0, 0,
					0, 0, 0, 0, 0, 0, 0, 0x1 } } };

/* Destination address for test packets */
static struct in6_addr dst_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 9, 0, 0, 0,
					0, 0, 0, 0, 0, 0, 0, 0x1 } } };

/* Extra address is assigned to ll_addr */
static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
				       0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
				       0x04 } } };

static struct sockaddr_in6 dst_addr6 = {
	.sin6_family = AF_INET6,
	.sin6_port = htons(PORT),
};

static struct {
	struct net_context *ctx;
} net_ctxs[NET_TC_COUNT];

static bool test_started;
static bool test_failed;
static bool start_receiving;
static bool recv_cb_called;
static struct k_sem wait_data;

#define WAIT_TIME K_SECONDS(1)

struct eth_context {
	struct net_if *iface;
	u8_t mac_addr[6];

	u16_t expecting_tag;
};

static struct eth_context eth_context;

static void eth_iface_init(struct net_if *iface)
{
	struct device *dev = net_if_get_device(iface);
	struct eth_context *context = dev->driver_data;

	net_if_set_link_addr(iface, context->mac_addr,
			     sizeof(context->mac_addr),
			     NET_LINK_ETHERNET);
}

static bool check_higher_priority_pkt_sent(int tc, struct net_pkt *pkt)
{
	/* If we have sent any higher priority packets, then
	 * this test fails as those packets should have been
	 * sent before this one.
	 */
	int j, k;

	for (j = tc + 1; j < MAX_TC; j++) {
		for (k = 0; k < MAX_PKT_TO_SEND; k++) {
			if (send_priorities[j][k]) {
				return true;
			}
		}
	}

	return false;
}

static bool check_higher_priority_pkt_recv(int tc, struct net_pkt *pkt)
{
	/* If we have received any higher priority packets, then
	 * this test fails as those packets should have been
	 * received before this one.
	 */
	int j, k;

	for (j = tc + 1; j < MAX_TC; j++) {
		for (k = 0; k < MAX_PKT_TO_SEND; k++) {
			if (recv_priorities[j][k]) {
				return true;
			}
		}
	}

	return false;
}

/* The eth_tx() will handle both sent packets or and it will also
 * simulate the receiving of the packets.
 */
static int eth_tx(struct device *dev, struct net_pkt *pkt)
{
	if (!pkt->buffer) {
		DBG("No data to send!\n");
		return -ENODATA;
	}

	if (start_receiving) {
		struct in6_addr addr;
		struct net_udp_hdr hdr, *udp_hdr;
		u16_t port;

		DBG("Packet %p received\n", pkt);

		/* Swap IP src and destination address so that we can receive
		 * the packet and the stack will not reject it.
		 */
		net_ipaddr_copy(&addr, &NET_IPV6_HDR(pkt)->src);
		net_ipaddr_copy(&NET_IPV6_HDR(pkt)->src,
				&NET_IPV6_HDR(pkt)->dst);
		net_ipaddr_copy(&NET_IPV6_HDR(pkt)->dst, &addr);

		udp_hdr = net_udp_get_hdr(pkt, &hdr);
		zassert_not_null(udp_hdr, "UDP header missing");

		port = udp_hdr->src_port;
		udp_hdr->src_port = udp_hdr->dst_port;
		udp_hdr->dst_port = port;

		if (net_recv_data(net_pkt_iface(pkt),
				  net_pkt_clone(pkt, K_NO_WAIT)) < 0) {
			test_failed = true;
			zassert_true(false, "Packet %p receive failed\n", pkt);
		}

		return 0;
	}

	if (test_started) {
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
		k_tid_t thread = k_current_get();
#endif
		int i, prio, ret;

		prio = net_pkt_priority(pkt);

		for (i = 0; i < MAX_PKT_TO_SEND; i++) {
			ret = check_higher_priority_pkt_sent(
				net_tx_priority2tc(prio), pkt);
			if (ret) {
				DBG("Current thread priority %d "
				    "pkt %p prio %d tc %d\n",
				    k_thread_priority_get(thread),
				    pkt, prio, net_tx_priority2tc(prio));

				test_failed = true;
				zassert_false(test_failed,
					      "Invalid priority sent %d TC %d,"
					      " expecting %d (pkt %p)\n",
					      prio,
					      net_tx_priority2tc(prio),
				  send_priorities[net_tx_priority2tc(prio)][i],
					      pkt);
				goto fail;
			}

			send_priorities[net_tx_priority2tc(prio)][i] = 0;
		}

		DBG("Received pkt %p from TC %c (thread prio %d)\n", pkt,
		    *(pkt->frags->data +
		      sizeof(struct net_ipv6_hdr) +
		      sizeof(struct net_udp_hdr)),
		    k_thread_priority_get(thread));

		k_sem_give(&wait_data);
	}

fail:
	return 0;
}

static struct dummy_api api_funcs = {
	.iface_api.init	= eth_iface_init,
	.send	= eth_tx,
};

static void generate_mac(u8_t *mac_addr)
{
	/* 00-00-5E-00-53-xx Documentation RFC 7042 */
	mac_addr[0] = 0x00;
	mac_addr[1] = 0x00;
	mac_addr[2] = 0x5E;
	mac_addr[3] = 0x00;
	mac_addr[4] = 0x53;
	mac_addr[5] = sys_rand32_get();
}

static int eth_init(struct device *dev)
{
	struct eth_context *context = dev->driver_data;

	generate_mac(context->mac_addr);

	return 0;
}

/* Create one ethernet interface that does not have VLAN support. This
 * is quite unlikely that this would be done in real life but for testing
 * purposes create it here.
 */
NET_DEVICE_INIT(eth_test, "eth_test", eth_init, &eth_context,
		NULL, CONFIG_ETH_INIT_PRIORITY, &api_funcs,
		DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2),
		NET_ETH_MTU);

static void address_setup(void)
{
	struct net_if_addr *ifaddr;
	struct net_if *iface1;

	iface1 = net_if_get_default();

	zassert_not_null(iface1, "Interface 1");

	ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
				      NET_ADDR_MANUAL, 0);
	if (!ifaddr) {
		DBG("Cannot add IPv6 address %s\n",
		       net_sprint_ipv6_addr(&my_addr1));
		zassert_not_null(ifaddr, "addr1");
	}

	/* For testing purposes we need to set the adddresses preferred */
	ifaddr->addr_state = NET_ADDR_PREFERRED;

	ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr,
				      NET_ADDR_MANUAL, 0);
	if (!ifaddr) {
		DBG("Cannot add IPv6 address %s\n",
		       net_sprint_ipv6_addr(&ll_addr));
		zassert_not_null(ifaddr, "ll_addr");
	}

	ifaddr->addr_state = NET_ADDR_PREFERRED;

	ifaddr = net_if_ipv6_addr_add(iface1, &my_addr2,
				      NET_ADDR_MANUAL, 0);
	if (!ifaddr) {
		DBG("Cannot add IPv6 address %s\n",
		       net_sprint_ipv6_addr(&my_addr2));
		zassert_not_null(ifaddr, "addr2");
	}

	ifaddr->addr_state = NET_ADDR_PREFERRED;

	ifaddr = net_if_ipv6_addr_add(iface1, &my_addr3,
				      NET_ADDR_MANUAL, 0);
	if (!ifaddr) {
		DBG("Cannot add IPv6 address %s\n",
		       net_sprint_ipv6_addr(&my_addr3));
		zassert_not_null(ifaddr, "addr3");
	}

	net_if_up(iface1);

	/* The interface might receive data which might fail the checks
	 * in the iface sending function, so we need to reset the failure
	 * flag.
	 */
	test_failed = false;
}

static void priority_setup(void)
{
	int i;

	for (i = 0; i < MAX_PRIORITIES; i++) {
		tx_tc2prio[net_tx_priority2tc(i)] = i;
		rx_tc2prio[net_rx_priority2tc(i)] = i;
	}
}

#if defined(CONFIG_NET_IPV6_NBR_CACHE)
static bool add_neighbor(struct net_if *iface, struct in6_addr *addr)
{
	struct net_linkaddr_storage llstorage;
	struct net_linkaddr lladdr;
	struct net_nbr *nbr;

	llstorage.addr[0] = 0x01;
	llstorage.addr[1] = 0x02;
	llstorage.addr[2] = 0x33;
	llstorage.addr[3] = 0x44;
	llstorage.addr[4] = 0x05;
	llstorage.addr[5] = 0x06;

	lladdr.len = 6U;
	lladdr.addr = llstorage.addr;
	lladdr.type = NET_LINK_ETHERNET;

	nbr = net_ipv6_nbr_add(iface, addr, &lladdr, false,
			       NET_IPV6_NBR_STATE_REACHABLE);
	if (!nbr) {
		DBG("Cannot add dst %s to neighbor cache\n",
		    net_sprint_ipv6_addr(addr));
		return false;
	}

	return true;
}
#else
#define add_neighbor(iface, addr) true
#endif /* CONFIG_NET_IPV6_NBR_CACHE */

static void setup_net_context(struct net_context **ctx)
{
	struct sockaddr_in6 src_addr6 = {
		.sin6_family = AF_INET6,
		.sin6_port = 0,
	};
	int ret;
	struct net_if *iface1;

	iface1 = net_if_get_default();

	ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, ctx);
	zassert_equal(ret, 0, "Create IPv6 UDP context %p failed (%d)\n",
		      *ctx, ret);

	memcpy(&src_addr6.sin6_addr, &my_addr1, sizeof(struct in6_addr));
	memcpy(&dst_addr6.sin6_addr, &dst_addr, sizeof(struct in6_addr));

	ret = add_neighbor(iface1, &dst_addr);
	zassert_true(ret, "Cannot add neighbor");

	ret = net_context_bind(*ctx, (struct sockaddr *)&src_addr6,
			       sizeof(struct sockaddr_in6));
	zassert_equal(ret, 0,
		      "Context bind failure test failed (%d)\n", ret);
}

static void traffic_class_general_setup(void)
{
	address_setup();
	priority_setup();
}

static void traffic_class_setup(enum net_priority *tc2prio, int count)
{
	u8_t priority;
	int i, ret;

	for (i = 0; i < count; i++) {
		setup_net_context(&net_ctxs[i].ctx);

		priority = tc2prio[i];

		ret = net_context_set_option(net_ctxs[i].ctx,
					     NET_OPT_PRIORITY,
					     &priority, sizeof(priority));
		zassert_equal(ret, 0,
			      "Cannot set priority %d to ctx %p (%d)\n",
			      priority, net_ctxs[i].ctx, ret);
	}
}

static void traffic_class_setup_tx(void)
{
	traffic_class_setup(tx_tc2prio, NET_TC_TX_COUNT);
}

static void traffic_class_setup_rx(void)
{
	traffic_class_setup(rx_tc2prio, NET_TC_RX_COUNT);
}

static void traffic_class_cleanup(int count)
{
	int i;

	for (i = 0; i < count; i++) {
		if (net_ctxs[i].ctx) {
			net_context_unref(net_ctxs[i].ctx);
			net_ctxs[i].ctx = NULL;
		}
	}
}

static void traffic_class_cleanup_tx(void)
{
	traffic_class_cleanup(NET_TC_TX_COUNT);
}

static void traffic_class_cleanup_rx(void)
{
	traffic_class_cleanup(NET_TC_RX_COUNT);
}

static void traffic_class_send_packets_with_prio(enum net_priority prio,
						 int pkt_count)
{
	/* Start to send data to each queue and verify that the data
	 * is received in correct order.
	 */
	u8_t data[128];
	int len, ret;
	int tc = net_tx_priority2tc(prio);

	/* Convert num to ascii */
	data[0] = tc + 0x30;
	len = strlen(test_data);
	memcpy(data+1, test_data, strlen(test_data));

	len += 1;

	test_started = true;

	DBG("Sending on TC %d priority %d\n", tc, prio);

	send_priorities[net_tx_priority2tc(prio)][pkt_count - 1] = prio + 1;

	ret = net_context_sendto(net_ctxs[tc].ctx, data, len,
				 (struct sockaddr *)&dst_addr6,
				 sizeof(struct sockaddr_in6),
				 NULL, K_NO_WAIT, NULL);
	zassert_true(ret > 0, "Send UDP pkt failed");
}

static void traffic_class_send_priority(enum net_priority prio,
					int num_packets,
					bool wait_for_packets)
{
	int i;

	if (wait_for_packets) {
		k_sem_init(&wait_data, MAX_PKT_TO_SEND, UINT_MAX);
	}

	for (i = 0; i < num_packets; i++) {
		traffic_class_send_packets_with_prio(prio, i + 1);
	}

	if (wait_for_packets) {
		if (k_sem_take(&wait_data, WAIT_TIME)) {
			DBG("Timeout while waiting ok status\n");
			zassert_false(true, "Timeout");
		}

		/* This sleep is needed here so that the sending side
		 * can run properly.
		 */
		k_sleep(K_MSEC(1));
	}
}

static void traffic_class_send_data_prio_bk(void)
{
	/* Send number of packets with each priority and make sure
	 * they are sent properly.
	 */
	traffic_class_send_priority(NET_PRIORITY_BK, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_be(void)
{
	traffic_class_send_priority(NET_PRIORITY_BE, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_ee(void)
{
	traffic_class_send_priority(NET_PRIORITY_EE, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_ca(void)
{
	traffic_class_send_priority(NET_PRIORITY_CA, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_vi(void)
{
	traffic_class_send_priority(NET_PRIORITY_VI, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_vo(void)
{
	traffic_class_send_priority(NET_PRIORITY_VO, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_ic(void)
{
	traffic_class_send_priority(NET_PRIORITY_IC, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_prio_nc(void)
{
	traffic_class_send_priority(NET_PRIORITY_NC, MAX_PKT_TO_SEND, true);
}

static void traffic_class_send_data_mix(void)
{
	/* Start to send data to each queue and verify that the data
	 * is received in correct order.
	 */
	int total_packets = 0;

	(void)memset(send_priorities, 0, sizeof(send_priorities));

	traffic_class_send_priority(NET_PRIORITY_BK, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_BE, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	/* The semaphore is released as many times as we have sent packets */
	k_sem_init(&wait_data, total_packets, UINT_MAX);

	if (k_sem_take(&wait_data, WAIT_TIME)) {
		DBG("Timeout while waiting ok status\n");
		zassert_false(true, "Timeout");
	}

	zassert_false(test_failed, "Traffic class verification failed.");
}

static void traffic_class_send_data_mix_all_1(void)
{
	int total_packets = 0;

	(void)memset(send_priorities, 0, sizeof(send_priorities));

	traffic_class_send_priority(NET_PRIORITY_BK, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_BE, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_EE, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_CA, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_VI, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_VO, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_IC, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	traffic_class_send_priority(NET_PRIORITY_NC, MAX_PKT_TO_SEND, false);
	total_packets += MAX_PKT_TO_SEND;

	/* The semaphore is released as many times as we have sent packets */
	k_sem_init(&wait_data, total_packets, UINT_MAX);

	if (k_sem_take(&wait_data, WAIT_TIME)) {
		DBG("Timeout while waiting ok status\n");
		zassert_false(true, "Timeout");
	}

	zassert_false(test_failed, "Traffic class verification failed.");
}

static void traffic_class_send_data_mix_all_2(void)
{
	/* Start to send data to each queue and verify that the data
	 * is received in correct order.
	 */
	int total_packets = 0;
	int i;

	(void)memset(send_priorities, 0, sizeof(send_priorities));

	/* In this test send one packet for each queue instead of sending
	 * n packets to same queue at a time.
	 */
	for (i = 0; i < MAX_PKT_TO_SEND; i++) {
		traffic_class_send_priority(NET_PRIORITY_BK, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_BE, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_EE, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_CA, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_VI, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_VO, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_IC, 1, false);
		total_packets += 1;

		traffic_class_send_priority(NET_PRIORITY_NC, 1, false);
		total_packets += 1;
	}

	/* The semaphore is released as many times as we have sent packets */
	k_sem_init(&wait_data, total_packets, UINT_MAX);

	if (k_sem_take(&wait_data, WAIT_TIME)) {
		DBG("Timeout while waiting ok status\n");
		zassert_false(true, "Timeout");
	}

	zassert_false(test_failed, "Traffic class verification failed.");
}

static void recv_cb(struct net_context *context,
		    struct net_pkt *pkt,
		    union net_ip_header *ip_hdr,
		    union net_proto_header *proto_hdr,
		    int status,
		    void *user_data)
{
#if NET_LOG_LEVEL >= LOG_LEVEL_DBG
	k_tid_t thread = k_current_get();
#endif
	int i, prio, ret;

	DBG("Data received in priority %d\n", k_thread_priority_get(thread));

	prio = net_pkt_priority(pkt);

	for (i = 0; i < MAX_PKT_TO_RECV; i++) {
		ret = check_higher_priority_pkt_recv(net_rx_priority2tc(prio),
						     pkt);
		if (ret) {
			DBG("Current thread priority %d "
			    "pkt %p prio %d tc %d\n",
			    k_thread_priority_get(thread),
			    pkt, prio, net_rx_priority2tc(prio));

			test_failed = true;
			zassert_false(test_failed,
				      "Invalid priority received %d TC %d,"
				      " expecting %d (pkt %p)\n",
				      prio,
				      net_rx_priority2tc(prio),
				  recv_priorities[net_rx_priority2tc(prio)][i],
				      pkt);
			goto fail;
		}

		recv_priorities[net_rx_priority2tc(prio)][i] = 0;
	}

fail:
	recv_cb_called = true;
	k_sem_give(&wait_data);

	net_pkt_unref(pkt);
}

static void traffic_class_setup_recv(void)
{
	int ret, i;

	recv_cb_called = false;

	for (i = 0; i < NET_TC_RX_COUNT; i++) {
		ret = net_context_recv(net_ctxs[i].ctx, recv_cb,
				       K_NO_WAIT, NULL);
		zassert_equal(ret, 0,
			      "[%d] Context recv UDP setup failed (%d)\n",
			      i, ret);
	}
}

static void traffic_class_recv_packets_with_prio(enum net_priority prio,
						 int pkt_count)
{
	/* Start to receive data to each queue and verify that the data
	 * is received in correct order.
	 */
	u8_t data[128];
	int len, ret;
	int tc = net_rx_priority2tc(prio);
	const struct in6_addr *src_addr;
	struct net_if_addr *ifaddr;
	struct net_if *iface = NULL;

	/* Convert num to ascii */
	data[0] = tc + 0x30;
	len = strlen(test_data);
	memcpy(data+1, test_data, strlen(test_data));

	len += 1;

	test_started = true;
	start_receiving = true;

	DBG("Receiving on TC %d priority %d\n", tc, prio);

	recv_priorities[net_rx_priority2tc(prio)][pkt_count - 1] = prio + 1;

	src_addr = net_if_ipv6_select_src_addr(NULL, &dst_addr);
	zassert_not_null(src_addr, "Cannot select source address");

	ifaddr = net_if_ipv6_addr_lookup(src_addr, &iface);
	zassert_not_null(ifaddr, "Cannot find source address");
	zassert_not_null(iface, "Interface not found");

	/* We cannot use net_recv_data() here as the packet does not have
	 * UDP header.
	 */
	ret = net_context_sendto(net_ctxs[tc].ctx, data, len,
				 (struct sockaddr *)&dst_addr6,
				 sizeof(struct sockaddr_in6),
				 NULL, K_NO_WAIT, NULL);
	zassert_true(ret > 0, "Send UDP pkt failed");

	/* Let the receiver to receive the packets */
	k_sleep(K_MSEC(1));
}

static void traffic_class_recv_priority(enum net_priority prio,
					int num_packets,
					bool wait_for_packets)
{
	int i;

	if (wait_for_packets) {
		k_sem_init(&wait_data, MAX_PKT_TO_RECV, UINT_MAX);
	}

	for (i = 0; i < num_packets; i++) {
		traffic_class_recv_packets_with_prio(prio, i + 1);
	}

	if (wait_for_packets) {
		if (k_sem_take(&wait_data, WAIT_TIME)) {
			DBG("Timeout while waiting ok status\n");
			zassert_false(true, "Timeout");
		}

		/* This sleep is needed here so that the receiving side
		 * can run properly.
		 */
		k_sleep(K_MSEC(1));
	}
}

static void traffic_class_recv_data_prio_bk(void)
{
	/* Receive number of packets with each priority and make sure
	 * they are received properly.
	 */
	traffic_class_recv_priority(NET_PRIORITY_BK, MAX_PKT_TO_RECV, true);

	zassert_false(test_failed, "Traffic class verification failed.");
}

static void traffic_class_recv_data_prio_be(void)
{
	traffic_class_recv_priority(NET_PRIORITY_BE, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_prio_ee(void)
{
	traffic_class_recv_priority(NET_PRIORITY_EE, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_prio_ca(void)
{
	traffic_class_recv_priority(NET_PRIORITY_CA, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_prio_vi(void)
{
	traffic_class_recv_priority(NET_PRIORITY_VI, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_prio_vo(void)
{
	traffic_class_recv_priority(NET_PRIORITY_VO, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_prio_ic(void)
{
	traffic_class_recv_priority(NET_PRIORITY_IC, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_prio_nc(void)
{
	traffic_class_recv_priority(NET_PRIORITY_NC, MAX_PKT_TO_RECV, true);
}

static void traffic_class_recv_data_mix(void)
{
	/* Start to receive data to each queue and verify that the data
	 * is received in correct order.
	 */
	int total_packets = 0;

	(void)memset(recv_priorities, 0, sizeof(recv_priorities));

	traffic_class_recv_priority(NET_PRIORITY_BK, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_BE, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	/* The semaphore is released as many times as we have sent packets */
	k_sem_init(&wait_data, total_packets, UINT_MAX);

	if (k_sem_take(&wait_data, WAIT_TIME)) {
		DBG("Timeout while waiting ok status\n");
		zassert_false(true, "Timeout");
	}

	zassert_false(test_failed, "Traffic class verification failed.");
}

static void traffic_class_recv_data_mix_all_1(void)
{
	int total_packets = 0;

	(void)memset(recv_priorities, 0, sizeof(recv_priorities));

	traffic_class_recv_priority(NET_PRIORITY_BK, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_BE, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_EE, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_CA, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_VI, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_VO, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_IC, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	traffic_class_recv_priority(NET_PRIORITY_NC, MAX_PKT_TO_RECV, false);
	total_packets += MAX_PKT_TO_RECV;

	/* The semaphore is released as many times as we have sent packets */
	k_sem_init(&wait_data, total_packets, UINT_MAX);

	if (k_sem_take(&wait_data, WAIT_TIME)) {
		DBG("Timeout while waiting ok status\n");
		zassert_false(true, "Timeout");
	}

	zassert_false(test_failed, "Traffic class verification failed.");
}

static void traffic_class_recv_data_mix_all_2(void)
{
	/* Start to receive data to each queue and verify that the data
	 * is received in correct order.
	 */
	int total_packets = 0;
	int i;

	(void)memset(recv_priorities, 0, sizeof(recv_priorities));

	/* In this test receive one packet for each queue instead of receiving
	 * n packets to same queue at a time.
	 */
	for (i = 0; i < MAX_PKT_TO_RECV; i++) {
		traffic_class_recv_priority(NET_PRIORITY_BK, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_BE, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_EE, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_CA, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_VI, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_VO, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_IC, 1, false);
		total_packets += 1;

		traffic_class_recv_priority(NET_PRIORITY_NC, 1, false);
		total_packets += 1;
	}

	/* The semaphore is released as many times as we have sent packets */
	k_sem_init(&wait_data, total_packets, UINT_MAX);

	if (k_sem_take(&wait_data, WAIT_TIME)) {
		DBG("Timeout while waiting ok status\n");
		zassert_false(true, "Timeout");
	}

	zassert_false(test_failed, "Traffic class verification failed.");
}

void test_main(void)
{
	ztest_test_suite(net_traffic_class_test,
			 ztest_unit_test(traffic_class_general_setup),
			 ztest_unit_test(traffic_class_setup_tx),
			 /* Send only same priority packets and verify that
			  * all are sent with proper traffic class.
			  */
			 ztest_unit_test(traffic_class_send_data_prio_bk),
			 ztest_unit_test(traffic_class_send_data_prio_be),
			 ztest_unit_test(traffic_class_send_data_prio_ee),
			 ztest_unit_test(traffic_class_send_data_prio_ca),
			 ztest_unit_test(traffic_class_send_data_prio_vi),
			 ztest_unit_test(traffic_class_send_data_prio_vo),
			 ztest_unit_test(traffic_class_send_data_prio_ic),
			 ztest_unit_test(traffic_class_send_data_prio_nc),
			 /* Then mix traffic classes and verify that higher
			  * class packets are sent first.
			  */
			 ztest_unit_test(traffic_class_send_data_mix),
			 ztest_unit_test(traffic_class_send_data_mix_all_1),
			 ztest_unit_test(traffic_class_send_data_mix_all_2),
			 ztest_unit_test(traffic_class_cleanup_tx),

			 /* Same tests for received packets */
			 ztest_unit_test(traffic_class_setup_rx),
			 ztest_unit_test(traffic_class_setup_recv),
			 ztest_unit_test(traffic_class_recv_data_prio_bk),
			 ztest_unit_test(traffic_class_recv_data_prio_be),
			 ztest_unit_test(traffic_class_recv_data_prio_ee),
			 ztest_unit_test(traffic_class_recv_data_prio_ca),
			 ztest_unit_test(traffic_class_recv_data_prio_vi),
			 ztest_unit_test(traffic_class_recv_data_prio_vo),
			 ztest_unit_test(traffic_class_recv_data_prio_ic),
			 ztest_unit_test(traffic_class_recv_data_prio_nc),
			 ztest_unit_test(traffic_class_recv_data_mix),
			 ztest_unit_test(traffic_class_recv_data_mix_all_1),
			 ztest_unit_test(traffic_class_recv_data_mix_all_2),
			 ztest_unit_test(traffic_class_cleanup_rx)
			 );

	ztest_run_test_suite(net_traffic_class_test);
}