xref: /unit/src/nxt_port_memory.c (revision 1546:06017e6e3a5f)
1 
2 /*
3  * Copyright (C) Max Romanov
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 #if (NXT_HAVE_MEMFD_CREATE)
10 
11 #include <linux/memfd.h>
12 #include <unistd.h>
13 #include <sys/syscall.h>
14 
15 #endif
16 
17 #include <nxt_port_memory_int.h>
18 
19 
20 nxt_inline void
21 nxt_port_mmap_handler_use(nxt_port_mmap_handler_t *mmap_handler, int i)
22 {
23     int  c;
24 
25     c = nxt_atomic_fetch_add(&mmap_handler->use_count, i);
26 
27     if (i < 0 && c == -i) {
28         if (mmap_handler->hdr != NULL) {
29             nxt_mem_munmap(mmap_handler->hdr, PORT_MMAP_SIZE);
30             mmap_handler->hdr = NULL;
31         }
32 
33         nxt_free(mmap_handler);
34     }
35 }
36 
37 
38 static nxt_port_mmap_t *
39 nxt_port_mmap_at(nxt_port_mmaps_t *port_mmaps, uint32_t i)
40 {
41     uint32_t  cap;
42 
43     cap = port_mmaps->cap;
44 
45     if (cap == 0) {
46         cap = i + 1;
47     }
48 
49     while (i + 1 > cap) {
50 
51         if (cap < 16) {
52             cap = cap * 2;
53 
54         } else {
55             cap = cap + cap / 2;
56         }
57     }
58 
59     if (cap != port_mmaps->cap) {
60 
61         port_mmaps->elts = nxt_realloc(port_mmaps->elts,
62                                        cap * sizeof(nxt_port_mmap_t));
63         if (nxt_slow_path(port_mmaps->elts == NULL)) {
64             return NULL;
65         }
66 
67         nxt_memzero(port_mmaps->elts + port_mmaps->cap,
68                     sizeof(nxt_port_mmap_t) * (cap - port_mmaps->cap));
69 
70         port_mmaps->cap = cap;
71     }
72 
73     if (i + 1 > port_mmaps->size) {
74         port_mmaps->size = i + 1;
75     }
76 
77     return port_mmaps->elts + i;
78 }
79 
80 
81 void
82 nxt_port_mmaps_destroy(nxt_port_mmaps_t *port_mmaps, nxt_bool_t free_elts)
83 {
84     uint32_t         i;
85     nxt_port_mmap_t  *port_mmap;
86 
87     if (port_mmaps == NULL) {
88         return;
89     }
90 
91     port_mmap = port_mmaps->elts;
92 
93     for (i = 0; i < port_mmaps->size; i++) {
94         nxt_port_mmap_handler_use(port_mmap[i].mmap_handler, -1);
95     }
96 
97     port_mmaps->size = 0;
98 
99     if (free_elts != 0) {
100         nxt_free(port_mmaps->elts);
101     }
102 }
103 
104 
105 #define nxt_port_mmap_free_junk(p, size)                                      \
106     memset((p), 0xA5, size)
107 
108 
109 static void
110 nxt_port_mmap_buf_completion(nxt_task_t *task, void *obj, void *data)
111 {
112     u_char                   *p;
113     nxt_mp_t                 *mp;
114     nxt_buf_t                *b, *next;
115     nxt_port_t               *port;
116     nxt_process_t            *process;
117     nxt_chunk_id_t           c;
118     nxt_port_mmap_header_t   *hdr;
119     nxt_port_mmap_handler_t  *mmap_handler;
120 
121     if (nxt_buf_ts_handle(task, obj, data)) {
122         return;
123     }
124 
125     b = obj;
126 
127     nxt_assert(data == b->parent);
128 
129     mmap_handler = data;
130 
131 complete_buf:
132 
133     hdr = mmap_handler->hdr;
134 
135     if (nxt_slow_path(hdr->src_pid != nxt_pid && hdr->dst_pid != nxt_pid)) {
136         nxt_debug(task, "mmap buf completion: mmap for other process pair "
137                   "%PI->%PI", hdr->src_pid, hdr->dst_pid);
138 
139         goto release_buf;
140     }
141 
142     if (b->is_port_mmap_sent && b->mem.pos > b->mem.start) {
143         /*
144          * Chunks until b->mem.pos has been sent to other side,
145          * let's release rest (if any).
146          */
147         p = b->mem.pos - 1;
148         c = nxt_port_mmap_chunk_id(hdr, p) + 1;
149         p = nxt_port_mmap_chunk_start(hdr, c);
150 
151     } else {
152         p = b->mem.start;
153         c = nxt_port_mmap_chunk_id(hdr, p);
154     }
155 
156     nxt_port_mmap_free_junk(p, b->mem.end - p);
157 
158     nxt_debug(task, "mmap buf completion: %p [%p,%uz] (sent=%d), "
159               "%PI->%PI,%d,%d", b, b->mem.start, b->mem.end - b->mem.start,
160               b->is_port_mmap_sent, hdr->src_pid, hdr->dst_pid, hdr->id, c);
161 
162     while (p < b->mem.end) {
163         nxt_port_mmap_set_chunk_free(hdr->free_map, c);
164 
165         p += PORT_MMAP_CHUNK_SIZE;
166         c++;
167     }
168 
169     if (hdr->dst_pid == nxt_pid
170         && nxt_atomic_cmp_set(&hdr->oosm, 1, 0))
171     {
172         process = nxt_runtime_process_find(task->thread->runtime, hdr->src_pid);
173 
174         if (process != NULL && !nxt_queue_is_empty(&process->ports)) {
175             port = nxt_process_port_first(process);
176 
177             if (port->type == NXT_PROCESS_APP) {
178                 (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_SHM_ACK,
179                                              -1, 0, 0, NULL);
180             }
181         }
182     }
183 
184 release_buf:
185 
186     nxt_port_mmap_handler_use(mmap_handler, -1);
187 
188     next = b->next;
189     mp = b->data;
190 
191     nxt_mp_free(mp, b);
192     nxt_mp_release(mp);
193 
194     if (next != NULL) {
195         b = next;
196         mmap_handler = b->parent;
197 
198         goto complete_buf;
199     }
200 }
201 
202 
203 nxt_port_mmap_handler_t *
204 nxt_port_incoming_port_mmap(nxt_task_t *task, nxt_process_t *process,
205     nxt_fd_t fd)
206 {
207     void                     *mem;
208     struct stat              mmap_stat;
209     nxt_port_mmap_t          *port_mmap;
210     nxt_port_mmap_header_t   *hdr;
211     nxt_port_mmap_handler_t  *mmap_handler;
212 
213     nxt_debug(task, "got new mmap fd #%FD from process %PI",
214               fd, process->pid);
215 
216     port_mmap = NULL;
217 
218     if (fstat(fd, &mmap_stat) == -1) {
219         nxt_log(task, NXT_LOG_WARN, "fstat(%FD) failed %E", fd, nxt_errno);
220 
221         return NULL;
222     }
223 
224     mem = nxt_mem_mmap(NULL, mmap_stat.st_size,
225                        PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
226 
227     if (nxt_slow_path(mem == MAP_FAILED)) {
228         nxt_log(task, NXT_LOG_WARN, "mmap() failed %E", nxt_errno);
229 
230         return NULL;
231     }
232 
233     hdr = mem;
234 
235     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
236     if (nxt_slow_path(mmap_handler == NULL)) {
237         nxt_log(task, NXT_LOG_WARN, "failed to allocate mmap_handler");
238 
239         nxt_mem_munmap(mem, PORT_MMAP_SIZE);
240 
241         return NULL;
242     }
243 
244     mmap_handler->hdr = hdr;
245 
246     if (nxt_slow_path(hdr->src_pid != process->pid
247                       || hdr->dst_pid != nxt_pid))
248     {
249         nxt_log(task, NXT_LOG_WARN, "unexpected pid in mmap header detected: "
250                 "%PI != %PI or %PI != %PI", hdr->src_pid, process->pid,
251                 hdr->dst_pid, nxt_pid);
252 
253         return NULL;
254     }
255 
256     nxt_thread_mutex_lock(&process->incoming.mutex);
257 
258     port_mmap = nxt_port_mmap_at(&process->incoming, hdr->id);
259     if (nxt_slow_path(port_mmap == NULL)) {
260         nxt_log(task, NXT_LOG_WARN, "failed to add mmap to incoming array");
261 
262         nxt_mem_munmap(mem, PORT_MMAP_SIZE);
263         hdr = NULL;
264 
265         nxt_free(mmap_handler);
266         mmap_handler = NULL;
267 
268         goto fail;
269     }
270 
271     port_mmap->mmap_handler = mmap_handler;
272     nxt_port_mmap_handler_use(mmap_handler, 1);
273 
274     hdr->sent_over = 0xFFFFu;
275 
276 fail:
277 
278     nxt_thread_mutex_unlock(&process->incoming.mutex);
279 
280     return mmap_handler;
281 }
282 
283 
284 static nxt_port_mmap_handler_t *
285 nxt_port_new_port_mmap(nxt_task_t *task, nxt_port_mmaps_t *mmaps,
286     nxt_bool_t tracking, nxt_int_t n)
287 {
288     void                     *mem;
289     nxt_fd_t                 fd;
290     nxt_int_t                i;
291     nxt_free_map_t           *free_map;
292     nxt_port_mmap_t          *port_mmap;
293     nxt_port_mmap_header_t   *hdr;
294     nxt_port_mmap_handler_t  *mmap_handler;
295 
296     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
297     if (nxt_slow_path(mmap_handler == NULL)) {
298         nxt_alert(task, "failed to allocate mmap_handler");
299 
300         return NULL;
301     }
302 
303     port_mmap = nxt_port_mmap_at(mmaps, mmaps->size);
304     if (nxt_slow_path(port_mmap == NULL)) {
305         nxt_alert(task, "failed to add port mmap to mmaps array");
306 
307         nxt_free(mmap_handler);
308         return NULL;
309     }
310 
311     fd = nxt_shm_open(task, PORT_MMAP_SIZE);
312     if (nxt_slow_path(fd == -1)) {
313         goto remove_fail;
314     }
315 
316     mem = nxt_mem_mmap(NULL, PORT_MMAP_SIZE, PROT_READ | PROT_WRITE,
317                        MAP_SHARED, fd, 0);
318 
319     if (nxt_slow_path(mem == MAP_FAILED)) {
320         goto remove_fail;
321     }
322 
323     mmap_handler->hdr = mem;
324     mmap_handler->fd = fd;
325     port_mmap->mmap_handler = mmap_handler;
326     nxt_port_mmap_handler_use(mmap_handler, 1);
327 
328     /* Init segment header. */
329     hdr = mmap_handler->hdr;
330 
331     nxt_memset(hdr->free_map, 0xFFU, sizeof(hdr->free_map));
332     nxt_memset(hdr->free_tracking_map, 0xFFU, sizeof(hdr->free_tracking_map));
333 
334     hdr->id = mmaps->size - 1;
335     hdr->src_pid = nxt_pid;
336     hdr->sent_over = 0xFFFFu;
337 
338     /* Mark first chunk as busy */
339     free_map = tracking ? hdr->free_tracking_map : hdr->free_map;
340 
341     for (i = 0; i < n; i++) {
342         nxt_port_mmap_set_chunk_busy(free_map, i);
343     }
344 
345     /* Mark as busy chunk followed the last available chunk. */
346     nxt_port_mmap_set_chunk_busy(hdr->free_map, PORT_MMAP_CHUNK_COUNT);
347     nxt_port_mmap_set_chunk_busy(hdr->free_tracking_map, PORT_MMAP_CHUNK_COUNT);
348 
349     nxt_log(task, NXT_LOG_DEBUG, "new mmap #%D created for %PI -> ...",
350             hdr->id, nxt_pid);
351 
352     return mmap_handler;
353 
354 remove_fail:
355 
356     nxt_free(mmap_handler);
357 
358     mmaps->size--;
359 
360     return NULL;
361 }
362 
363 
364 nxt_int_t
365 nxt_shm_open(nxt_task_t *task, size_t size)
366 {
367     nxt_fd_t  fd;
368 
369 #if (NXT_HAVE_MEMFD_CREATE || NXT_HAVE_SHM_OPEN)
370 
371     u_char    *p, name[64];
372 
373     p = nxt_sprintf(name, name + sizeof(name), NXT_SHM_PREFIX "unit.%PI.%uxD",
374                     nxt_pid, nxt_random(&task->thread->random));
375     *p = '\0';
376 
377 #endif
378 
379 #if (NXT_HAVE_MEMFD_CREATE)
380 
381     fd = syscall(SYS_memfd_create, name, MFD_CLOEXEC);
382 
383     if (nxt_slow_path(fd == -1)) {
384         nxt_alert(task, "memfd_create(%s) failed %E", name, nxt_errno);
385 
386         return -1;
387     }
388 
389     nxt_debug(task, "memfd_create(%s): %FD", name, fd);
390 
391 #elif (NXT_HAVE_SHM_OPEN_ANON)
392 
393     fd = shm_open(SHM_ANON, O_RDWR, S_IRUSR | S_IWUSR);
394 
395     if (nxt_slow_path(fd == -1)) {
396         nxt_alert(task, "shm_open(SHM_ANON) failed %E", nxt_errno);
397 
398         return -1;
399     }
400 
401     nxt_debug(task, "shm_open(SHM_ANON): %FD", fd);
402 
403 #elif (NXT_HAVE_SHM_OPEN)
404 
405     /* Just in case. */
406     shm_unlink((char *) name);
407 
408     fd = shm_open((char *) name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
409 
410     if (nxt_slow_path(fd == -1)) {
411         nxt_alert(task, "shm_open(%s) failed %E", name, nxt_errno);
412 
413         return -1;
414     }
415 
416     nxt_debug(task, "shm_open(%s): %FD", name, fd);
417 
418     if (nxt_slow_path(shm_unlink((char *) name) == -1)) {
419         nxt_log(task, NXT_LOG_WARN, "shm_unlink(%s) failed %E", name,
420                 nxt_errno);
421     }
422 
423 #else
424 
425 #error No working shared memory implementation.
426 
427 #endif
428 
429     if (nxt_slow_path(ftruncate(fd, size) == -1)) {
430         nxt_alert(task, "ftruncate() failed %E", nxt_errno);
431 
432         nxt_fd_close(fd);
433 
434         return -1;
435     }
436 
437     return fd;
438 }
439 
440 
441 static nxt_port_mmap_handler_t *
442 nxt_port_mmap_get(nxt_task_t *task, nxt_port_mmaps_t *mmaps, nxt_chunk_id_t *c,
443     nxt_int_t n, nxt_bool_t tracking)
444 {
445     nxt_int_t                i, res, nchunks;
446     nxt_free_map_t           *free_map;
447     nxt_port_mmap_t          *port_mmap;
448     nxt_port_mmap_t          *end_port_mmap;
449     nxt_port_mmap_header_t   *hdr;
450     nxt_port_mmap_handler_t  *mmap_handler;
451 
452     nxt_thread_mutex_lock(&mmaps->mutex);
453 
454     end_port_mmap = mmaps->elts + mmaps->size;
455 
456     for (port_mmap = mmaps->elts;
457          port_mmap < end_port_mmap;
458          port_mmap++)
459     {
460         mmap_handler = port_mmap->mmap_handler;
461         hdr = mmap_handler->hdr;
462 
463         if (hdr->sent_over != 0xFFFFu) {
464             continue;
465         }
466 
467         *c = 0;
468 
469         free_map = tracking ? hdr->free_tracking_map : hdr->free_map;
470 
471         while (nxt_port_mmap_get_free_chunk(free_map, c)) {
472             nchunks = 1;
473 
474             while (nchunks < n) {
475                 res = nxt_port_mmap_chk_set_chunk_busy(free_map, *c + nchunks);
476 
477                 if (res == 0) {
478                     for (i = 0; i < nchunks; i++) {
479                         nxt_port_mmap_set_chunk_free(free_map, *c + i);
480                     }
481 
482                     *c += nchunks + 1;
483                     nchunks = 0;
484                     break;
485                 }
486 
487                 nchunks++;
488             }
489 
490             if (nchunks == n) {
491                 goto unlock_return;
492             }
493         }
494 
495         hdr->oosm = 1;
496     }
497 
498     /* TODO introduce port_mmap limit and release wait. */
499 
500     *c = 0;
501     mmap_handler = nxt_port_new_port_mmap(task, mmaps, tracking, n);
502 
503 unlock_return:
504 
505     nxt_thread_mutex_unlock(&mmaps->mutex);
506 
507     return mmap_handler;
508 }
509 
510 
511 static nxt_port_mmap_handler_t *
512 nxt_port_get_port_incoming_mmap(nxt_task_t *task, nxt_pid_t spid, uint32_t id)
513 {
514     nxt_process_t            *process;
515     nxt_port_mmap_handler_t  *mmap_handler;
516 
517     process = nxt_runtime_process_find(task->thread->runtime, spid);
518     if (nxt_slow_path(process == NULL)) {
519         return NULL;
520     }
521 
522     nxt_thread_mutex_lock(&process->incoming.mutex);
523 
524     if (nxt_fast_path(process->incoming.size > id)) {
525         mmap_handler = process->incoming.elts[id].mmap_handler;
526 
527     } else {
528         mmap_handler = NULL;
529 
530         nxt_debug(task, "invalid incoming mmap id %uD for pid %PI", id, spid);
531     }
532 
533     nxt_thread_mutex_unlock(&process->incoming.mutex);
534 
535     return mmap_handler;
536 }
537 
538 
539 nxt_int_t
540 nxt_port_mmap_get_tracking(nxt_task_t *task, nxt_port_mmaps_t *mmaps,
541     nxt_port_mmap_tracking_t *tracking, uint32_t stream)
542 {
543     nxt_chunk_id_t           c;
544     nxt_port_mmap_header_t   *hdr;
545     nxt_port_mmap_handler_t  *mmap_handler;
546 
547     nxt_debug(task, "request tracking for stream #%uD", stream);
548 
549     mmap_handler = nxt_port_mmap_get(task, mmaps, &c, 1, 1);
550     if (nxt_slow_path(mmap_handler == NULL)) {
551         return NXT_ERROR;
552     }
553 
554     nxt_port_mmap_handler_use(mmap_handler, 1);
555 
556     hdr = mmap_handler->hdr;
557 
558     tracking->mmap_handler = mmap_handler;
559     tracking->tracking = hdr->tracking + c;
560 
561     *tracking->tracking = stream;
562 
563     nxt_debug(task, "outgoing tracking allocation: %PI->%PI,%d,%d",
564               hdr->src_pid, hdr->dst_pid, hdr->id, c);
565 
566     return NXT_OK;
567 }
568 
569 
570 nxt_bool_t
571 nxt_port_mmap_tracking_cancel(nxt_task_t *task,
572     nxt_port_mmap_tracking_t *tracking, uint32_t stream)
573 {
574     nxt_bool_t               res;
575     nxt_chunk_id_t           c;
576     nxt_port_mmap_header_t   *hdr;
577     nxt_port_mmap_handler_t  *mmap_handler;
578 
579     mmap_handler = tracking->mmap_handler;
580 
581     if (nxt_slow_path(mmap_handler == NULL)) {
582         return 0;
583     }
584 
585     hdr = mmap_handler->hdr;
586 
587     res = nxt_atomic_cmp_set(tracking->tracking, stream, 0);
588 
589     nxt_debug(task, "%s tracking for stream #%uD",
590               (res ? "cancelled" : "failed to cancel"), stream);
591 
592     if (!res) {
593         c = tracking->tracking - hdr->tracking;
594         nxt_port_mmap_set_chunk_free(hdr->free_tracking_map, c);
595     }
596 
597     nxt_port_mmap_handler_use(mmap_handler, -1);
598 
599     return res;
600 }
601 
602 
603 nxt_int_t
604 nxt_port_mmap_tracking_write(uint32_t *buf, nxt_port_mmap_tracking_t *t)
605 {
606     nxt_port_mmap_handler_t  *mmap_handler;
607 
608     mmap_handler = t->mmap_handler;
609 
610 #if (NXT_DEBUG)
611     {
612     nxt_atomic_t  *tracking;
613 
614     tracking = mmap_handler->hdr->tracking;
615 
616     nxt_assert(t->tracking >= tracking);
617     nxt_assert(t->tracking < tracking + PORT_MMAP_CHUNK_COUNT);
618     }
619 #endif
620 
621     buf[0] = mmap_handler->hdr->id;
622     buf[1] = t->tracking - mmap_handler->hdr->tracking;
623 
624     return NXT_OK;
625 }
626 
627 nxt_bool_t
628 nxt_port_mmap_tracking_read(nxt_task_t *task, nxt_port_recv_msg_t *msg)
629 {
630     nxt_buf_t                     *b;
631     nxt_bool_t                    res;
632     nxt_chunk_id_t                c;
633     nxt_port_mmap_header_t        *hdr;
634     nxt_port_mmap_handler_t       *mmap_handler;
635     nxt_port_mmap_tracking_msg_t  *tracking_msg;
636 
637     b = msg->buf;
638 
639     if (nxt_buf_used_size(b) < (int) sizeof(nxt_port_mmap_tracking_msg_t)) {
640         nxt_debug(task, "too small message %O", nxt_buf_used_size(b));
641         return 0;
642     }
643 
644     tracking_msg = (nxt_port_mmap_tracking_msg_t *) b->mem.pos;
645 
646     b->mem.pos += sizeof(nxt_port_mmap_tracking_msg_t);
647     mmap_handler = nxt_port_get_port_incoming_mmap(task, msg->port_msg.pid,
648                                                    tracking_msg->mmap_id);
649 
650     if (nxt_slow_path(mmap_handler == NULL)) {
651         return 0;
652     }
653 
654     hdr = mmap_handler->hdr;
655 
656     c = tracking_msg->tracking_id;
657     res = nxt_atomic_cmp_set(hdr->tracking + c, msg->port_msg.stream, 0);
658 
659     nxt_debug(task, "tracking for stream #%uD %s", msg->port_msg.stream,
660               (res ? "received" : "already cancelled"));
661 
662     if (!res) {
663         nxt_port_mmap_set_chunk_free(hdr->free_tracking_map, c);
664     }
665 
666     return res;
667 }
668 
669 
670 nxt_buf_t *
671 nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_mmaps_t *mmaps, size_t size)
672 {
673     nxt_mp_t                 *mp;
674     nxt_buf_t                *b;
675     nxt_int_t                nchunks;
676     nxt_chunk_id_t           c;
677     nxt_port_mmap_header_t   *hdr;
678     nxt_port_mmap_handler_t  *mmap_handler;
679 
680     nxt_debug(task, "request %z bytes shm buffer", size);
681 
682     nchunks = (size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE;
683 
684     if (nxt_slow_path(nchunks > PORT_MMAP_CHUNK_COUNT)) {
685         nxt_alert(task, "requested buffer (%z) too big", size);
686 
687         return NULL;
688     }
689 
690     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 0);
691     if (nxt_slow_path(b == NULL)) {
692         return NULL;
693     }
694 
695     b->completion_handler = nxt_port_mmap_buf_completion;
696     nxt_buf_set_port_mmap(b);
697 
698     mmap_handler = nxt_port_mmap_get(task, mmaps, &c, nchunks, 0);
699     if (nxt_slow_path(mmap_handler == NULL)) {
700         mp = task->thread->engine->mem_pool;
701         nxt_mp_free(mp, b);
702         nxt_mp_release(mp);
703         return NULL;
704     }
705 
706     b->parent = mmap_handler;
707 
708     nxt_port_mmap_handler_use(mmap_handler, 1);
709 
710     hdr = mmap_handler->hdr;
711 
712     b->mem.start = nxt_port_mmap_chunk_start(hdr, c);
713     b->mem.pos = b->mem.start;
714     b->mem.free = b->mem.start;
715     b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
716 
717     nxt_debug(task, "outgoing mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d",
718               b, b->mem.start, b->mem.end - b->mem.start,
719               hdr->src_pid, hdr->dst_pid, hdr->id, c);
720 
721     return b;
722 }
723 
724 
725 nxt_int_t
726 nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size,
727     size_t min_size)
728 {
729     size_t                   nchunks, free_size;
730     nxt_chunk_id_t           c, start;
731     nxt_port_mmap_header_t   *hdr;
732     nxt_port_mmap_handler_t  *mmap_handler;
733 
734     nxt_debug(task, "request increase %z bytes shm buffer", size);
735 
736     if (nxt_slow_path(nxt_buf_is_port_mmap(b) == 0)) {
737         nxt_log(task, NXT_LOG_WARN,
738                 "failed to increase, not a mmap buffer");
739         return NXT_ERROR;
740     }
741 
742     free_size = nxt_buf_mem_free_size(&b->mem);
743 
744     if (nxt_slow_path(size <= free_size)) {
745         return NXT_OK;
746     }
747 
748     mmap_handler = b->parent;
749     hdr = mmap_handler->hdr;
750 
751     start = nxt_port_mmap_chunk_id(hdr, b->mem.end);
752 
753     size -= free_size;
754 
755     nchunks = (size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE;
756 
757     c = start;
758 
759     /* Try to acquire as much chunks as required. */
760     while (nchunks > 0) {
761 
762         if (nxt_port_mmap_chk_set_chunk_busy(hdr->free_map, c) == 0) {
763             break;
764         }
765 
766         c++;
767         nchunks--;
768     }
769 
770     if (nchunks != 0
771         && min_size > free_size + PORT_MMAP_CHUNK_SIZE * (c - start))
772     {
773         c--;
774         while (c >= start) {
775             nxt_port_mmap_set_chunk_free(hdr->free_map, c);
776             c--;
777         }
778 
779         nxt_debug(task, "failed to increase, %uz chunks busy", nchunks);
780 
781         return NXT_ERROR;
782 
783     } else {
784         b->mem.end += PORT_MMAP_CHUNK_SIZE * (c - start);
785 
786         return NXT_OK;
787     }
788 }
789 
790 
791 static nxt_buf_t *
792 nxt_port_mmap_get_incoming_buf(nxt_task_t *task, nxt_port_t *port,
793     nxt_pid_t spid, nxt_port_mmap_msg_t *mmap_msg)
794 {
795     size_t                   nchunks;
796     nxt_buf_t                *b;
797     nxt_port_mmap_header_t   *hdr;
798     nxt_port_mmap_handler_t  *mmap_handler;
799 
800     mmap_handler = nxt_port_get_port_incoming_mmap(task, spid,
801                                                    mmap_msg->mmap_id);
802     if (nxt_slow_path(mmap_handler == NULL)) {
803         return NULL;
804     }
805 
806     b = nxt_buf_mem_ts_alloc(task, port->mem_pool, 0);
807     if (nxt_slow_path(b == NULL)) {
808         return NULL;
809     }
810 
811     b->completion_handler = nxt_port_mmap_buf_completion;
812 
813     nxt_buf_set_port_mmap(b);
814 
815     nchunks = mmap_msg->size / PORT_MMAP_CHUNK_SIZE;
816     if ((mmap_msg->size % PORT_MMAP_CHUNK_SIZE) != 0) {
817         nchunks++;
818     }
819 
820     hdr = mmap_handler->hdr;
821 
822     b->mem.start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id);
823     b->mem.pos = b->mem.start;
824     b->mem.free = b->mem.start + mmap_msg->size;
825     b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
826 
827     b->parent = mmap_handler;
828     nxt_port_mmap_handler_use(mmap_handler, 1);
829 
830     nxt_debug(task, "incoming mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d",
831               b, b->mem.start, b->mem.end - b->mem.start,
832               hdr->src_pid, hdr->dst_pid, hdr->id, mmap_msg->chunk_id);
833 
834     return b;
835 }
836 
837 
838 void
839 nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port,
840     nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb, void *mmsg_buf)
841 {
842     size_t                   bsize;
843     nxt_buf_t                *bmem;
844     nxt_uint_t               i;
845     nxt_port_mmap_msg_t      *mmap_msg;
846     nxt_port_mmap_header_t   *hdr;
847     nxt_port_mmap_handler_t  *mmap_handler;
848 
849     nxt_debug(task, "prepare %z bytes message for transfer to process %PI "
850                     "via shared memory", sb->size, port->pid);
851 
852     bsize = sb->niov * sizeof(nxt_port_mmap_msg_t);
853     mmap_msg = mmsg_buf;
854 
855     bmem = msg->buf;
856 
857     for (i = 0; i < sb->niov; i++, mmap_msg++) {
858 
859         /* Lookup buffer which starts current iov_base. */
860         while (bmem && sb->iobuf[i].iov_base != bmem->mem.pos) {
861             bmem = bmem->next;
862         }
863 
864         if (nxt_slow_path(bmem == NULL)) {
865             nxt_log_error(NXT_LOG_ERR, task->log,
866                           "failed to find buf for iobuf[%d]", i);
867             return;
868             /* TODO clear b and exit */
869         }
870 
871         mmap_handler = bmem->parent;
872         hdr = mmap_handler->hdr;
873 
874         mmap_msg->mmap_id = hdr->id;
875         mmap_msg->chunk_id = nxt_port_mmap_chunk_id(hdr, bmem->mem.pos);
876         mmap_msg->size = sb->iobuf[i].iov_len;
877 
878         nxt_debug(task, "mmap_msg={%D, %D, %D} to %PI",
879                   mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
880                   port->pid);
881     }
882 
883     sb->iobuf[0].iov_base = mmsg_buf;
884     sb->iobuf[0].iov_len = bsize;
885     sb->niov = 1;
886     sb->size = bsize;
887 
888     msg->port_msg.mmap = 1;
889 }
890 
891 
892 void
893 nxt_port_mmap_read(nxt_task_t *task, nxt_port_recv_msg_t *msg)
894 {
895     nxt_buf_t            *b, **pb;
896     nxt_port_mmap_msg_t  *end, *mmap_msg;
897 
898     pb = &msg->buf;
899     msg->size = 0;
900 
901     for (b = msg->buf; b != NULL; b = b->next) {
902 
903         mmap_msg = (nxt_port_mmap_msg_t *) b->mem.pos;
904         end = (nxt_port_mmap_msg_t *) b->mem.free;
905 
906         while (mmap_msg < end) {
907             nxt_debug(task, "mmap_msg={%D, %D, %D} from %PI",
908                       mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
909                       msg->port_msg.pid);
910 
911             *pb = nxt_port_mmap_get_incoming_buf(task, msg->port,
912                                                  msg->port_msg.pid, mmap_msg);
913             if (nxt_slow_path(*pb == NULL)) {
914                 nxt_log_error(NXT_LOG_ERR, task->log,
915                               "failed to get mmap buffer");
916 
917                 break;
918             }
919 
920             msg->size += mmap_msg->size;
921             pb = &(*pb)->next;
922             mmap_msg++;
923 
924             /* Mark original buf as complete. */
925             b->mem.pos += sizeof(nxt_port_mmap_msg_t);
926         }
927     }
928 }
929 
930 
931 nxt_port_method_t
932 nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b)
933 {
934     nxt_port_method_t  m;
935 
936     m = NXT_PORT_METHOD_ANY;
937 
938     for (/* void */; b != NULL; b = b->next) {
939         if (nxt_buf_used_size(b) == 0) {
940             /* empty buffers does not affect method */
941             continue;
942         }
943 
944         if (nxt_buf_is_port_mmap(b)) {
945             if (m == NXT_PORT_METHOD_PLAIN) {
946                 nxt_log_error(NXT_LOG_ERR, task->log,
947                               "mixing plain and mmap buffers, "
948                               "using plain mode");
949 
950                 break;
951             }
952 
953             if (m == NXT_PORT_METHOD_ANY) {
954                 nxt_debug(task, "using mmap mode");
955 
956                 m = NXT_PORT_METHOD_MMAP;
957             }
958         } else {
959             if (m == NXT_PORT_METHOD_MMAP) {
960                 nxt_log_error(NXT_LOG_ERR, task->log,
961                               "mixing mmap and plain buffers, "
962                               "switching to plain mode");
963 
964                 m = NXT_PORT_METHOD_PLAIN;
965 
966                 break;
967             }
968 
969             if (m == NXT_PORT_METHOD_ANY) {
970                 nxt_debug(task, "using plain mode");
971 
972                 m = NXT_PORT_METHOD_PLAIN;
973             }
974         }
975     }
976 
977     return m;
978 }
979