Use thread local storage for thread index
[vpp.git] / src / plugins / dpdk / device / dpdk_priv.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #define rte_mbuf_from_vlib_buffer(x) (((struct rte_mbuf *)x) - 1)
17 #define vlib_buffer_from_rte_mbuf(x) ((vlib_buffer_t *)(x+1))
18
19 #define DPDK_NB_RX_DESC_DEFAULT   1024
20 #define DPDK_NB_TX_DESC_DEFAULT   1024
21 #define DPDK_NB_RX_DESC_VIRTIO    256
22 #define DPDK_NB_TX_DESC_VIRTIO    256
23
24 #define I40E_DEV_ID_SFP_XL710           0x1572
25 #define I40E_DEV_ID_QSFP_A              0x1583
26 #define I40E_DEV_ID_QSFP_B              0x1584
27 #define I40E_DEV_ID_QSFP_C              0x1585
28 #define I40E_DEV_ID_10G_BASE_T          0x1586
29 #define I40E_DEV_ID_VF                  0x154C
30
31 /* These args appear by themselves */
32 #define foreach_eal_double_hyphen_predicate_arg \
33 _(no-shconf)                                    \
34 _(no-hpet)                                      \
35 _(no-huge)                                      \
36 _(vmware-tsc-map)
37
38 #define foreach_eal_single_hyphen_mandatory_arg \
39 _(coremask, c)                                  \
40 _(nchannels, n)                                 \
41
42 #define foreach_eal_single_hyphen_arg           \
43 _(blacklist, b)                                 \
44 _(mem-alloc-request, m)                         \
45 _(force-ranks, r)
46
47 /* These args are preceeded by "--" and followed by a single string */
48 #define foreach_eal_double_hyphen_arg           \
49 _(huge-dir)                                     \
50 _(proc-type)                                    \
51 _(file-prefix)                                  \
52 _(vdev)
53
54 static inline void
55 dpdk_get_xstats (dpdk_device_t * xd)
56 {
57   int len;
58   if ((len = rte_eth_xstats_get (xd->device_index, NULL, 0)) > 0)
59     {
60       vec_validate (xd->xstats, len - 1);
61       vec_validate (xd->last_cleared_xstats, len - 1);
62
63       len =
64         rte_eth_xstats_get (xd->device_index, xd->xstats,
65                             vec_len (xd->xstats));
66
67       ASSERT (vec_len (xd->xstats) == len);
68       ASSERT (vec_len (xd->last_cleared_xstats) == len);
69
70       _vec_len (xd->xstats) = len;
71       _vec_len (xd->last_cleared_xstats) = len;
72
73     }
74 }
75
76
77 static inline void
78 dpdk_update_counters (dpdk_device_t * xd, f64 now)
79 {
80   vlib_simple_counter_main_t *cm;
81   vnet_main_t *vnm = vnet_get_main ();
82   u32 thread_index = vlib_get_thread_index ();
83   u64 rxerrors, last_rxerrors;
84
85   /* only update counters for PMD interfaces */
86   if ((xd->flags & DPDK_DEVICE_FLAG_PMD) == 0)
87     return;
88
89   xd->time_last_stats_update = now ? now : xd->time_last_stats_update;
90   clib_memcpy (&xd->last_stats, &xd->stats, sizeof (xd->last_stats));
91   rte_eth_stats_get (xd->device_index, &xd->stats);
92
93   /* maybe bump interface rx no buffer counter */
94   if (PREDICT_FALSE (xd->stats.rx_nombuf != xd->last_stats.rx_nombuf))
95     {
96       cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
97                              VNET_INTERFACE_COUNTER_RX_NO_BUF);
98
99       vlib_increment_simple_counter (cm, thread_index, xd->vlib_sw_if_index,
100                                      xd->stats.rx_nombuf -
101                                      xd->last_stats.rx_nombuf);
102     }
103
104   /* missed pkt counter */
105   if (PREDICT_FALSE (xd->stats.imissed != xd->last_stats.imissed))
106     {
107       cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
108                              VNET_INTERFACE_COUNTER_RX_MISS);
109
110       vlib_increment_simple_counter (cm, thread_index, xd->vlib_sw_if_index,
111                                      xd->stats.imissed -
112                                      xd->last_stats.imissed);
113     }
114   rxerrors = xd->stats.ierrors;
115   last_rxerrors = xd->last_stats.ierrors;
116
117   if (PREDICT_FALSE (rxerrors != last_rxerrors))
118     {
119       cm = vec_elt_at_index (vnm->interface_main.sw_if_counters,
120                              VNET_INTERFACE_COUNTER_RX_ERROR);
121
122       vlib_increment_simple_counter (cm, thread_index, xd->vlib_sw_if_index,
123                                      rxerrors - last_rxerrors);
124     }
125
126   dpdk_get_xstats (xd);
127 }
128
129 /*
130  * fd.io coding-style-patch-verification: ON
131  *
132  * Local Variables:
133  * eval: (c-set-style "gnu")
134  * End:
135  */