Lines Matching +full:auto +full:- +full:flow +full:- +full:control
1 // SPDX-License-Identifier: GPL-2.0-only
20 /* auto-bind range */
25 * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
30 * @confirm_rx: boolean; whether a resume-tx packet should be send in reply
47 * struct qrtr_hdr_v2 - (I|R)PCrouter packet header later versions
113 * struct qrtr_node - endpoint node
137 * struct qrtr_tx_flow - tx flow control
169 struct qrtr_tx_flow *flow; in __qrtr_node_release() local
174 if (node->nid != QRTR_EP_NID_AUTO) in __qrtr_node_release()
175 radix_tree_delete(&qrtr_nodes, node->nid); in __qrtr_node_release()
178 list_del(&node->item); in __qrtr_node_release()
181 skb_queue_purge(&node->rx_queue); in __qrtr_node_release()
183 /* Free tx flow counters */ in __qrtr_node_release()
184 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) { in __qrtr_node_release()
185 flow = *slot; in __qrtr_node_release()
186 radix_tree_iter_delete(&node->qrtr_tx_flow, &iter, slot); in __qrtr_node_release()
187 kfree(flow); in __qrtr_node_release()
196 kref_get(&node->ref); in qrtr_node_acquire()
205 kref_put_mutex(&node->ref, __qrtr_node_release, &qrtr_node_lock); in qrtr_node_release()
209 * qrtr_tx_resume() - reset flow control counter
215 struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data; in qrtr_tx_resume()
216 u64 remote_node = le32_to_cpu(pkt->client.node); in qrtr_tx_resume()
217 u32 remote_port = le32_to_cpu(pkt->client.port); in qrtr_tx_resume()
218 struct qrtr_tx_flow *flow; in qrtr_tx_resume() local
224 flow = radix_tree_lookup(&node->qrtr_tx_flow, key); in qrtr_tx_resume()
226 if (flow) { in qrtr_tx_resume()
227 spin_lock(&flow->resume_tx.lock); in qrtr_tx_resume()
228 flow->pending = 0; in qrtr_tx_resume()
229 spin_unlock(&flow->resume_tx.lock); in qrtr_tx_resume()
230 wake_up_interruptible_all(&flow->resume_tx); in qrtr_tx_resume()
237 * qrtr_tx_wait() - flow control for outgoing packets
243 * The flow control scheme is based around the low and high "watermarks". When
245 * message, which will trigger the remote to send a control message of the type
255 struct qrtr_tx_flow *flow; in qrtr_tx_wait() local
259 /* Never set confirm_rx on non-data packets */ in qrtr_tx_wait()
263 mutex_lock(&node->qrtr_tx_lock); in qrtr_tx_wait()
264 flow = radix_tree_lookup(&node->qrtr_tx_flow, key); in qrtr_tx_wait()
265 if (!flow) { in qrtr_tx_wait()
266 flow = kzalloc(sizeof(*flow), GFP_KERNEL); in qrtr_tx_wait()
267 if (flow) { in qrtr_tx_wait()
268 init_waitqueue_head(&flow->resume_tx); in qrtr_tx_wait()
269 radix_tree_insert(&node->qrtr_tx_flow, key, flow); in qrtr_tx_wait()
272 mutex_unlock(&node->qrtr_tx_lock); in qrtr_tx_wait()
274 /* Set confirm_rx if we where unable to find and allocate a flow */ in qrtr_tx_wait()
275 if (!flow) in qrtr_tx_wait()
278 spin_lock_irq(&flow->resume_tx.lock); in qrtr_tx_wait()
279 ret = wait_event_interruptible_locked_irq(flow->resume_tx, in qrtr_tx_wait()
280 flow->pending < QRTR_TX_FLOW_HIGH || in qrtr_tx_wait()
281 flow->tx_failed || in qrtr_tx_wait()
282 !node->ep); in qrtr_tx_wait()
285 } else if (!node->ep) { in qrtr_tx_wait()
286 confirm_rx = -EPIPE; in qrtr_tx_wait()
287 } else if (flow->tx_failed) { in qrtr_tx_wait()
288 flow->tx_failed = 0; in qrtr_tx_wait()
291 flow->pending++; in qrtr_tx_wait()
292 confirm_rx = flow->pending == QRTR_TX_FLOW_LOW; in qrtr_tx_wait()
294 spin_unlock_irq(&flow->resume_tx.lock); in qrtr_tx_wait()
300 * qrtr_tx_flow_failed() - flag that tx of confirm_rx flagged messages failed
306 * flow's "pending" counter will keep incrementing towards QRTR_TX_FLOW_HIGH,
309 * Work around this by marking the flow as having a failed transmission and
316 struct qrtr_tx_flow *flow; in qrtr_tx_flow_failed() local
319 flow = radix_tree_lookup(&node->qrtr_tx_flow, key); in qrtr_tx_flow_failed()
321 if (flow) { in qrtr_tx_flow_failed()
322 spin_lock_irq(&flow->resume_tx.lock); in qrtr_tx_flow_failed()
323 flow->tx_failed = 1; in qrtr_tx_flow_failed()
324 spin_unlock_irq(&flow->resume_tx.lock); in qrtr_tx_flow_failed()
334 size_t len = skb->len; in qrtr_node_enqueue()
337 confirm_rx = qrtr_tx_wait(node, to->sq_node, to->sq_port, type); in qrtr_node_enqueue()
344 hdr->version = cpu_to_le32(QRTR_PROTO_VER_1); in qrtr_node_enqueue()
345 hdr->type = cpu_to_le32(type); in qrtr_node_enqueue()
346 hdr->src_node_id = cpu_to_le32(from->sq_node); in qrtr_node_enqueue()
347 hdr->src_port_id = cpu_to_le32(from->sq_port); in qrtr_node_enqueue()
348 if (to->sq_port == QRTR_PORT_CTRL) { in qrtr_node_enqueue()
349 hdr->dst_node_id = cpu_to_le32(node->nid); in qrtr_node_enqueue()
350 hdr->dst_port_id = cpu_to_le32(QRTR_NODE_BCAST); in qrtr_node_enqueue()
352 hdr->dst_node_id = cpu_to_le32(to->sq_node); in qrtr_node_enqueue()
353 hdr->dst_port_id = cpu_to_le32(to->sq_port); in qrtr_node_enqueue()
356 hdr->size = cpu_to_le32(len); in qrtr_node_enqueue()
357 hdr->confirm_rx = !!confirm_rx; in qrtr_node_enqueue()
362 mutex_lock(&node->ep_lock); in qrtr_node_enqueue()
363 rc = -ENODEV; in qrtr_node_enqueue()
364 if (node->ep) in qrtr_node_enqueue()
365 rc = node->ep->xmit(node->ep, skb); in qrtr_node_enqueue()
368 mutex_unlock(&node->ep_lock); in qrtr_node_enqueue()
373 qrtr_tx_flow_failed(node, to->sq_node, to->sq_port); in qrtr_node_enqueue()
404 if (node->nid != QRTR_EP_NID_AUTO || nid == QRTR_EP_NID_AUTO) in qrtr_node_assign()
409 node->nid = nid; in qrtr_node_assign()
414 * qrtr_endpoint_post() - post incoming data
423 struct qrtr_node *node = ep->node; in qrtr_endpoint_post()
434 return -EINVAL; in qrtr_endpoint_post()
438 return -ENOMEM; in qrtr_endpoint_post()
440 cb = (struct qrtr_cb *)skb->cb; in qrtr_endpoint_post()
452 cb->type = le32_to_cpu(v1->type); in qrtr_endpoint_post()
453 cb->src_node = le32_to_cpu(v1->src_node_id); in qrtr_endpoint_post()
454 cb->src_port = le32_to_cpu(v1->src_port_id); in qrtr_endpoint_post()
455 cb->confirm_rx = !!v1->confirm_rx; in qrtr_endpoint_post()
456 cb->dst_node = le32_to_cpu(v1->dst_node_id); in qrtr_endpoint_post()
457 cb->dst_port = le32_to_cpu(v1->dst_port_id); in qrtr_endpoint_post()
459 size = le32_to_cpu(v1->size); in qrtr_endpoint_post()
465 hdrlen = sizeof(*v2) + v2->optlen; in qrtr_endpoint_post()
467 cb->type = v2->type; in qrtr_endpoint_post()
468 cb->confirm_rx = !!(v2->flags & QRTR_FLAGS_CONFIRM_RX); in qrtr_endpoint_post()
469 cb->src_node = le16_to_cpu(v2->src_node_id); in qrtr_endpoint_post()
470 cb->src_port = le16_to_cpu(v2->src_port_id); in qrtr_endpoint_post()
471 cb->dst_node = le16_to_cpu(v2->dst_node_id); in qrtr_endpoint_post()
472 cb->dst_port = le16_to_cpu(v2->dst_port_id); in qrtr_endpoint_post()
474 if (cb->src_port == (u16)QRTR_PORT_CTRL) in qrtr_endpoint_post()
475 cb->src_port = QRTR_PORT_CTRL; in qrtr_endpoint_post()
476 if (cb->dst_port == (u16)QRTR_PORT_CTRL) in qrtr_endpoint_post()
477 cb->dst_port = QRTR_PORT_CTRL; in qrtr_endpoint_post()
479 size = le32_to_cpu(v2->size); in qrtr_endpoint_post()
489 if (cb->dst_port != QRTR_PORT_CTRL && cb->type != QRTR_TYPE_DATA && in qrtr_endpoint_post()
490 cb->type != QRTR_TYPE_RESUME_TX) in qrtr_endpoint_post()
495 qrtr_node_assign(node, cb->src_node); in qrtr_endpoint_post()
497 if (cb->type == QRTR_TYPE_RESUME_TX) { in qrtr_endpoint_post()
500 ipc = qrtr_port_lookup(cb->dst_port); in qrtr_endpoint_post()
504 if (sock_queue_rcv_skb(&ipc->sk, skb)) in qrtr_endpoint_post()
514 return -EINVAL; in qrtr_endpoint_post()
520 * qrtr_alloc_ctrl_packet() - allocate control packet skb
526 * on success returns a reference to the control packet in @pkt.
544 * qrtr_endpoint_register() - register a new endpoint
546 * @nid: desired node id; may be QRTR_EP_NID_AUTO for auto-assignment
555 if (!ep || !ep->xmit) in qrtr_endpoint_register()
556 return -EINVAL; in qrtr_endpoint_register()
560 return -ENOMEM; in qrtr_endpoint_register()
562 kref_init(&node->ref); in qrtr_endpoint_register()
563 mutex_init(&node->ep_lock); in qrtr_endpoint_register()
564 skb_queue_head_init(&node->rx_queue); in qrtr_endpoint_register()
565 node->nid = QRTR_EP_NID_AUTO; in qrtr_endpoint_register()
566 node->ep = ep; in qrtr_endpoint_register()
568 INIT_RADIX_TREE(&node->qrtr_tx_flow, GFP_KERNEL); in qrtr_endpoint_register()
569 mutex_init(&node->qrtr_tx_lock); in qrtr_endpoint_register()
574 list_add(&node->item, &qrtr_all_nodes); in qrtr_endpoint_register()
576 ep->node = node; in qrtr_endpoint_register()
583 * qrtr_endpoint_unregister - unregister endpoint
588 struct qrtr_node *node = ep->node; in qrtr_endpoint_unregister()
589 struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL}; in qrtr_endpoint_unregister()
593 struct qrtr_tx_flow *flow; in qrtr_endpoint_unregister() local
597 mutex_lock(&node->ep_lock); in qrtr_endpoint_unregister()
598 node->ep = NULL; in qrtr_endpoint_unregister()
599 mutex_unlock(&node->ep_lock); in qrtr_endpoint_unregister()
604 pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE); in qrtr_endpoint_unregister()
608 /* Wake up any transmitters waiting for resume-tx from the node */ in qrtr_endpoint_unregister()
609 mutex_lock(&node->qrtr_tx_lock); in qrtr_endpoint_unregister()
610 radix_tree_for_each_slot(slot, &node->qrtr_tx_flow, &iter, 0) { in qrtr_endpoint_unregister()
611 flow = *slot; in qrtr_endpoint_unregister()
612 wake_up_interruptible_all(&flow->resume_tx); in qrtr_endpoint_unregister()
614 mutex_unlock(&node->qrtr_tx_lock); in qrtr_endpoint_unregister()
617 ep->node = NULL; in qrtr_endpoint_unregister()
635 sock_hold(&ipc->sk); in qrtr_port_lookup()
644 sock_put(&ipc->sk); in qrtr_port_put()
652 int port = ipc->us.sq_port; in qrtr_port_remove()
661 pkt->cmd = cpu_to_le32(QRTR_TYPE_DEL_CLIENT); in qrtr_port_remove()
662 pkt->client.node = cpu_to_le32(ipc->us.sq_node); in qrtr_port_remove()
663 pkt->client.port = cpu_to_le32(ipc->us.sq_port); in qrtr_port_remove()
665 skb_set_owner_w(skb, &ipc->sk); in qrtr_port_remove()
666 qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us, in qrtr_port_remove()
673 __sock_put(&ipc->sk); in qrtr_port_remove()
706 rc = -EACCES; in qrtr_port_assign()
718 if (rc == -ENOSPC) in qrtr_port_assign()
719 return -EADDRINUSE; in qrtr_port_assign()
723 sock_hold(&ipc->sk); in qrtr_port_assign()
728 /* Reset all non-control ports */
736 /* Don't reset control port */ in qrtr_reset_ports()
740 sock_hold(&ipc->sk); in qrtr_reset_ports()
741 ipc->sk.sk_err = ENETRESET; in qrtr_reset_ports()
742 ipc->sk.sk_error_report(&ipc->sk); in qrtr_reset_ports()
743 sock_put(&ipc->sk); in qrtr_reset_ports()
755 struct qrtr_sock *ipc = qrtr_sk(sock->sk); in __qrtr_bind()
756 struct sock *sk = sock->sk; in __qrtr_bind()
761 if (!zapped && addr->sq_port == ipc->us.sq_port) in __qrtr_bind()
764 port = addr->sq_port; in __qrtr_bind()
772 ipc->us.sq_port = port; in __qrtr_bind()
783 /* Auto bind to an ephemeral port. */
786 struct sock *sk = sock->sk; in qrtr_autobind()
803 struct qrtr_sock *ipc = qrtr_sk(sock->sk); in qrtr_bind()
804 struct sock *sk = sock->sk; in qrtr_bind()
807 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR) in qrtr_bind()
808 return -EINVAL; in qrtr_bind()
810 if (addr->sq_node != ipc->us.sq_node) in qrtr_bind()
811 return -EINVAL; in qrtr_bind()
828 ipc = qrtr_port_lookup(to->sq_port); in qrtr_local_enqueue()
829 if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */ in qrtr_local_enqueue()
831 return -ENODEV; in qrtr_local_enqueue()
834 cb = (struct qrtr_cb *)skb->cb; in qrtr_local_enqueue()
835 cb->src_node = from->sq_node; in qrtr_local_enqueue()
836 cb->src_port = from->sq_port; in qrtr_local_enqueue()
838 if (sock_queue_rcv_skb(&ipc->sk, skb)) { in qrtr_local_enqueue()
841 return -ENOSPC; in qrtr_local_enqueue()
861 skb_set_owner_w(skbn, skb->sk); in qrtr_bcast_enqueue()
873 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name); in qrtr_sendmsg()
877 struct qrtr_sock *ipc = qrtr_sk(sock->sk); in qrtr_sendmsg()
878 struct sock *sk = sock->sk; in qrtr_sendmsg()
885 if (msg->msg_flags & ~(MSG_DONTWAIT)) in qrtr_sendmsg()
886 return -EINVAL; in qrtr_sendmsg()
889 return -EMSGSIZE; in qrtr_sendmsg()
894 if (msg->msg_namelen < sizeof(*addr)) { in qrtr_sendmsg()
896 return -EINVAL; in qrtr_sendmsg()
899 if (addr->sq_family != AF_QIPCRTR) { in qrtr_sendmsg()
901 return -EINVAL; in qrtr_sendmsg()
909 } else if (sk->sk_state == TCP_ESTABLISHED) { in qrtr_sendmsg()
910 addr = &ipc->peer; in qrtr_sendmsg()
913 return -ENOTCONN; in qrtr_sendmsg()
917 if (addr->sq_node == QRTR_NODE_BCAST) { in qrtr_sendmsg()
918 if (addr->sq_port != QRTR_PORT_CTRL && in qrtr_sendmsg()
921 return -ENOTCONN; in qrtr_sendmsg()
924 } else if (addr->sq_node == ipc->us.sq_node) { in qrtr_sendmsg()
927 node = qrtr_node_lookup(addr->sq_node); in qrtr_sendmsg()
930 return -ECONNRESET; in qrtr_sendmsg()
937 msg->msg_flags & MSG_DONTWAIT, &rc); in qrtr_sendmsg()
949 if (ipc->us.sq_port == QRTR_PORT_CTRL) { in qrtr_sendmsg()
951 rc = -EINVAL; in qrtr_sendmsg()
956 /* control messages already require the type as 'command' */ in qrtr_sendmsg()
961 rc = enqueue_fn(node, skb, type, &ipc->us, addr); in qrtr_sendmsg()
974 struct sockaddr_qrtr remote = { AF_QIPCRTR, cb->src_node, cb->src_port }; in qrtr_send_resume_tx()
975 struct sockaddr_qrtr local = { AF_QIPCRTR, cb->dst_node, cb->dst_port }; in qrtr_send_resume_tx()
983 return -EINVAL; in qrtr_send_resume_tx()
987 return -ENOMEM; in qrtr_send_resume_tx()
989 pkt->cmd = cpu_to_le32(QRTR_TYPE_RESUME_TX); in qrtr_send_resume_tx()
990 pkt->client.node = cpu_to_le32(cb->dst_node); in qrtr_send_resume_tx()
991 pkt->client.port = cpu_to_le32(cb->dst_port); in qrtr_send_resume_tx()
1003 DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name); in qrtr_recvmsg()
1004 struct sock *sk = sock->sk; in qrtr_recvmsg()
1013 return -EADDRNOTAVAIL; in qrtr_recvmsg()
1022 cb = (struct qrtr_cb *)skb->cb; in qrtr_recvmsg()
1024 copied = skb->len; in qrtr_recvmsg()
1027 msg->msg_flags |= MSG_TRUNC; in qrtr_recvmsg()
1036 addr->sq_family = AF_QIPCRTR; in qrtr_recvmsg()
1037 addr->sq_node = cb->src_node; in qrtr_recvmsg()
1038 addr->sq_port = cb->src_port; in qrtr_recvmsg()
1039 msg->msg_namelen = sizeof(*addr); in qrtr_recvmsg()
1043 if (cb->confirm_rx) in qrtr_recvmsg()
1056 struct qrtr_sock *ipc = qrtr_sk(sock->sk); in qrtr_connect()
1057 struct sock *sk = sock->sk; in qrtr_connect()
1060 if (len < sizeof(*addr) || addr->sq_family != AF_QIPCRTR) in qrtr_connect()
1061 return -EINVAL; in qrtr_connect()
1065 sk->sk_state = TCP_CLOSE; in qrtr_connect()
1066 sock->state = SS_UNCONNECTED; in qrtr_connect()
1074 ipc->peer = *addr; in qrtr_connect()
1075 sock->state = SS_CONNECTED; in qrtr_connect()
1076 sk->sk_state = TCP_ESTABLISHED; in qrtr_connect()
1086 struct qrtr_sock *ipc = qrtr_sk(sock->sk); in qrtr_getname()
1088 struct sock *sk = sock->sk; in qrtr_getname()
1092 if (sk->sk_state != TCP_ESTABLISHED) { in qrtr_getname()
1094 return -ENOTCONN; in qrtr_getname()
1097 qaddr = ipc->peer; in qrtr_getname()
1099 qaddr = ipc->us; in qrtr_getname()
1113 struct qrtr_sock *ipc = qrtr_sk(sock->sk); in qrtr_ioctl()
1114 struct sock *sk = sock->sk; in qrtr_ioctl()
1125 len = sk->sk_sndbuf - sk_wmem_alloc_get(sk); in qrtr_ioctl()
1131 skb = skb_peek(&sk->sk_receive_queue); in qrtr_ioctl()
1133 len = skb->len; in qrtr_ioctl()
1138 rc = -EFAULT; in qrtr_ioctl()
1143 *sq = ipc->us; in qrtr_ioctl()
1145 rc = -EFAULT; in qrtr_ioctl()
1158 rc = -EINVAL; in qrtr_ioctl()
1161 rc = -ENOIOCTLCMD; in qrtr_ioctl()
1172 struct sock *sk = sock->sk; in qrtr_release()
1181 sk->sk_shutdown = SHUTDOWN_MASK; in qrtr_release()
1183 sk->sk_state_change(sk); in qrtr_release()
1187 sock->sk = NULL; in qrtr_release()
1192 skb_queue_purge(&sk->sk_receive_queue); in qrtr_release()
1232 if (sock->type != SOCK_DGRAM) in qrtr_create()
1233 return -EPROTOTYPE; in qrtr_create()
1237 return -ENOMEM; in qrtr_create()
1242 sock->ops = &qrtr_proto_ops; in qrtr_create()
1245 ipc->us.sq_family = AF_QIPCRTR; in qrtr_create()
1246 ipc->us.sq_node = qrtr_local_nid; in qrtr_create()
1247 ipc->us.sq_port = 0; in qrtr_create()
1286 MODULE_DESCRIPTION("Qualcomm IPC-router driver");