aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHendrik Brueckner <brueckner@linux.vnet.ibm.com>2009-01-09 06:14:58 -0500
committerMartin Schwidefsky <schwidefsky@de.ibm.com>2009-01-09 06:15:08 -0500
commitc45ce4b5774498ae919f6219c751e824a7196e2b (patch)
tree867a8db3c10faca35726fba34beafaa0fa5a994b
parent2dc184c0ba5c8661649ed2ca0b9e97ed49860cb5 (diff)
[S390] hvc_iucv: Limit rate of outgoing IUCV messages
This patch introduces a send buffer to limit outgoing IUCV messages up to a maximum of 25 IUCV messages per second. If no communication path to a IUCV HVC terminal exist, any data written to the terminal is discarded. Signed-off-by: Hendrik Brueckner <brueckner@linux.vnet.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
-rw-r--r--drivers/char/hvc_iucv.c211
1 files changed, 174 insertions, 37 deletions
diff --git a/drivers/char/hvc_iucv.c b/drivers/char/hvc_iucv.c
index a80d92233169..a23f7e8fba86 100644
--- a/drivers/char/hvc_iucv.c
+++ b/drivers/char/hvc_iucv.c
@@ -12,9 +12,11 @@
12 12
13#include <linux/types.h> 13#include <linux/types.h>
14#include <asm/ebcdic.h> 14#include <asm/ebcdic.h>
15#include <linux/delay.h>
15#include <linux/mempool.h> 16#include <linux/mempool.h>
16#include <linux/module.h> 17#include <linux/module.h>
17#include <linux/tty.h> 18#include <linux/tty.h>
19#include <linux/wait.h>
18#include <net/iucv/iucv.h> 20#include <net/iucv/iucv.h>
19 21
20#include "hvc_console.h" 22#include "hvc_console.h"
@@ -37,7 +39,7 @@
37struct iucv_tty_msg { 39struct iucv_tty_msg {
38 u8 version; /* Message version */ 40 u8 version; /* Message version */
39 u8 type; /* Message type */ 41 u8 type; /* Message type */
40#define MSG_MAX_DATALEN (~(u16)0) 42#define MSG_MAX_DATALEN ((u16)(~0))
41 u16 datalen; /* Payload length */ 43 u16 datalen; /* Payload length */
42 u8 data[]; /* Payload buffer */ 44 u8 data[]; /* Payload buffer */
43} __attribute__((packed)); 45} __attribute__((packed));
@@ -60,6 +62,12 @@ struct hvc_iucv_private {
60 enum tty_state_t tty_state; /* TTY status */ 62 enum tty_state_t tty_state; /* TTY status */
61 struct iucv_path *path; /* IUCV path pointer */ 63 struct iucv_path *path; /* IUCV path pointer */
62 spinlock_t lock; /* hvc_iucv_private lock */ 64 spinlock_t lock; /* hvc_iucv_private lock */
65#define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
66 void *sndbuf; /* send buffer */
67 size_t sndbuf_len; /* length of send buffer */
68#define QUEUE_SNDBUF_DELAY (HZ / 25)
69 struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
70 wait_queue_head_t sndbuf_waitq; /* wait for send completion */
63 struct list_head tty_outqueue; /* outgoing IUCV messages */ 71 struct list_head tty_outqueue; /* outgoing IUCV messages */
64 struct list_head tty_inqueue; /* incoming IUCV messages */ 72 struct list_head tty_inqueue; /* incoming IUCV messages */
65}; 73};
@@ -318,57 +326,118 @@ static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
318} 326}
319 327
320/** 328/**
321 * hvc_iucv_send() - Send an IUCV message containing terminal data. 329 * hvc_iucv_queue() - Buffer terminal data for sending.
322 * @priv: Pointer to struct hvc_iucv_private instance. 330 * @priv: Pointer to struct hvc_iucv_private instance.
323 * @buf: Buffer containing data to send. 331 * @buf: Buffer containing data to send.
324 * @size: Size of buffer and amount of data to send. 332 * @count: Size of buffer and amount of data to send.
325 * 333 *
326 * If an IUCV communication path is established, the function copies the buffer 334 * The function queues data for sending. To actually send the buffered data,
327 * data to a newly allocated struct iucv_tty_buffer element, sends the data and 335 * a work queue function is * scheduled (with QUEUE_SNDBUF_DELAY).
328 * puts the element to the outqueue. 336 * The function returns the number of data bytes that has been buffered.
329 * 337 *
330 * If there is no IUCV communication path established, the function returns 0. 338 * If the device is not connected, data is ignored and the function returns
339 * @count.
340 * If the buffer is full, the function returns 0.
331 * If an existing IUCV communicaton path has been severed, the function returns 341 * If an existing IUCV communicaton path has been severed, the function returns
332 * -EPIPE (can be passed to HVC layer to cause a tty hangup). 342 * -EPIPE (can be passed to HVC layer to cause a tty hangup).
333 */ 343 */
334static int hvc_iucv_send(struct hvc_iucv_private *priv, const char *buf, 344static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
335 int count) 345 int count)
336{ 346{
347 size_t len;
348
349 if (priv->iucv_state == IUCV_DISCONN)
350 return count; /* ignore data */
351
352 if (priv->iucv_state == IUCV_SEVERED)
353 return -EPIPE;
354
355 len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
356 if (!len)
357 return 0;
358
359 memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
360 priv->sndbuf_len += len;
361
362 if (priv->iucv_state == IUCV_CONNECTED)
363 schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
364
365 return len;
366}
367
368/**
369 * hvc_iucv_send() - Send an IUCV message containing terminal data.
370 * @priv: Pointer to struct hvc_iucv_private instance.
371 *
372 * If an IUCV communication path has been established, the queued data
373 * for output are sent via an IUCV message.
374 *
375 * If there is no IUCV communication path established, the function returns 0.
376 * If an existing IUCV communicaton path has been severed, the function returns
377 * -EPIPE.
378 */
379static int hvc_iucv_send(struct hvc_iucv_private *priv)
380{
337 struct iucv_tty_buffer *sb; 381 struct iucv_tty_buffer *sb;
338 int rc; 382 int rc, len;
339 u16 len;
340 383
341 if (priv->iucv_state == IUCV_SEVERED) 384 if (priv->iucv_state == IUCV_SEVERED)
342 return -EPIPE; 385 return -EPIPE;
343 386
344 if (priv->iucv_state == IUCV_DISCONN) 387 if (priv->iucv_state == IUCV_DISCONN)
345 return 0; 388 return -EIO;
346 389
347 len = min_t(u16, MSG_MAX_DATALEN, count); 390 if (!priv->sndbuf_len)
391 return 0;
348 392
349 /* allocate internal buffer to store msg data and also compute total 393 /* allocate internal buffer to store msg data and also compute total
350 * message length */ 394 * message length */
351 sb = alloc_tty_buffer(len, GFP_ATOMIC); 395 sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
352 if (!sb) 396 if (!sb)
353 return -ENOMEM; 397 return -ENOMEM;
354 398
355 sb->mbuf->datalen = len; 399 memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
356 memcpy(sb->mbuf->data, buf, len); 400 sb->mbuf->datalen = (u16) priv->sndbuf_len;
401 sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
357 402
358 list_add_tail(&sb->list, &priv->tty_outqueue); 403 list_add_tail(&sb->list, &priv->tty_outqueue);
359 404
360 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0, 405 rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
361 (void *) sb->mbuf, sb->msg.length); 406 (void *) sb->mbuf, sb->msg.length);
362 if (rc) { 407 if (rc) {
408 /* drop the message here; however we might want to handle
409 * 0x03 (msg limit reached) by trying again... */
363 list_del(&sb->list); 410 list_del(&sb->list);
364 destroy_tty_buffer(sb); 411 destroy_tty_buffer(sb);
365 len = 0;
366 } 412 }
413 len = priv->sndbuf_len;
414 priv->sndbuf_len = 0;
367 415
368 return len; 416 return len;
369} 417}
370 418
371/** 419/**
420 * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
421 * @work: Work structure.
422 *
423 * The function sends buffered output data over IUCV and, if necessary,
424 * reschedules itself if not all buffered data could be sent.
425 */
426static void hvc_iucv_sndbuf_work(struct work_struct *work)
427{
428 struct hvc_iucv_private *priv;
429
430 priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
431
432 if (!priv)
433 return;
434
435 spin_lock_bh(&priv->lock);
436 hvc_iucv_send(priv);
437 spin_unlock_bh(&priv->lock);
438}
439
440/**
372 * hvc_iucv_put_chars() - HVC put_chars operation. 441 * hvc_iucv_put_chars() - HVC put_chars operation.
373 * @vtermno: HVC virtual terminal number. 442 * @vtermno: HVC virtual terminal number.
374 * @buf: Pointer to an buffer to read data from 443 * @buf: Pointer to an buffer to read data from
@@ -385,7 +454,7 @@ static int hvc_iucv_send(struct hvc_iucv_private *priv, const char *buf,
385static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count) 454static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
386{ 455{
387 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno); 456 struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
388 int sent; 457 int queued;
389 458
390 if (count <= 0) 459 if (count <= 0)
391 return 0; 460 return 0;
@@ -394,10 +463,10 @@ static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
394 return -ENODEV; 463 return -ENODEV;
395 464
396 spin_lock(&priv->lock); 465 spin_lock(&priv->lock);
397 sent = hvc_iucv_send(priv, buf, count); 466 queued = hvc_iucv_queue(priv, buf, count);
398 spin_unlock(&priv->lock); 467 spin_unlock(&priv->lock);
399 468
400 return sent; 469 return queued;
401} 470}
402 471
403/** 472/**
@@ -441,6 +510,46 @@ static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
441 510
442 priv->tty_state = TTY_CLOSED; 511 priv->tty_state = TTY_CLOSED;
443 priv->iucv_state = IUCV_DISCONN; 512 priv->iucv_state = IUCV_DISCONN;
513
514 priv->sndbuf_len = 0;
515}
516
517/**
518 * tty_outqueue_empty() - Test if the tty outq is empty
519 * @priv: Pointer to struct hvc_iucv_private instance.
520 */
521static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
522{
523 int rc;
524
525 spin_lock_bh(&priv->lock);
526 rc = list_empty(&priv->tty_outqueue);
527 spin_unlock_bh(&priv->lock);
528
529 return rc;
530}
531
532/**
533 * flush_sndbuf_sync() - Flush send buffer and wait for completion
534 * @priv: Pointer to struct hvc_iucv_private instance.
535 *
536 * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
537 * to flush any buffered terminal output data and waits for completion.
538 */
539static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
540{
541 int sync_wait;
542
543 cancel_delayed_work_sync(&priv->sndbuf_work);
544
545 spin_lock_bh(&priv->lock);
546 hvc_iucv_send(priv); /* force sending buffered data */
547 sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
548 spin_unlock_bh(&priv->lock);
549
550 if (sync_wait)
551 wait_event_timeout(priv->sndbuf_waitq,
552 tty_outqueue_empty(priv), HZ);
444} 553}
445 554
446/** 555/**
@@ -471,6 +580,8 @@ static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
471 if (!priv) 580 if (!priv)
472 return; 581 return;
473 582
583 flush_sndbuf_sync(priv);
584
474 spin_lock_bh(&priv->lock); 585 spin_lock_bh(&priv->lock);
475 /* NOTE: If the hangup was scheduled by ourself (from the iucv 586 /* NOTE: If the hangup was scheduled by ourself (from the iucv
476 * path_servered callback [IUCV_SEVERED]), then we have to 587 * path_servered callback [IUCV_SEVERED]), then we have to
@@ -510,6 +621,8 @@ static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
510 if (!priv) 621 if (!priv)
511 return; 622 return;
512 623
624 flush_sndbuf_sync(priv);
625
513 spin_lock_bh(&priv->lock); 626 spin_lock_bh(&priv->lock);
514 path = priv->path; /* save reference to IUCV path */ 627 path = priv->path; /* save reference to IUCV path */
515 priv->path = NULL; 628 priv->path = NULL;
@@ -588,6 +701,9 @@ static int hvc_iucv_path_pending(struct iucv_path *path,
588 priv->path = path; 701 priv->path = path;
589 priv->iucv_state = IUCV_CONNECTED; 702 priv->iucv_state = IUCV_CONNECTED;
590 703
704 /* flush buffered output data... */
705 schedule_delayed_work(&priv->sndbuf_work, 5);
706
591out_path_handled: 707out_path_handled:
592 spin_unlock(&priv->lock); 708 spin_unlock(&priv->lock);
593 return 0; 709 return 0;
@@ -615,15 +731,18 @@ static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
615 spin_lock(&priv->lock); 731 spin_lock(&priv->lock);
616 priv->iucv_state = IUCV_SEVERED; 732 priv->iucv_state = IUCV_SEVERED;
617 733
618 /* NOTE: If the tty has not yet been opened by a getty program 734 /* If the tty has not yet been opened, clean up the hvc_iucv_private
619 * (e.g. to see console messages), then cleanup the 735 * structure to allow re-connects.
620 * hvc_iucv_private structure to allow re-connects.
621 * 736 *
622 * If the tty has been opened, the get_chars() callback returns 737 * If it has been opened, let get_chars() return -EPIPE to signal the
623 * -EPIPE to signal the hvc console layer to hang up the tty. */ 738 * HVC layer to hang up the tty.
739 * If so, we need to wake up the HVC thread to call get_chars()...
740 */
624 priv->path = NULL; 741 priv->path = NULL;
625 if (priv->tty_state == TTY_CLOSED) 742 if (priv->tty_state == TTY_CLOSED)
626 hvc_iucv_cleanup(priv); 743 hvc_iucv_cleanup(priv);
744 else
745 hvc_kick();
627 spin_unlock(&priv->lock); 746 spin_unlock(&priv->lock);
628 747
629 /* finally sever path (outside of priv->lock due to lock ordering) */ 748 /* finally sever path (outside of priv->lock due to lock ordering) */
@@ -648,6 +767,12 @@ static void hvc_iucv_msg_pending(struct iucv_path *path,
648 struct hvc_iucv_private *priv = path->private; 767 struct hvc_iucv_private *priv = path->private;
649 struct iucv_tty_buffer *rb; 768 struct iucv_tty_buffer *rb;
650 769
770 /* reject messages that exceed max size of iucv_tty_msg->datalen */
771 if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
772 iucv_message_reject(path, msg);
773 return;
774 }
775
651 spin_lock(&priv->lock); 776 spin_lock(&priv->lock);
652 777
653 /* reject messages if tty has not yet been opened */ 778 /* reject messages if tty has not yet been opened */
@@ -656,7 +781,7 @@ static void hvc_iucv_msg_pending(struct iucv_path *path,
656 goto unlock_return; 781 goto unlock_return;
657 } 782 }
658 783
659 /* allocate buffer an empty buffer element */ 784 /* allocate tty buffer to save iucv msg only */
660 rb = alloc_tty_buffer(0, GFP_ATOMIC); 785 rb = alloc_tty_buffer(0, GFP_ATOMIC);
661 if (!rb) { 786 if (!rb) {
662 iucv_message_reject(path, msg); 787 iucv_message_reject(path, msg);
@@ -666,7 +791,7 @@ static void hvc_iucv_msg_pending(struct iucv_path *path,
666 791
667 list_add_tail(&rb->list, &priv->tty_inqueue); 792 list_add_tail(&rb->list, &priv->tty_inqueue);
668 793
669 hvc_kick(); /* wakup hvc console thread */ 794 hvc_kick(); /* wake up hvc console thread */
670 795
671unlock_return: 796unlock_return:
672 spin_unlock(&priv->lock); 797 spin_unlock(&priv->lock);
@@ -697,6 +822,7 @@ static void hvc_iucv_msg_complete(struct iucv_path *path,
697 list_move(&ent->list, &list_remove); 822 list_move(&ent->list, &list_remove);
698 break; 823 break;
699 } 824 }
825 wake_up(&priv->sndbuf_waitq);
700 spin_unlock(&priv->lock); 826 spin_unlock(&priv->lock);
701 destroy_tty_buffer_list(&list_remove); 827 destroy_tty_buffer_list(&list_remove);
702} 828}
@@ -732,16 +858,28 @@ static int __init hvc_iucv_alloc(int id)
732 spin_lock_init(&priv->lock); 858 spin_lock_init(&priv->lock);
733 INIT_LIST_HEAD(&priv->tty_outqueue); 859 INIT_LIST_HEAD(&priv->tty_outqueue);
734 INIT_LIST_HEAD(&priv->tty_inqueue); 860 INIT_LIST_HEAD(&priv->tty_inqueue);
861 INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
862 init_waitqueue_head(&priv->sndbuf_waitq);
863
864 priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
865 if (!priv->sndbuf) {
866 kfree(priv);
867 return -ENOMEM;
868 }
735 869
736 /* Finally allocate hvc */ 870 /* Finally allocate hvc */
737 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, 871 priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
738 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, PAGE_SIZE); 872 HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
739 if (IS_ERR(priv->hvc)) { 873 if (IS_ERR(priv->hvc)) {
740 rc = PTR_ERR(priv->hvc); 874 rc = PTR_ERR(priv->hvc);
875 free_page((unsigned long) priv->sndbuf);
741 kfree(priv); 876 kfree(priv);
742 return rc; 877 return rc;
743 } 878 }
744 879
880 /* kick khvcd thread; instead of using polling */
881 priv->hvc->irq_requested = 1;
882
745 /* setup iucv related information */ 883 /* setup iucv related information */
746 snprintf(name, 9, "lnxhvc%-2d", id); 884 snprintf(name, 9, "lnxhvc%-2d", id);
747 memcpy(priv->srv_name, name, 8); 885 memcpy(priv->srv_name, name, 8);
@@ -759,8 +897,8 @@ static int __init hvc_iucv_init(void)
759 int rc, i; 897 int rc, i;
760 898
761 if (!MACHINE_IS_VM) { 899 if (!MACHINE_IS_VM) {
762 pr_warning("The z/VM IUCV Hypervisor console cannot be " 900 pr_info("The z/VM IUCV HVC device driver cannot "
763 "used without z/VM.\n"); 901 "be used without z/VM\n");
764 return -ENODEV; 902 return -ENODEV;
765 } 903 }
766 904
@@ -774,16 +912,14 @@ static int __init hvc_iucv_init(void)
774 sizeof(struct iucv_tty_buffer), 912 sizeof(struct iucv_tty_buffer),
775 0, 0, NULL); 913 0, 0, NULL);
776 if (!hvc_iucv_buffer_cache) { 914 if (!hvc_iucv_buffer_cache) {
777 pr_err("Not enough memory for driver initialization " 915 pr_err("Allocating memory failed with reason code=%d\n", 1);
778 "(rs=%d).\n", 1);
779 return -ENOMEM; 916 return -ENOMEM;
780 } 917 }
781 918
782 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR, 919 hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
783 hvc_iucv_buffer_cache); 920 hvc_iucv_buffer_cache);
784 if (!hvc_iucv_mempool) { 921 if (!hvc_iucv_mempool) {
785 pr_err("Not enough memory for driver initialization " 922 pr_err("Allocating memory failed with reason code=%d\n", 2);
786 "(rs=%d).\n", 2);
787 kmem_cache_destroy(hvc_iucv_buffer_cache); 923 kmem_cache_destroy(hvc_iucv_buffer_cache);
788 return -ENOMEM; 924 return -ENOMEM;
789 } 925 }
@@ -792,8 +928,8 @@ static int __init hvc_iucv_init(void)
792 for (i = 0; i < hvc_iucv_devices; i++) { 928 for (i = 0; i < hvc_iucv_devices; i++) {
793 rc = hvc_iucv_alloc(i); 929 rc = hvc_iucv_alloc(i);
794 if (rc) { 930 if (rc) {
795 pr_err("Could not create new z/VM IUCV HVC backend " 931 pr_err("Creating a new HVC terminal device "
796 "rc=%d.\n", rc); 932 "failed with error code=%d\n", rc);
797 goto out_error_hvc; 933 goto out_error_hvc;
798 } 934 }
799 } 935 }
@@ -801,7 +937,8 @@ static int __init hvc_iucv_init(void)
801 /* register IUCV callback handler */ 937 /* register IUCV callback handler */
802 rc = iucv_register(&hvc_iucv_handler, 0); 938 rc = iucv_register(&hvc_iucv_handler, 0);
803 if (rc) { 939 if (rc) {
804 pr_err("Could not register iucv handler (rc=%d).\n", rc); 940 pr_err("Registering IUCV handlers failed with error code=%d\n",
941 rc);
805 goto out_error_iucv; 942 goto out_error_iucv;
806 } 943 }
807 944