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;
84 int plog_set_lvl(int lvl)
86 if (lvl <= PROX_MAX_LOG_LVL) {
94 static void file_lock(void)
96 pthread_mutex_lock(&file_mtx);
99 static void file_unlock(void)
101 pthread_mutex_unlock(&file_mtx);
104 void file_print(const char *str)
113 static void plog_buf(const char* buf)
115 if (prox_cfg.logbuf) {
117 if (prox_cfg.logbuf_pos + strlen(buf) + 1 < prox_cfg.logbuf_size) {
118 memcpy(prox_cfg.logbuf + prox_cfg.logbuf_pos, buf, strlen(buf));
119 prox_cfg.logbuf_pos += strlen(buf);
127 /* ncurses never initialized */
134 static const char* lvl_to_str(int lvl, int always)
137 case PROX_LOG_ERR: return "error ";
138 case PROX_LOG_WARN: return "warn ";
139 case PROX_LOG_INFO: return always? "info " : "";
140 case PROX_LOG_DBG: return "debug ";
145 static int dump_pkt(char *dst, size_t dst_size, const struct rte_mbuf *mbuf)
147 const struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, const struct ether_hdr *);
148 const struct ipv4_hdr *dpip = (const struct ipv4_hdr *)(peth + 1);
149 const uint8_t *pkt_bytes = (const uint8_t *)peth;
150 const uint16_t len = rte_pktmbuf_pkt_len(mbuf);
153 if (peth->ether_type == ETYPE_IPv4)
154 str_len = snprintf(dst, dst_size, "pkt_len=%u, Eth=%x, Proto=%#06x",
155 len, peth->ether_type, dpip->next_proto_id);
157 str_len = snprintf(dst, dst_size, "pkt_len=%u, Eth=%x",
158 len, peth->ether_type);
160 for (uint16_t i = 0; i < len && i < DUMP_PKT_LEN && str_len < dst_size; ++i) {
162 str_len += snprintf(dst + str_len, dst_size - str_len, "\n%04x ", i);
164 else if (i % 8 == 0) {
165 str_len += snprintf(dst + str_len, dst_size - str_len, " ");
167 str_len += snprintf(dst + str_len, dst_size - str_len, "%02x ", pkt_bytes[i]);
169 if (str_len < dst_size)
170 snprintf(dst + str_len, dst_size - str_len, "\n");
174 static int vplog(int lvl, const char *format, va_list ap, const struct rte_mbuf *mbuf, int extended)
177 uint64_t hz, rtime_tsc, rtime_sec, rtime_usec;
183 if (format == NULL && mbuf == NULL)
188 hz = rte_get_tsc_hz();
189 rtime_tsc = rte_rdtsc() - tsc_off;
190 rtime_sec = rtime_tsc / hz;
191 rtime_usec = (rtime_tsc - rtime_sec * hz) / (hz / 1000000);
192 ret += snprintf(buf, sizeof(buf) - ret, "%2"PRIu64".%06"PRIu64" C%u %s%s",
193 rtime_sec, rtime_usec, rte_lcore_id(), lvl_to_str(lvl, 1), format? " " : "");
196 ret += snprintf(buf, sizeof(buf) - ret, "%s%s", lvl_to_str(lvl, 0), format? " " : "");
201 ret += vsnprintf(buf + ret, sizeof(buf) - ret, format, ap);
206 ret += dump_pkt(buf + ret, sizeof(buf) - ret, mbuf);
210 if (lvl == PROX_LOG_WARN) {
216 #if PROX_MAX_LOG_LVL >= PROX_LOG_INFO
217 int plog_info(const char *fmt, ...)
223 ret = vplog(PROX_LOG_INFO, fmt, ap, NULL, 0);
228 int plogx_info(const char *fmt, ...)
234 ret = vplog(PROX_LOG_INFO, fmt, ap, NULL, 1);
239 int plogd_info(const struct rte_mbuf *mbuf, const char *fmt, ...)
245 ret = vplog(PROX_LOG_INFO, fmt, ap, mbuf, 0);
250 int plogdx_info(const struct rte_mbuf *mbuf, const char *fmt, ...)
256 ret = vplog(PROX_LOG_INFO, fmt, ap, mbuf, 1);
262 #if PROX_MAX_LOG_LVL >= PROX_LOG_ERR
263 int plog_err(const char *fmt, ...)
269 ret = vplog(PROX_LOG_ERR, fmt, ap, NULL, 0);
274 int plogx_err(const char *fmt, ...)
280 ret = vplog(PROX_LOG_ERR, fmt, ap, NULL, 1);
285 int plogd_err(const struct rte_mbuf *mbuf, const char *fmt, ...)
291 ret = vplog(PROX_LOG_ERR, fmt, ap, mbuf, 1);
296 int plogdx_err(const struct rte_mbuf *mbuf, const char *fmt, ...)
302 ret = vplog(PROX_LOG_ERR, fmt, ap, mbuf, 1);
309 #if PROX_MAX_LOG_LVL >= PROX_LOG_WARN
310 int plog_warn(const char *fmt, ...)
316 ret = vplog(PROX_LOG_WARN, fmt, ap, NULL, 0);
321 int plogx_warn(const char *fmt, ...)
327 ret = vplog(PROX_LOG_WARN, fmt, ap, NULL, 1);
332 int plogd_warn(const struct rte_mbuf *mbuf, const char *fmt, ...)
338 ret = vplog(PROX_LOG_WARN, fmt, ap, mbuf, 0);
343 int plogdx_warn(const struct rte_mbuf *mbuf, const char *fmt, ...)
349 ret = vplog(PROX_LOG_WARN, fmt, ap, mbuf, 1);
355 #if PROX_MAX_LOG_LVL >= PROX_LOG_DBG
356 int plog_dbg(const char *fmt, ...)
362 ret = vplog(PROX_LOG_DBG, fmt, ap, NULL, 0);
367 int plogx_dbg(const char *fmt, ...)
373 ret = vplog(PROX_LOG_DBG, fmt, ap, NULL, 1);
378 int plogd_dbg(const struct rte_mbuf *mbuf, const char *fmt, ...)
384 ret = vplog(PROX_LOG_DBG, fmt, ap, mbuf, 0);
389 int plogdx_dbg(const struct rte_mbuf *mbuf, const char *fmt, ...)
395 ret = vplog(PROX_LOG_DBG, fmt, ap, mbuf, 1);