summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/handle_irq.c
blob: 00c192f67b36d3a8b179cb7eabe2cb9fa8c97e86 (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
/*
// Copyright (c) 2010-2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include <rte_cycles.h>

#include "lconf.h"
#include "task_base.h"
#include "task_init.h"
#include "handle_irq.h"
#include "stats_irq.h"
#include "log.h"
#include "unistd.h"
#include "input.h"

#define MAX_INTERRUPT_LENGTH	500000	/* Maximum length of an interrupt is (1 / MAX_INTERRUPT_LENGTH) seconds */
uint64_t irq_bucket_maxtime_micro[] = {1,5,10,50,100,500,1000,5000,10000,50000,100000,500000,UINT64_MAX};
/*
 *	This module is not handling any packets.
 *	It loops on rdtsc() and checks whether it has been interrupted
 *		 for more than (1 / MAX_INTERRUPT_LENGTH) sec.
 *	This is a debugging only task, useful to check if the system h
 *		as been properly configured.
*/

static void update_irq_stats(struct task_irq *task, uint64_t irq)
{
	if (irq > task->stats.max_irq)
		task->stats.max_irq = irq;
	for (uint i = 0; i < IRQ_BUCKETS_COUNT; ++i) {
		if (irq < irq_bucket_maxtime_cycles[i]) {
			task->stats.irq[i]++;
			break;
		}
	}
}

void task_irq_show_stats(struct task_irq *task_irq, struct input *input)
{
	struct irq_bucket *bucket = &task_irq->buffer[!task_irq->task_use_lt];
	if (input->reply) {
		char buf[8192] = {0};
		if (bucket->index == 0) {
			sprintf(buf, "\n");
			input->reply(input, buf, strlen(buf));
			buf[0] = 0;
		}
		for (uint64_t i = 0; i < bucket->index; i++) {
			sprintf(buf + strlen(buf), "%d; %"PRIu64"""; %ld; %ld; %ld; %ld ;",
				task_irq->lcore_id,
				i,
				bucket->info[i].lat,
				bucket->info[i].lat * 1000000 / rte_get_tsc_hz(),
				bucket->info[i].tsc - task_irq->start_tsc,
				(bucket->info[i].tsc - task_irq->start_tsc) * 1000 / rte_get_tsc_hz());
			sprintf(buf+strlen(buf), "\n");
			input->reply(input, buf, strlen(buf));
			buf[0] = 0;
		}
	} else {
		for (uint64_t i = 0; i < bucket->index; i++)
			if (bucket->info[i].lat)
				plog_info("[%d]; Interrupt %"PRIu64": %ld cycles (%ld micro-sec) at %ld cycles (%ld msec)\n",
					  task_irq->lcore_id,
					  i,
					  bucket->info[i].lat,
					  bucket->info[i].lat * 1000000 / rte_get_tsc_hz(),
					  bucket->info[i].tsc - task_irq->start_tsc,
					  (bucket->info[i].tsc - task_irq->start_tsc) * 1000 / rte_get_tsc_hz());
	}
	task_irq->stats_use_lt = !task_irq->task_use_lt;
	bucket->index = 0;
}

static void irq_stop(struct task_base *tbase)
{
	struct task_irq *task = (struct task_irq *)tbase;
	uint32_t i;
	uint32_t lcore_id = rte_lcore_id();
	uint64_t lat, max_lat = 0, tot_lat = 0;
	int bucket_id;
	int n_lat = 0;

	if (task->irq_debug) {
		plog_info("Stopping core %u\n", lcore_id);
		sleep(2);	// Make sure all cores are stopped before starting to write
		plog_info("Core ID; Interrupt (nanosec); Time (msec)\n");
		for (int j = 0; j < 2; j++) {
			// Start dumping the oldest bucket first
			if (task->buffer[0].info[0].tsc < task->buffer[1].info[0].tsc)
				bucket_id = j;
			else
				bucket_id = !j;
			struct irq_bucket *bucket = &task->buffer[bucket_id];
			for (i=0; i< bucket->index;i++) {
				if (bucket->info[i].lat != 0) {
					lat = bucket->info[i].lat * 1000000000 / rte_get_tsc_hz();
					if (max_lat < lat)
						max_lat = lat;
					n_lat++;
					tot_lat += lat;
					plog_info("%d; %ld; %ld\n", lcore_id, lat,
					  	(bucket->info[i].tsc - task->start_tsc) * 1000 / rte_get_tsc_hz());
				}
			}
		}
		if (n_lat)
			tot_lat = tot_lat / n_lat;
		plog_info("Core %u stopped. max lat is %ld and average is %ld\n", lcore_id, max_lat, tot_lat);
	}
}

static inline int handle_irq_bulk(struct task_base *tbase, struct rte_mbuf **mbufs, uint16_t n_pkts)
{
	struct task_irq *task = (struct task_irq *)tbase;
	uint64_t tsc1;
	uint64_t index;

	if (task->stats_use_lt != task->task_use_lt)
		task->task_use_lt = task->stats_use_lt;
	struct irq_bucket *bucket = &task->buffer[task->task_use_lt];

	tsc1 = rte_rdtsc();
	if ((tsc1 > task->first_tsc) && (task->tsc != 0)) {
		update_irq_stats(task, tsc1 - task->tsc);
		if (((tsc1 - task->tsc) > task->max_irq) && (bucket->index < MAX_INDEX)) {
			bucket->info[bucket->index].tsc = tsc1;
			bucket->info[bucket->index++].lat = tsc1 - task->tsc;
		}
	}
	task->tsc = tsc1;
	return 0;
}

static void init_task_irq(struct task_base *tbase,
			  __attribute__((unused)) struct task_args *targ)
{
	struct task_irq *task = (struct task_irq *)tbase;
	task->start_tsc = rte_rdtsc();
	task->first_tsc = task->start_tsc + 2 * rte_get_tsc_hz();
	task->lcore_id = targ->lconf->id;
	task->irq_debug = targ->irq_debug;
	// max_irq expressed in cycles
	task->max_irq = rte_get_tsc_hz() / MAX_INTERRUPT_LENGTH;
	plog_info("\tusing irq mode with max irq set to %ld cycles\n", task->max_irq);

	for (uint bucket_id = 0; bucket_id < IRQ_BUCKETS_COUNT - 1; bucket_id++)
		irq_bucket_maxtime_cycles[bucket_id] = rte_get_tsc_hz() * irq_bucket_maxtime_micro[bucket_id] / 1000000;
	irq_bucket_maxtime_cycles[IRQ_BUCKETS_COUNT - 1] = UINT64_MAX;
}

static struct task_init task_init_irq = {
	.mode_str = "irq",
	.init = init_task_irq,
	.handle = handle_irq_bulk,
	.stop = irq_stop,
	.flag_features = TASK_FEATURE_NO_RX,
	.size = sizeof(struct task_irq)
};

static struct task_init task_init_none;

__attribute__((constructor)) static void reg_task_irq(void)
{
	reg_task(&task_init_irq);
}
an class="k">struct rte_eth_xstats), prox_port_cfg[port_id].socket); PROX_PANIC(eth_xstats[port_id] == NULL, "Error allocating memory for xstats"); eth_xstat_names[port_id] = eth_xstats[port_id]; num_xstats[port_id] = rte_eth_xstats_get(port_id, eth_xstats[port_id], num_xstats[port_id]); #endif if (!strcmp(prox_port_cfg[port_id].short_name, "ixgbe")) { potential_ixgbe_warn = 1; xstat_tor_offset[port_id] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "rx_total_bytes"); xstat_tpr_offset[port_id] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "rx_total_packets"); } tx_pkt_size_offset[port_id][PKT_SIZE_64] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_64_packets"); tx_pkt_size_offset[port_id][PKT_SIZE_65] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_65_to_127_packets"); tx_pkt_size_offset[port_id][PKT_SIZE_128] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_128_to_255_packets"); tx_pkt_size_offset[port_id][PKT_SIZE_256] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_256_to_511_packets"); tx_pkt_size_offset[port_id][PKT_SIZE_512] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_512_to_1023_packets"); if (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe")) { tx_pkt_size_offset[port_id][PKT_SIZE_1024] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_1024_to_max_packets"); } else { tx_pkt_size_offset[port_id][PKT_SIZE_1024] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_1024_to_1522_packets"); tx_pkt_size_offset[port_id][PKT_SIZE_1522] = find_xstats_str(eth_xstat_names[port_id], num_xstats[port_id], "tx_size_1523_to_max_packets"); } plog_info("offset = %d, %d, %d, %d, %d, %d %d\n", tx_pkt_size_offset[port_id][PKT_SIZE_64], tx_pkt_size_offset[port_id][PKT_SIZE_65], tx_pkt_size_offset[port_id][PKT_SIZE_128], tx_pkt_size_offset[port_id][PKT_SIZE_256], tx_pkt_size_offset[port_id][PKT_SIZE_512], tx_pkt_size_offset[port_id][PKT_SIZE_1024], tx_pkt_size_offset[port_id][PKT_SIZE_1522]); #if RTE_VERSION >= RTE_VERSION_NUM(16,7,0,0) prox_free(eth_xstat_names[port_id]); #endif if (num_xstats[port_id] == 0 || eth_xstats[port_id] == NULL) { plog_warn("Failed to initialize xstat for port %d, running without xstats\n", port_id); num_xstats[port_id] = 0; } } } for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if ((xstat_tor_offset[port_id] != -1) && (xstat_tpr_offset[port_id] != -1)) { num_ixgbe_xstats = 2; // ixgbe PMD supports tor and tpr xstats break; } } if ((num_ixgbe_xstats == 0) && (potential_ixgbe_warn)) plog_warn("Failed to initialize ixgbe xstat, running without ixgbe xstats\n"); #endif } static void nic_read_stats(uint8_t port_id) { unsigned is_ixgbe = (0 == strcmp(prox_port_cfg[port_id].short_name, "ixgbe")); struct port_stats_sample *stats = &port_stats[port_id].sample[last_stat]; #if defined(PROX_STATS) && defined(PROX_HW_DIRECT_STATS) if (is_ixgbe) { struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat]; ixgbe_read_stats(port_id, stats, prev, last_stat); return; } #endif uint64_t before, after; struct rte_eth_stats eth_stat; before = rte_rdtsc(); rte_eth_stats_get(port_id, &eth_stat); after = rte_rdtsc(); stats->tsc = (before >> 1) + (after >> 1); stats->no_mbufs = eth_stat.rx_nombuf; stats->ierrors = eth_stat.ierrors; stats->imissed = eth_stat.imissed; stats->oerrors = eth_stat.oerrors; stats->rx_bytes = eth_stat.ibytes; /* The goal would be to get the total number of bytes received by the NIC (including overhead). Without the patch (i.e. num_ixgbe_xstats == 0) we can't do this directly with DPDK 2.1 API. So, we report the number of bytes (including overhead) received by the host. */ #if XSTATS_SUPPORT if (num_xstats[port_id]) { rte_eth_xstats_get(port_id, eth_xstats[port_id], num_xstats[port_id]); for (size_t i = 0; i < sizeof(tx_pkt_size_offset[0])/sizeof(tx_pkt_size_offset[0][0]); ++i) { if (tx_pkt_size_offset[port_id][i] != -1) stats->tx_pkt_size[i] = (eth_xstats[port_id][tx_pkt_size_offset[port_id][i]]).value; else stats->tx_pkt_size[i] = -1; } } else { for (size_t i = 0; i < sizeof(tx_pkt_size_offset[0])/sizeof(tx_pkt_size_offset[0][0]); ++i) { stats->tx_pkt_size[i] = -1; } } #endif if (is_ixgbe) { #if XSTATS_SUPPORT if (num_ixgbe_xstats) { stats->rx_tot = eth_xstats[port_id][xstat_tpr_offset[port_id]].value; stats->rx_bytes = eth_xstats[port_id][xstat_tor_offset[port_id]].value; } else #endif { stats->rx_tot = eth_stat.ipackets + eth_stat.ierrors + eth_stat.imissed; /* On ixgbe, the rx_bytes counts bytes received by Host without overhead. The rx_tot counts the number of packets received by the NIC. If we only add 20 * rx_tot to rx_bytes, the result will also take into account 20 * "number of packets dropped by the nic". Note that in case CRC is stripped on ixgbe, the CRC bytes are not counted. */ if (prox_port_cfg[port_id].port_conf.rxmode.hw_strip_crc == 1) stats->rx_bytes = eth_stat.ibytes + (24 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed)); else stats->rx_bytes = eth_stat.ibytes + (20 * eth_stat.ipackets - 20 * (eth_stat.ierrors + eth_stat.imissed)); } } else if (strcmp(prox_port_cfg[port_id].short_name, "i40e_vf") == 0) { // For I40E VF, imissed already part of received packets stats->rx_tot = eth_stat.ipackets; } else { stats->rx_tot = eth_stat.ipackets + eth_stat.imissed; } stats->tx_tot = eth_stat.opackets; stats->tx_bytes = eth_stat.obytes; } void stats_port_reset(void) { for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if (prox_port_cfg[port_id].active) { rte_eth_stats_reset(port_id); memset(&port_stats[port_id], 0, sizeof(struct port_stats)); } } } void stats_port_update(void) { for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if (prox_port_cfg[port_id].active) { nic_read_stats(port_id); } } } uint64_t stats_port_get_ierrors(void) { uint64_t ret = 0; for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if (prox_port_cfg[port_id].active) ret += port_stats[port_id].sample[last_stat].ierrors; } return ret; } uint64_t stats_port_get_imissed(void) { uint64_t ret = 0; for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if (prox_port_cfg[port_id].active) ret += port_stats[port_id].sample[last_stat].imissed; } return ret; } uint64_t stats_port_get_rx_packets(void) { uint64_t ret = 0; for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if (prox_port_cfg[port_id].active) ret += port_stats[port_id].sample[last_stat].rx_tot; } return ret; } uint64_t stats_port_get_tx_packets(void) { uint64_t ret = 0; for (uint8_t port_id = 0; port_id < nb_interface; ++port_id) { if (prox_port_cfg[port_id].active) ret += port_stats[port_id].sample[last_stat].tx_tot; } return ret; } int stats_get_n_ports(void) { return n_ports; } struct port_stats_sample *stats_get_port_stats_sample(uint32_t port_id, int l) { return &port_stats[port_id].sample[l == last_stat]; } int stats_port(uint8_t port_id, struct get_port_stats *gps) { if (!prox_port_cfg[port_id].active) return -1; struct port_stats_sample *last = &port_stats[port_id].sample[last_stat]; struct port_stats_sample *prev = &port_stats[port_id].sample[!last_stat]; gps->no_mbufs_diff = last->no_mbufs - prev->no_mbufs; gps->ierrors_diff = last->ierrors - prev->ierrors; gps->imissed_diff = last->imissed - prev->imissed; gps->rx_bytes_diff = last->rx_bytes - prev->rx_bytes; gps->tx_bytes_diff = last->tx_bytes - prev->tx_bytes; gps->rx_pkts_diff = last->rx_tot - prev->rx_tot; if (unlikely(prev->rx_tot > last->rx_tot)) gps->rx_pkts_diff = 0; gps->tx_pkts_diff = last->tx_tot - prev->tx_tot; if (unlikely(prev->tx_tot > last->tx_tot)) gps->rx_pkts_diff = 0; gps->rx_tot = last->rx_tot; gps->tx_tot = last->tx_tot; gps->no_mbufs_tot = last->no_mbufs; gps->ierrors_tot = last->ierrors; gps->imissed_tot = last->imissed; gps->last_tsc = last->tsc; gps->prev_tsc = prev->tsc; return 0; }