xref: /unit/src/nxt_port_memory.c (revision 1488:6976d36be926)
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_process_t *process,
286     nxt_port_t *port, nxt_bool_t tracking, nxt_int_t n)
287 {
288     void                     *mem;
289     u_char                   *p, name[64];
290     nxt_fd_t                 fd;
291     nxt_int_t                i;
292     nxt_free_map_t           *free_map;
293     nxt_port_mmap_t          *port_mmap;
294     nxt_port_mmap_header_t   *hdr;
295     nxt_port_mmap_handler_t  *mmap_handler;
296 
297     mmap_handler = nxt_zalloc(sizeof(nxt_port_mmap_handler_t));
298     if (nxt_slow_path(mmap_handler == NULL)) {
299         nxt_log(task, NXT_LOG_WARN, "failed to allocate mmap_handler");
300 
301         return NULL;
302     }
303 
304     port_mmap = nxt_port_mmap_at(&process->outgoing, process->outgoing.size);
305     if (nxt_slow_path(port_mmap == NULL)) {
306         nxt_log(task, NXT_LOG_WARN,
307                 "failed to add port mmap to outgoing array");
308 
309         nxt_free(mmap_handler);
310         return NULL;
311     }
312 
313     p = nxt_sprintf(name, name + sizeof(name), NXT_SHM_PREFIX "unit.%PI.%uxD",
314                     nxt_pid, nxt_random(&task->thread->random));
315     *p = '\0';
316 
317 #if (NXT_HAVE_MEMFD_CREATE)
318 
319     fd = syscall(SYS_memfd_create, name, MFD_CLOEXEC);
320 
321     if (nxt_slow_path(fd == -1)) {
322         nxt_alert(task, "memfd_create(%s) failed %E", name, nxt_errno);
323 
324         goto remove_fail;
325     }
326 
327     nxt_debug(task, "memfd_create(%s): %FD", name, fd);
328 
329 #elif (NXT_HAVE_SHM_OPEN_ANON)
330 
331     fd = shm_open(SHM_ANON, O_RDWR, S_IRUSR | S_IWUSR);
332 
333     nxt_debug(task, "shm_open(SHM_ANON): %FD", fd);
334 
335     if (nxt_slow_path(fd == -1)) {
336         nxt_alert(task, "shm_open(SHM_ANON) failed %E", nxt_errno);
337 
338         goto remove_fail;
339     }
340 
341 #elif (NXT_HAVE_SHM_OPEN)
342 
343     /* Just in case. */
344     shm_unlink((char *) name);
345 
346     fd = shm_open((char *) name, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
347 
348     nxt_debug(task, "shm_open(%s): %FD", name, fd);
349 
350     if (nxt_slow_path(fd == -1)) {
351         nxt_alert(task, "shm_open(%s) failed %E", name, nxt_errno);
352 
353         goto remove_fail;
354     }
355 
356     if (nxt_slow_path(shm_unlink((char *) name) == -1)) {
357         nxt_log(task, NXT_LOG_WARN, "shm_unlink(%s) failed %E", name,
358                 nxt_errno);
359     }
360 
361 #else
362 
363 #error No working shared memory implementation.
364 
365 #endif
366 
367     if (nxt_slow_path(ftruncate(fd, PORT_MMAP_SIZE) == -1)) {
368         nxt_log(task, NXT_LOG_WARN, "ftruncate() failed %E", nxt_errno);
369 
370         goto remove_fail;
371     }
372 
373     mem = nxt_mem_mmap(NULL, PORT_MMAP_SIZE, PROT_READ | PROT_WRITE,
374                        MAP_SHARED, fd, 0);
375 
376     if (nxt_slow_path(mem == MAP_FAILED)) {
377         goto remove_fail;
378     }
379 
380     mmap_handler->hdr = mem;
381     port_mmap->mmap_handler = mmap_handler;
382     nxt_port_mmap_handler_use(mmap_handler, 1);
383 
384     /* Init segment header. */
385     hdr = mmap_handler->hdr;
386 
387     nxt_memset(hdr->free_map, 0xFFU, sizeof(hdr->free_map));
388     nxt_memset(hdr->free_tracking_map, 0xFFU, sizeof(hdr->free_tracking_map));
389 
390     hdr->id = process->outgoing.size - 1;
391     hdr->src_pid = nxt_pid;
392     hdr->dst_pid = process->pid;
393     hdr->sent_over = port->id;
394 
395     /* Mark first chunk as busy */
396     free_map = tracking ? hdr->free_tracking_map : hdr->free_map;
397 
398     for (i = 0; i < n; i++) {
399         nxt_port_mmap_set_chunk_busy(free_map, i);
400     }
401 
402     /* Mark as busy chunk followed the last available chunk. */
403     nxt_port_mmap_set_chunk_busy(hdr->free_map, PORT_MMAP_CHUNK_COUNT);
404     nxt_port_mmap_set_chunk_busy(hdr->free_tracking_map, PORT_MMAP_CHUNK_COUNT);
405 
406     nxt_debug(task, "send mmap fd %FD to process %PI", fd, port->pid);
407 
408     /* TODO handle error */
409     (void) nxt_port_socket_write(task, port, NXT_PORT_MSG_MMAP, fd, 0, 0, NULL);
410 
411     nxt_log(task, NXT_LOG_DEBUG, "new mmap #%D created for %PI -> %PI",
412             hdr->id, nxt_pid, process->pid);
413 
414     return mmap_handler;
415 
416 remove_fail:
417 
418     nxt_free(mmap_handler);
419 
420     process->outgoing.size--;
421 
422     return NULL;
423 }
424 
425 
426 static nxt_port_mmap_handler_t *
427 nxt_port_mmap_get(nxt_task_t *task, nxt_port_t *port, nxt_chunk_id_t *c,
428     nxt_int_t n, nxt_bool_t tracking)
429 {
430     nxt_int_t                i, res, nchunks;
431     nxt_process_t            *process;
432     nxt_free_map_t           *free_map;
433     nxt_port_mmap_t          *port_mmap;
434     nxt_port_mmap_t          *end_port_mmap;
435     nxt_port_mmap_header_t   *hdr;
436     nxt_port_mmap_handler_t  *mmap_handler;
437 
438     process = port->process;
439     if (nxt_slow_path(process == NULL)) {
440         return NULL;
441     }
442 
443     nxt_thread_mutex_lock(&process->outgoing.mutex);
444 
445     end_port_mmap = process->outgoing.elts + process->outgoing.size;
446 
447     for (port_mmap = process->outgoing.elts;
448          port_mmap < end_port_mmap;
449          port_mmap++)
450     {
451         mmap_handler = port_mmap->mmap_handler;
452         hdr = mmap_handler->hdr;
453 
454         if (hdr->sent_over != 0xFFFFu && hdr->sent_over != port->id) {
455             continue;
456         }
457 
458         *c = 0;
459 
460         free_map = tracking ? hdr->free_tracking_map : hdr->free_map;
461 
462         while (nxt_port_mmap_get_free_chunk(free_map, c)) {
463             nchunks = 1;
464 
465             while (nchunks < n) {
466                 res = nxt_port_mmap_chk_set_chunk_busy(free_map, *c + nchunks);
467 
468                 if (res == 0) {
469                     for (i = 0; i < nchunks; i++) {
470                         nxt_port_mmap_set_chunk_free(free_map, *c + i);
471                     }
472 
473                     *c += nchunks + 1;
474                     nchunks = 0;
475                     break;
476                 }
477 
478                 nchunks++;
479             }
480 
481             if (nchunks == n) {
482                 goto unlock_return;
483             }
484         }
485 
486         hdr->oosm = 1;
487     }
488 
489     /* TODO introduce port_mmap limit and release wait. */
490 
491     *c = 0;
492     mmap_handler = nxt_port_new_port_mmap(task, process, port, tracking, n);
493 
494 unlock_return:
495 
496     nxt_thread_mutex_unlock(&process->outgoing.mutex);
497 
498     return mmap_handler;
499 }
500 
501 
502 static nxt_port_mmap_handler_t *
503 nxt_port_get_port_incoming_mmap(nxt_task_t *task, nxt_pid_t spid, uint32_t id)
504 {
505     nxt_process_t            *process;
506     nxt_port_mmap_handler_t  *mmap_handler;
507 
508     process = nxt_runtime_process_find(task->thread->runtime, spid);
509     if (nxt_slow_path(process == NULL)) {
510         return NULL;
511     }
512 
513     nxt_thread_mutex_lock(&process->incoming.mutex);
514 
515     if (nxt_fast_path(process->incoming.size > id)) {
516         mmap_handler = process->incoming.elts[id].mmap_handler;
517 
518     } else {
519         mmap_handler = NULL;
520 
521         nxt_debug(task, "invalid incoming mmap id %uD for pid %PI", id, spid);
522     }
523 
524     nxt_thread_mutex_unlock(&process->incoming.mutex);
525 
526     return mmap_handler;
527 }
528 
529 
530 nxt_int_t
531 nxt_port_mmap_get_tracking(nxt_task_t *task, nxt_port_t *port,
532     nxt_port_mmap_tracking_t *tracking, uint32_t stream)
533 {
534     nxt_chunk_id_t           c;
535     nxt_port_mmap_header_t   *hdr;
536     nxt_port_mmap_handler_t  *mmap_handler;
537 
538     nxt_debug(task, "request tracking for stream #%uD", stream);
539 
540     mmap_handler = nxt_port_mmap_get(task, port, &c, 1, 1);
541     if (nxt_slow_path(mmap_handler == NULL)) {
542         return NXT_ERROR;
543     }
544 
545     nxt_port_mmap_handler_use(mmap_handler, 1);
546 
547     hdr = mmap_handler->hdr;
548 
549     tracking->mmap_handler = mmap_handler;
550     tracking->tracking = hdr->tracking + c;
551 
552     *tracking->tracking = stream;
553 
554     nxt_debug(task, "outgoing tracking allocation: %PI->%PI,%d,%d",
555               hdr->src_pid, hdr->dst_pid, hdr->id, c);
556 
557     return NXT_OK;
558 }
559 
560 
561 nxt_bool_t
562 nxt_port_mmap_tracking_cancel(nxt_task_t *task,
563     nxt_port_mmap_tracking_t *tracking, uint32_t stream)
564 {
565     nxt_bool_t               res;
566     nxt_chunk_id_t           c;
567     nxt_port_mmap_header_t   *hdr;
568     nxt_port_mmap_handler_t  *mmap_handler;
569 
570     mmap_handler = tracking->mmap_handler;
571 
572     if (nxt_slow_path(mmap_handler == NULL)) {
573         return 0;
574     }
575 
576     hdr = mmap_handler->hdr;
577 
578     res = nxt_atomic_cmp_set(tracking->tracking, stream, 0);
579 
580     nxt_debug(task, "%s tracking for stream #%uD",
581               (res ? "cancelled" : "failed to cancel"), stream);
582 
583     if (!res) {
584         c = tracking->tracking - hdr->tracking;
585         nxt_port_mmap_set_chunk_free(hdr->free_tracking_map, c);
586     }
587 
588     nxt_port_mmap_handler_use(mmap_handler, -1);
589 
590     return res;
591 }
592 
593 
594 nxt_int_t
595 nxt_port_mmap_tracking_write(uint32_t *buf, nxt_port_mmap_tracking_t *t)
596 {
597     nxt_port_mmap_handler_t  *mmap_handler;
598 
599     mmap_handler = t->mmap_handler;
600 
601 #if (NXT_DEBUG)
602     {
603     nxt_atomic_t  *tracking;
604 
605     tracking = mmap_handler->hdr->tracking;
606 
607     nxt_assert(t->tracking >= tracking);
608     nxt_assert(t->tracking < tracking + PORT_MMAP_CHUNK_COUNT);
609     }
610 #endif
611 
612     buf[0] = mmap_handler->hdr->id;
613     buf[1] = t->tracking - mmap_handler->hdr->tracking;
614 
615     return NXT_OK;
616 }
617 
618 nxt_bool_t
619 nxt_port_mmap_tracking_read(nxt_task_t *task, nxt_port_recv_msg_t *msg)
620 {
621     nxt_buf_t                     *b;
622     nxt_bool_t                    res;
623     nxt_chunk_id_t                c;
624     nxt_port_mmap_header_t        *hdr;
625     nxt_port_mmap_handler_t       *mmap_handler;
626     nxt_port_mmap_tracking_msg_t  *tracking_msg;
627 
628     b = msg->buf;
629 
630     if (nxt_buf_used_size(b) < (int) sizeof(nxt_port_mmap_tracking_msg_t)) {
631         nxt_debug(task, "too small message %O", nxt_buf_used_size(b));
632         return 0;
633     }
634 
635     tracking_msg = (nxt_port_mmap_tracking_msg_t *) b->mem.pos;
636 
637     b->mem.pos += sizeof(nxt_port_mmap_tracking_msg_t);
638     mmap_handler = nxt_port_get_port_incoming_mmap(task, msg->port_msg.pid,
639                                                    tracking_msg->mmap_id);
640 
641     if (nxt_slow_path(mmap_handler == NULL)) {
642         return 0;
643     }
644 
645     hdr = mmap_handler->hdr;
646 
647     c = tracking_msg->tracking_id;
648     res = nxt_atomic_cmp_set(hdr->tracking + c, msg->port_msg.stream, 0);
649 
650     nxt_debug(task, "tracking for stream #%uD %s", msg->port_msg.stream,
651               (res ? "received" : "already cancelled"));
652 
653     if (!res) {
654         nxt_port_mmap_set_chunk_free(hdr->free_tracking_map, c);
655     }
656 
657     return res;
658 }
659 
660 
661 nxt_buf_t *
662 nxt_port_mmap_get_buf(nxt_task_t *task, nxt_port_t *port, size_t size)
663 {
664     nxt_mp_t                 *mp;
665     nxt_buf_t                *b;
666     nxt_int_t                nchunks;
667     nxt_chunk_id_t           c;
668     nxt_port_mmap_header_t   *hdr;
669     nxt_port_mmap_handler_t  *mmap_handler;
670 
671     nxt_debug(task, "request %z bytes shm buffer", size);
672 
673     nchunks = (size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE;
674 
675     if (nxt_slow_path(nchunks > PORT_MMAP_CHUNK_COUNT)) {
676         nxt_alert(task, "requested buffer (%z) too big", size);
677 
678         return NULL;
679     }
680 
681     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, 0);
682     if (nxt_slow_path(b == NULL)) {
683         return NULL;
684     }
685 
686     b->completion_handler = nxt_port_mmap_buf_completion;
687     nxt_buf_set_port_mmap(b);
688 
689     mmap_handler = nxt_port_mmap_get(task, port, &c, nchunks, 0);
690     if (nxt_slow_path(mmap_handler == NULL)) {
691         mp = task->thread->engine->mem_pool;
692         nxt_mp_free(mp, b);
693         nxt_mp_release(mp);
694         return NULL;
695     }
696 
697     b->parent = mmap_handler;
698 
699     nxt_port_mmap_handler_use(mmap_handler, 1);
700 
701     hdr = mmap_handler->hdr;
702 
703     b->mem.start = nxt_port_mmap_chunk_start(hdr, c);
704     b->mem.pos = b->mem.start;
705     b->mem.free = b->mem.start;
706     b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
707 
708     nxt_debug(task, "outgoing mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d",
709               b, b->mem.start, b->mem.end - b->mem.start,
710               hdr->src_pid, hdr->dst_pid, hdr->id, c);
711 
712     return b;
713 }
714 
715 
716 nxt_int_t
717 nxt_port_mmap_increase_buf(nxt_task_t *task, nxt_buf_t *b, size_t size,
718     size_t min_size)
719 {
720     size_t                   nchunks, free_size;
721     nxt_chunk_id_t           c, start;
722     nxt_port_mmap_header_t   *hdr;
723     nxt_port_mmap_handler_t  *mmap_handler;
724 
725     nxt_debug(task, "request increase %z bytes shm buffer", size);
726 
727     if (nxt_slow_path(nxt_buf_is_port_mmap(b) == 0)) {
728         nxt_log(task, NXT_LOG_WARN,
729                 "failed to increase, not a mmap buffer");
730         return NXT_ERROR;
731     }
732 
733     free_size = nxt_buf_mem_free_size(&b->mem);
734 
735     if (nxt_slow_path(size <= free_size)) {
736         return NXT_OK;
737     }
738 
739     mmap_handler = b->parent;
740     hdr = mmap_handler->hdr;
741 
742     start = nxt_port_mmap_chunk_id(hdr, b->mem.end);
743 
744     size -= free_size;
745 
746     nchunks = (size + PORT_MMAP_CHUNK_SIZE - 1) / PORT_MMAP_CHUNK_SIZE;
747 
748     c = start;
749 
750     /* Try to acquire as much chunks as required. */
751     while (nchunks > 0) {
752 
753         if (nxt_port_mmap_chk_set_chunk_busy(hdr->free_map, c) == 0) {
754             break;
755         }
756 
757         c++;
758         nchunks--;
759     }
760 
761     if (nchunks != 0
762         && min_size > free_size + PORT_MMAP_CHUNK_SIZE * (c - start))
763     {
764         c--;
765         while (c >= start) {
766             nxt_port_mmap_set_chunk_free(hdr->free_map, c);
767             c--;
768         }
769 
770         nxt_debug(task, "failed to increase, %uz chunks busy", nchunks);
771 
772         return NXT_ERROR;
773 
774     } else {
775         b->mem.end += PORT_MMAP_CHUNK_SIZE * (c - start);
776 
777         return NXT_OK;
778     }
779 }
780 
781 
782 static nxt_buf_t *
783 nxt_port_mmap_get_incoming_buf(nxt_task_t *task, nxt_port_t *port,
784     nxt_pid_t spid, nxt_port_mmap_msg_t *mmap_msg)
785 {
786     size_t                   nchunks;
787     nxt_buf_t                *b;
788     nxt_port_mmap_header_t   *hdr;
789     nxt_port_mmap_handler_t  *mmap_handler;
790 
791     mmap_handler = nxt_port_get_port_incoming_mmap(task, spid,
792                                                    mmap_msg->mmap_id);
793     if (nxt_slow_path(mmap_handler == NULL)) {
794         return NULL;
795     }
796 
797     b = nxt_buf_mem_ts_alloc(task, port->mem_pool, 0);
798     if (nxt_slow_path(b == NULL)) {
799         return NULL;
800     }
801 
802     b->completion_handler = nxt_port_mmap_buf_completion;
803 
804     nxt_buf_set_port_mmap(b);
805 
806     nchunks = mmap_msg->size / PORT_MMAP_CHUNK_SIZE;
807     if ((mmap_msg->size % PORT_MMAP_CHUNK_SIZE) != 0) {
808         nchunks++;
809     }
810 
811     hdr = mmap_handler->hdr;
812 
813     b->mem.start = nxt_port_mmap_chunk_start(hdr, mmap_msg->chunk_id);
814     b->mem.pos = b->mem.start;
815     b->mem.free = b->mem.start + mmap_msg->size;
816     b->mem.end = b->mem.start + nchunks * PORT_MMAP_CHUNK_SIZE;
817 
818     b->parent = mmap_handler;
819     nxt_port_mmap_handler_use(mmap_handler, 1);
820 
821     nxt_debug(task, "incoming mmap buf allocation: %p [%p,%uz] %PI->%PI,%d,%d",
822               b, b->mem.start, b->mem.end - b->mem.start,
823               hdr->src_pid, hdr->dst_pid, hdr->id, mmap_msg->chunk_id);
824 
825     return b;
826 }
827 
828 
829 void
830 nxt_port_mmap_write(nxt_task_t *task, nxt_port_t *port,
831     nxt_port_send_msg_t *msg, nxt_sendbuf_coalesce_t *sb, void *mmsg_buf)
832 {
833     size_t                   bsize;
834     nxt_buf_t                *bmem;
835     nxt_uint_t               i;
836     nxt_port_mmap_msg_t      *mmap_msg;
837     nxt_port_mmap_header_t   *hdr;
838     nxt_port_mmap_handler_t  *mmap_handler;
839 
840     nxt_debug(task, "prepare %z bytes message for transfer to process %PI "
841                     "via shared memory", sb->size, port->pid);
842 
843     bsize = sb->niov * sizeof(nxt_port_mmap_msg_t);
844     mmap_msg = mmsg_buf;
845 
846     bmem = msg->buf;
847 
848     for (i = 0; i < sb->niov; i++, mmap_msg++) {
849 
850         /* Lookup buffer which starts current iov_base. */
851         while (bmem && sb->iobuf[i].iov_base != bmem->mem.pos) {
852             bmem = bmem->next;
853         }
854 
855         if (nxt_slow_path(bmem == NULL)) {
856             nxt_log_error(NXT_LOG_ERR, task->log,
857                           "failed to find buf for iobuf[%d]", i);
858             return;
859             /* TODO clear b and exit */
860         }
861 
862         mmap_handler = bmem->parent;
863         hdr = mmap_handler->hdr;
864 
865         mmap_msg->mmap_id = hdr->id;
866         mmap_msg->chunk_id = nxt_port_mmap_chunk_id(hdr, bmem->mem.pos);
867         mmap_msg->size = sb->iobuf[i].iov_len;
868 
869         nxt_debug(task, "mmap_msg={%D, %D, %D} to %PI",
870                   mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
871                   port->pid);
872     }
873 
874     sb->iobuf[0].iov_base = mmsg_buf;
875     sb->iobuf[0].iov_len = bsize;
876     sb->niov = 1;
877     sb->size = bsize;
878 
879     msg->port_msg.mmap = 1;
880 }
881 
882 
883 void
884 nxt_port_mmap_read(nxt_task_t *task, nxt_port_recv_msg_t *msg)
885 {
886     nxt_buf_t            *b, **pb;
887     nxt_port_mmap_msg_t  *end, *mmap_msg;
888 
889     pb = &msg->buf;
890     msg->size = 0;
891 
892     for (b = msg->buf; b != NULL; b = b->next) {
893 
894         mmap_msg = (nxt_port_mmap_msg_t *) b->mem.pos;
895         end = (nxt_port_mmap_msg_t *) b->mem.free;
896 
897         while (mmap_msg < end) {
898             nxt_debug(task, "mmap_msg={%D, %D, %D} from %PI",
899                       mmap_msg->mmap_id, mmap_msg->chunk_id, mmap_msg->size,
900                       msg->port_msg.pid);
901 
902             *pb = nxt_port_mmap_get_incoming_buf(task, msg->port,
903                                                  msg->port_msg.pid, mmap_msg);
904             if (nxt_slow_path(*pb == NULL)) {
905                 nxt_log_error(NXT_LOG_ERR, task->log,
906                               "failed to get mmap buffer");
907 
908                 break;
909             }
910 
911             msg->size += mmap_msg->size;
912             pb = &(*pb)->next;
913             mmap_msg++;
914 
915             /* Mark original buf as complete. */
916             b->mem.pos += sizeof(nxt_port_mmap_msg_t);
917         }
918     }
919 }
920 
921 
922 nxt_port_method_t
923 nxt_port_mmap_get_method(nxt_task_t *task, nxt_port_t *port, nxt_buf_t *b)
924 {
925     nxt_port_method_t        m;
926     nxt_port_mmap_header_t   *hdr;
927     nxt_port_mmap_handler_t  *mmap_handler;
928 
929     m = NXT_PORT_METHOD_ANY;
930 
931     for (/* void */; b != NULL; b = b->next) {
932         if (nxt_buf_used_size(b) == 0) {
933             /* empty buffers does not affect method */
934             continue;
935         }
936 
937         if (nxt_buf_is_port_mmap(b)) {
938             mmap_handler = b->parent;
939             hdr = mmap_handler->hdr;
940 
941             if (m == NXT_PORT_METHOD_PLAIN) {
942                 nxt_log_error(NXT_LOG_ERR, task->log,
943                               "mixing plain and mmap buffers, "
944                               "using plain mode");
945 
946                 break;
947             }
948 
949             if (port->pid != hdr->dst_pid) {
950                 nxt_log_error(NXT_LOG_ERR, task->log,
951                               "send mmap buffer for %PI to %PI, "
952                               "using plain mode", hdr->dst_pid, port->pid);
953 
954                 m = NXT_PORT_METHOD_PLAIN;
955 
956                 break;
957             }
958 
959             if (m == NXT_PORT_METHOD_ANY) {
960                 nxt_debug(task, "using mmap mode");
961 
962                 m = NXT_PORT_METHOD_MMAP;
963             }
964         } else {
965             if (m == NXT_PORT_METHOD_MMAP) {
966                 nxt_log_error(NXT_LOG_ERR, task->log,
967                               "mixing mmap and plain buffers, "
968                               "switching to plain mode");
969 
970                 m = NXT_PORT_METHOD_PLAIN;
971 
972                 break;
973             }
974 
975             if (m == NXT_PORT_METHOD_ANY) {
976                 nxt_debug(task, "using plain mode");
977 
978                 m = NXT_PORT_METHOD_PLAIN;
979             }
980         }
981     }
982 
983     return m;
984 }
985