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>
33 static pthread_mutex_t file_mtx = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
34 int log_lvl = PROX_MAX_LOG_LVL;
35 static uint64_t tsc_off;
37 static int n_warnings = 0;
38 char last_warn[5][1024];
39 int get_n_warnings(void)
41 #if PROX_MAX_LOG_LVL < PROX_LOG_WARN
47 const char *get_warning(int i)
49 #if PROX_MAX_LOG_LVL < PROX_LOG_WARN
54 return last_warn[(n_warnings - 1 + i + 5) % 5];
57 static void store_warning(const char *warning)
59 strncpy(last_warn[n_warnings % 5], warning, sizeof(last_warn[0]));
63 void plog_init(const char *log_name, int log_name_pid)
70 snprintf(buf, sizeof(buf), "%s-%u.log", "prox", getpid());
72 strncpy(buf, "prox.log", sizeof(buf));
75 strncpy(buf, log_name, sizeof(buf));
80 tsc_off = rte_rdtsc() + 2500000000;
83 int plog_set_lvl(int lvl)
85 if (lvl <= PROX_MAX_LOG_LVL) {
93 static void file_lock(void)
95 pthread_mutex_lock(&file_mtx);
98 static void file_unlock(void)
100 pthread_mutex_unlock(&file_mtx);
103 void file_print(const char *str)
112 static void plog_buf(const char* buf)
114 if (prox_cfg.logbuf) {
116 if (prox_cfg.logbuf_pos + strlen(buf) + 1 < prox_cfg.logbuf_size) {
117 memcpy(prox_cfg.logbuf + prox_cfg.logbuf_pos, buf, strlen(buf));
118 prox_cfg.logbuf_pos += strlen(buf);
126 /* ncurses never initialized */
133 static const char* lvl_to_str(int lvl, int always)
136 case PROX_LOG_ERR: return "error ";
137 case PROX_LOG_WARN: return "warn ";
138 case PROX_LOG_INFO: return always? "info " : "";
139 case PROX_LOG_DBG: return "debug ";
144 static int dump_pkt(char *dst, size_t dst_size, const struct rte_mbuf *mbuf)
146 const struct ether_hdr *peth = rte_pktmbuf_mtod(mbuf, const struct ether_hdr *);
147 const struct ipv4_hdr *dpip = (const struct ipv4_hdr *)(peth + 1);
148 const uint8_t *pkt_bytes = (const uint8_t *)peth;
149 const uint16_t len = rte_pktmbuf_pkt_len(mbuf);
152 if (peth->ether_type == ETYPE_IPv4)
153 str_len = snprintf(dst, dst_size, "pkt_len=%u, Eth=%x, Proto=%#06x",
154 len, peth->ether_type, dpip->next_proto_id);
156 str_len = snprintf(dst, dst_size, "pkt_len=%u, Eth=%x",
157 len, peth->ether_type);
159 for (uint16_t i = 0; i < len && i < DUMP_PKT_LEN && str_len < dst_size; ++i) {
161 str_len += snprintf(dst + str_len, dst_size - str_len, "\n%04x ", i);
163 else if (i % 8 == 0) {
164 str_len += snprintf(dst + str_len, dst_size - str_len, " ");
166 str_len += snprintf(dst + str_len, dst_size - str_len, "%02x ", pkt_bytes[i]);
168 if (str_len < dst_size)
169 snprintf(dst + str_len, dst_size - str_len, "\n");
173 static int vplog(int lvl, const char *format, va_list ap, const struct rte_mbuf *mbuf, int extended)
176 uint64_t hz, rtime_tsc, rtime_sec, rtime_usec;
182 if (format == NULL && mbuf == NULL)
187 hz = rte_get_tsc_hz();
188 rtime_tsc = rte_rdtsc() - tsc_off;
189 rtime_sec = rtime_tsc / hz;
190 rtime_usec = (rtime_tsc - rtime_sec * hz) / (hz / 1000000);
191 ret += snprintf(buf, sizeof(buf) - ret, "%2"PRIu64".%06"PRIu64" C%u %s%s",
192 rtime_sec, rtime_usec, rte_lcore_id(), lvl_to_str(lvl, 1), format? " " : "");
195 ret += snprintf(buf, sizeof(buf) - ret, "%s%s", lvl_to_str(lvl, 0), format? " " : "");
200 ret += vsnprintf(buf + ret, sizeof(buf) - ret, format, ap);
205 ret += dump_pkt(buf + ret, sizeof(buf) - ret, mbuf);
209 if (lvl == PROX_LOG_WARN) {
215 #if PROX_MAX_LOG_LVL >= PROX_LOG_INFO
216 int plog_info(const char *fmt, ...)
222 ret = vplog(PROX_LOG_INFO, fmt, ap, NULL, 0);
227 int plogx_info(const char *fmt, ...)
233 ret = vplog(PROX_LOG_INFO, fmt, ap, NULL, 1);
238 int plogd_info(const struct rte_mbuf *mbuf, const char *fmt, ...)
244 ret = vplog(PROX_LOG_INFO, fmt, ap, mbuf, 0);
249 int plogdx_info(const struct rte_mbuf *mbuf, const char *fmt, ...)
255 ret = vplog(PROX_LOG_INFO, fmt, ap, mbuf, 1);
261 #if PROX_MAX_LOG_LVL >= PROX_LOG_ERR
262 int plog_err(const char *fmt, ...)
268 ret = vplog(PROX_LOG_ERR, fmt, ap, NULL, 0);
273 int plogx_err(const char *fmt, ...)
279 ret = vplog(PROX_LOG_ERR, fmt, ap, NULL, 1);
284 int plogd_err(const struct rte_mbuf *mbuf, const char *fmt, ...)
290 ret = vplog(PROX_LOG_ERR, fmt, ap, mbuf, 1);
295 int plogdx_err(const struct rte_mbuf *mbuf, const char *fmt, ...)
301 ret = vplog(PROX_LOG_ERR, fmt, ap, mbuf, 1);
308 #if PROX_MAX_LOG_LVL >= PROX_LOG_WARN
309 int plog_warn(const char *fmt, ...)
315 ret = vplog(PROX_LOG_WARN, fmt, ap, NULL, 0);
320 int plogx_warn(const char *fmt, ...)
326 ret = vplog(PROX_LOG_WARN, fmt, ap, NULL, 1);
331 int plogd_warn(const struct rte_mbuf *mbuf, const char *fmt, ...)
337 ret = vplog(PROX_LOG_WARN, fmt, ap, mbuf, 0);
342 int plogdx_warn(const struct rte_mbuf *mbuf, const char *fmt, ...)
348 ret = vplog(PROX_LOG_WARN, fmt, ap, mbuf, 1);
354 #if PROX_MAX_LOG_LVL >= PROX_LOG_DBG
355 int plog_dbg(const char *fmt, ...)
361 ret = vplog(PROX_LOG_DBG, fmt, ap, NULL, 0);
366 int plogx_dbg(const char *fmt, ...)
372 ret = vplog(PROX_LOG_DBG, fmt, ap, NULL, 1);
377 int plogd_dbg(const struct rte_mbuf *mbuf, const char *fmt, ...)
383 ret = vplog(PROX_LOG_DBG, fmt, ap, mbuf, 0);
388 int plogdx_dbg(const struct rte_mbuf *mbuf, const char *fmt, ...)
394 ret = vplog(PROX_LOG_DBG, fmt, ap, mbuf, 1);