aboutsummaryrefslogtreecommitdiffstats
path: root/vpp_patches/common/0001-session-pinning.patch
blob: 22faa0b73288434e466d41e69221e9be389fa111 (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
From 310c46798bdfe655ea35eabec57215568b3632ec Mon Sep 17 00:00:00 2001
From: SunGuoao <guoao.sun@intel.com>
Date: Thu, 17 Oct 2019 00:16:31 +0800
Subject: [PATCH] session: pinning

Change-Id: Ic3600bc293bdae7ff62611da8d1241afccdac02a
---
 src/vnet/session/application.c | 83 ++++++++++++++++++++++++++++++++--
 src/vnet/session/application.h | 13 ++++++
 2 files changed, 92 insertions(+), 4 deletions(-)

diff --git a/src/vnet/session/application.c b/src/vnet/session/application.c
index 396470ae6..869fec45c 100644
--- a/src/vnet/session/application.c
+++ b/src/vnet/session/application.c
@@ -32,13 +32,19 @@ static app_listener_t *
 app_listener_alloc (application_t * app)
 {
   app_listener_t *app_listener;
+  vpp_app_worker_map_t *current_thread_map;
   pool_get (app->listeners, app_listener);
+  pool_get (app->app_worker_thread_map, current_thread_map);
   clib_memset (app_listener, 0, sizeof (*app_listener));
+  clib_memset (current_thread_map, 0, sizeof (*current_thread_map));
+  current_thread_map->map_index = current_thread_map - app->app_worker_thread_map;
   app_listener->al_index = app_listener - app->listeners;
   app_listener->app_index = app->app_index;
   app_listener->session_index = SESSION_INVALID_INDEX;
   app_listener->local_index = SESSION_INVALID_INDEX;
   app_listener->ls_handle = SESSION_INVALID_HANDLE;
+  app_listener->app_worker_thread_map = app->app_worker_thread_map;
+  app_listener->all_selected = 0;
   return app_listener;
 }
 
@@ -253,18 +259,87 @@ app_listener_cleanup (app_listener_t * al)
   app_listener_free (app, al);
 }
 
+static void
+app_listener_select_worker_add_worker (application_t * app, u32 wrk_index)
+{
+  u32 current_thread = vlib_get_thread_index ();
+  u32 *added_wrk_index;
+  vpp_app_worker_map_t *current_thread_map;
+
+  while (pool_is_free_index (app->app_worker_thread_map, current_thread))
+    {
+      vpp_app_worker_map_t *the_thread_map;
+
+      pool_get (app->app_worker_thread_map, the_thread_map);
+      clib_memset (the_thread_map, 0, sizeof (*the_thread_map));
+      the_thread_map->map_index = the_thread_map - app->app_worker_thread_map;
+    }
+  current_thread_map =
+    pool_elt_at_index (app->app_worker_thread_map, current_thread);
+  pool_get (current_thread_map->app_worker_map, added_wrk_index);
+  *added_wrk_index = wrk_index;
+  current_thread_map->total_workers += 1;
+}
+
+static u32
+app_listener_select_worker_interval (application_t * app)
+{
+  u32 current_thread = vlib_get_thread_index ();
+  u32 wrk_index;
+  vpp_app_worker_map_t *current_map;
+
+  if (PREDICT_FALSE(pool_is_free_index (app->app_worker_thread_map, current_thread)))
+    return 0;
+
+  current_map =
+    pool_elt_at_index (app->app_worker_thread_map, current_thread);
+
+  /** If the number of app worker(s) is smaller than vpp worker/thread(s) */
+  if (PREDICT_FALSE (current_map->total_workers == 0))
+    return 0;
+
+  current_map->last_accept_wrk += 1;
+  if (current_map->last_accept_wrk == current_map->total_workers)
+    current_map->last_accept_wrk = 0;
+
+  wrk_index = current_map->app_worker_map[current_map->last_accept_wrk];
+  return wrk_index;
+}
+
 static app_worker_t *
 app_listener_select_worker (application_t * app, app_listener_t * al)
 {
   u32 wrk_index;
 
   app = application_get (al->app_index);
-  wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
-  if (wrk_index == ~0)
-    wrk_index = clib_bitmap_first_set (al->workers);
+  if (PREDICT_FALSE (al->all_selected == 0))
+    {
+      wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
+      if (wrk_index == ~0)
+       {
+         al->all_selected = 1;
+         wrk_index = app_listener_select_worker_interval (app);
+         if (wrk_index == 0)
+           wrk_index = clib_bitmap_first_set (al->workers);
+       }
+      else
+       {
+         app_listener_select_worker_add_worker (app, wrk_index);
+         al->accept_rotor = wrk_index;
+       }
+    }
+  else
+    {
+      wrk_index = app_listener_select_worker_interval (app);
+      if (PREDICT_FALSE (wrk_index == 0)){
+         wrk_index = clib_bitmap_next_set (al->workers, al->accept_rotor + 1);
+         if (wrk_index == ~0)
+          wrk_index = clib_bitmap_first_set (al->workers);
+        al->accept_rotor = wrk_index;
+        }
+   }
 
   ASSERT (wrk_index != ~0);
-  al->accept_rotor = wrk_index;
   return application_get_worker (app, wrk_index);
 }
 
diff --git a/src/vnet/session/application.h b/src/vnet/session/application.h
index a853c3cb7..5da3ce84a 100644
--- a/src/vnet/session/application.h
+++ b/src/vnet/session/application.h
@@ -70,6 +70,14 @@ typedef struct app_worker_map_
   u32 wrk_index;
 } app_worker_map_t;
 
+typedef struct vpp_app_worker_map_
+{
+  u32 map_index;
+  u32 *app_worker_map;         /** app worker(s) pinned to the thread */
+  u32 last_accept_wrk;         /** last selected app worker by the thread */
+  u32 total_workers;         /** total app worker(s) pinned to the thread */
+}vpp_app_worker_map_t;
+
 typedef struct app_listener_
 {
   clib_bitmap_t *workers;	/**< workers accepting connections */
@@ -81,6 +89,8 @@ typedef struct app_listener_
   session_handle_t ls_handle;	/**< session handle of the local or global
 				     listening session that also identifies
 				     the app listener */
+  u8 all_selected;
+  vpp_app_worker_map_t *app_worker_thread_map;   /** app worker map for the vpp thread */
 } app_listener_t;
 
 typedef struct application_
@@ -111,6 +121,9 @@ typedef struct application_
   /** Pool of listeners for the app */
   app_listener_t *listeners;
 
+  /** app worker(s) selected by this VPP thread */
+  vpp_app_worker_map_t *app_worker_thread_map;
+
   /** Preferred tls engine */
   u8 tls_engine;
 
-- 
2.17.1