2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #include <sys/types.h>
21 #include <rte_cycles.h>
22 #include <rte_lcore.h>
23 #include <rte_ether.h>
32 #include "prox_compat.h"
34 static pthread_mutex_t file_mtx = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
35 int log_lvl = PROX_MAX_LOG_LVL;
36 static uint64_t tsc_off;
38 static int n_warnings = 0;
39 char last_warn[5][1024];
40 int get_n_warnings(void)
42 #if PROX_MAX_LOG_LVL < PROX_LOG_WARN
48 const char *get_warning(int i)
50 #if PROX_MAX_LOG_LVL < PROX_LOG_WARN
55 return last_warn[(n_warnings - 1 + i + 5) % 5];
58 static void store_warning(const char *warning)
60 prox_strncpy(last_warn[n_warnings % 5], warning, sizeof(last_warn[0]));
64 void plog_init(const char *log_name, int log_name_pid)
71 snprintf(buf, sizeof(buf), "%s-%u.log", "prox", getpid());
73 prox_strncpy(buf, "prox.log", sizeof(buf));
76 prox_strncpy(buf, log_name, sizeof(buf));
81 tsc_off = rte_rdtsc() + 2500000000;
91 int plog_set_lvl(int lvl)
93 if (lvl <= PROX_MAX_LOG_LVL) {
101 static void file_lock(void)
103 pthread_mutex_lock(&file_mtx);
106 static void file_unlock(void)
108 pthread_mutex_unlock(&file_mtx);
111 void file_print(const char *str)
120 static void plog_buf(const char* buf)
122 if (prox_cfg.logbuf) {
124 if (prox_cfg.logbuf_pos + strlen(buf) + 1 < prox_cfg.logbuf_size) {
125 memcpy(prox_cfg.logbuf + prox_cfg.logbuf_pos, buf, strlen(buf));
126 prox_cfg.logbuf_pos += strlen(buf);
134 /* ncurses never initialized */
141 static const char* lvl_to_str(int lvl, int always)
144 case PROX_LOG_ERR: return "error ";
145 case PROX_LOG_WARN: return "warn ";
146 case PROX_LOG_INFO: return always? "info " : "";
147 case PROX_LOG_DBG: return "debug ";
152 static int dump_pkt(char *dst, size_t dst_size, const struct rte_mbuf *mbuf)
154 const prox_rte_ether_hdr *peth = rte_pktmbuf_mtod(mbuf, const prox_rte_ether_hdr *);
155 const prox_rte_ipv4_hdr *dpip = (const prox_rte_ipv4_hdr *)(peth + 1);
156 const uint8_t *pkt_bytes = (const uint8_t *)peth;
157 const uint16_t len = rte_pktmbuf_pkt_len(mbuf);
160 if (peth->ether_type == ETYPE_IPv4)
161 str_len = snprintf(dst, dst_size, "pkt_len=%u, Eth=%x, Proto=%#06x",
162 len, peth->ether_type, dpip->next_proto_id);
164 str_len = snprintf(dst, dst_size, "pkt_len=%u, Eth=%x",
165 len, peth->ether_type);
167 for (uint16_t i = 0; i < len && i < DUMP_PKT_LEN && str_len < dst_size; ++i) {
169 str_len += snprintf(dst + str_len, dst_size - str_len, "\n%04x ", i);
171 else if (i % 8 == 0) {
172 str_len += snprintf(dst + str_len, dst_size - str_len, " ");
174 str_len += snprintf(dst + str_len, dst_size - str_len, "%02x ", pkt_bytes[i]);
176 if (str_len < dst_size)
177 snprintf(dst + str_len, dst_size - str_len, "\n");
181 static int vplog(int lvl, const char *format, va_list ap, const struct rte_mbuf *mbuf, int extended)
184 uint64_t hz, rtime_tsc, rtime_sec, rtime_usec;
190 if (format == NULL && mbuf == NULL)
195 hz = rte_get_tsc_hz();
196 rtime_tsc = rte_rdtsc() - tsc_off;
197 rtime_sec = rtime_tsc / hz;
198 rtime_usec = (rtime_tsc - rtime_sec * hz) / (hz / 1000000);
199 ret += snprintf(buf, sizeof(buf) - ret, "%2"PRIu64".%06"PRIu64" C%u %s%s",
200 rtime_sec, rtime_usec, rte_lcore_id(), lvl_to_str(lvl, 1), format? " " : "");
203 ret += snprintf(buf, sizeof(buf) - ret, "%s%s", lvl_to_str(lvl, 0), format? " " : "");
208 ret += vsnprintf(buf + ret, sizeof(buf) - ret, format, ap);
213 ret += dump_pkt(buf + ret, sizeof(buf) - ret, mbuf);
217 if (lvl == PROX_LOG_WARN) {
223 #if PROX_MAX_LOG_LVL >= PROX_LOG_INFO
224 int plog_info(const char *fmt, ...)
230 ret = vplog(PROX_LOG_INFO, fmt, ap, NULL, 0);
235 int plogx_info(const char *fmt, ...)
241 ret = vplog(PROX_LOG_INFO, fmt, ap, NULL, 1);
246 int plogd_info(const struct rte_mbuf *mbuf, const char *fmt, ...)
252 ret = vplog(PROX_LOG_INFO, fmt, ap, mbuf, 0);
257 int plogdx_info(const struct rte_mbuf *mbuf, const char *fmt, ...)
263 ret = vplog(PROX_LOG_INFO, fmt, ap, mbuf, 1);
269 #if PROX_MAX_LOG_LVL >= PROX_LOG_ERR
270 int plog_err(const char *fmt, ...)
276 ret = vplog(PROX_LOG_ERR, fmt, ap, NULL, 0);
281 int plogx_err(const char *fmt, ...)
287 ret = vplog(PROX_LOG_ERR, fmt, ap, NULL, 1);
292 int plogd_err(const struct rte_mbuf *mbuf, const char *fmt, ...)
298 ret = vplog(PROX_LOG_ERR, fmt, ap, mbuf, 1);
303 int plogdx_err(const struct rte_mbuf *mbuf, const char *fmt, ...)
309 ret = vplog(PROX_LOG_ERR, fmt, ap, mbuf, 1);
316 #if PROX_MAX_LOG_LVL >= PROX_LOG_WARN
317 int plog_warn(const char *fmt, ...)
323 ret = vplog(PROX_LOG_WARN, fmt, ap, NULL, 0);
328 int plogx_warn(const char *fmt, ...)
334 ret = vplog(PROX_LOG_WARN, fmt, ap, NULL, 1);
339 int plogd_warn(const struct rte_mbuf *mbuf, const char *fmt, ...)
345 ret = vplog(PROX_LOG_WARN, fmt, ap, mbuf, 0);
350 int plogdx_warn(const struct rte_mbuf *mbuf, const char *fmt, ...)
356 ret = vplog(PROX_LOG_WARN, fmt, ap, mbuf, 1);
362 #if PROX_MAX_LOG_LVL >= PROX_LOG_DBG
363 int plog_dbg(const char *fmt, ...)
369 ret = vplog(PROX_LOG_DBG, fmt, ap, NULL, 0);
374 int plogx_dbg(const char *fmt, ...)
380 ret = vplog(PROX_LOG_DBG, fmt, ap, NULL, 1);
385 int plogd_dbg(const struct rte_mbuf *mbuf, const char *fmt, ...)
391 ret = vplog(PROX_LOG_DBG, fmt, ap, mbuf, 0);
396 int plogdx_dbg(const struct rte_mbuf *mbuf, const char *fmt, ...)
402 ret = vplog(PROX_LOG_DBG, fmt, ap, mbuf, 1);